Bài tập C++ OOP: tạo ra lớp Customer và Invoice | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create a Customer class and an Invoice class with the following information:

Explanation:

The above diagram describes the information about customers and their invoices.

Customer class

  • This class is used for storing information about customers.
  • id, name and discount attributes are customer ID, customer name and discount rate (as percentage) of a customer respectively.
  • Customer(id: int, name: String, discount: int) is the parameterized constructor which is used for initializing values to corresponding attributes.
  • getId(), setName(), getName(), setDiscount(), getDiscount() are setters and getters. This class does not include setId() method because ID is unchanged.

Invoice class

  • This class is used for storing information about customers' invoices.
  • id, customer, amount are ID invoice, customer and the amount of money on a bill.
  • Invoice(id: int, customer: Customer, amount: double) is the parameterized constructor which is used for initializing values to corresponding attributes.
  • getId(), setCustomer(), getCustomer(), setAmount(), getAmount() are setters and getters. This class does not include setId() method.
  • getCustomerName() method returns the customer's name.
  • getAmountAfterDiscount() is the method to calculate the bill after the discount has been applied to customers. For example, if discount = 20 (20%), the getAmountAfterDiscount() method returns the following result 80000. Because 10000 - 10000 * 0.2 = 80000.

A program to test the above classes:

#include "Customer.cpp"
#include "Invoice.cpp"

int main() {
	Customer customer1(1000, "Tuan", 20);
	Invoice invoice1(1000, customer1, 100000);
	cout << "Customer name: " << invoice1.getCustomerName() << endl;
	cout << "Invoice after discount: " << invoice1.getAmountAfterDiscount() << endl;

	return 0;
}

When the above code is compiled and executed, it produces the following result:

Customer name: Tuan
Invoice after discount: 80000

Note: Codelearn compiler accepts the numerical error less than 0.000001

Instruction

Code sample:

Customer.cpp file

#pragma once

#include <iostream>

using namespace std;

class Customer {
private:
	int id;
	string name;
	int discount;

public:
	Customer() {

	}
	Customer(int id, string name, int discount) {
		this->id = id;
		this->name = name;
		this->discount = discount;
	}

	int getId() {
		return id;
	}

	string getName() {
		return name;
	}

	void setName(string name) {
		this->name = name;
	}

	int getDiscount() {
		return discount;
	}

	void setDiscount(int discount) {
		this->discount = discount;
	}
};

Invoice.cpp file

#pragma once

#include "Customer.cpp"

class Invoice {
private:
	int id;
	Customer customer;
	double amount;

public:
	Invoice(int id, Customer customer, double amount) {
		this->id = id;
		this->customer = customer;
		this->amount = amount;
	}

	int getId() {
		return id;
	}

	Customer getCustomer() {
		return customer;
	}

	void setCustomer(Customer customer) {
		this->customer = customer;
	}

	double getAmount() {
		return amount;
	}

	void setAmount(double amount) {
		this->amount = amount;
	}

	string getCustomerName() {
		return customer.getName();
	}

	double getAmountAfterDiscount() {
		return amount - amount * customer.getDiscount() / 100;
	}
};