[Java OOP] relationships4

  • 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 parameter 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 parameter 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 of calculating 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:

public class Entry {
	public static void main(String[] args) {
		Customer customer1 = new Customer(1000, "Tuan", 20);
		Invoice invoice1 = new Invoice(1000, customer1, 100000);
		System.out.println("Customer name: " + invoice1.getCustomerName());
		System.out.println("Invoice after discount: " + invoice1.getAmountAfterDiscount());
	}
}

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

Customer name: Tuan
Invoice after discount: 80000.0

Note: Codelearn compiler accepts the numerical error less than 0.000001

Instruction

Code sample:

File Customer.java

public class Customer {
	private int id;
	private String name;
	private int discount;

	public Customer(int id, String name, int discount) {
		this.id = id;
		this.name = name;
		this.discount = discount;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getDiscount() {
		return discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}
}

File Invoice.java

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

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

	public int getId() {
		return id;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public String getCustomerName() {
		return customer.getName();
	}

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