The Customer class models a customer using as shown in the class diagram below. Write the codes for the Customer class and a test driver to test all the public methods. Note that a discount is given as a percentage. The toString() method should output the customer information including the ID, name and discount
SOLUTION CODE
package com.company;
class Customer {
private String name;
private int ID;
private int discount;
//constructor to set the values
public Customer(String name, int ID, int discount) {
this.name = name;
this.ID = ID;
this.discount = discount;
}
//defining the to string method
public String toString(){//overriding the toString() method
return this.name + ", ID " + this.ID + "discount"+discount+ "%";
}
}
//driver class
class Main {
public static void main(String[] args)
{
Customer c = new Customer("kelly", 100, 10);
c.toString();
}
}
Comments
Leave a comment