7. Person and Customer Classes Design a class named Person with fields for holdi
ID: 3739829 • Letter: 7
Question
7. Person and Customer Classes
Design a class named Person with fields for holding a person’s name, address, and telephone
number. Write one or more constructors and the appropriate mutator and accessor methods
for the class’s fields.
Next, design a class named Customer , which inherits from the Person class. The Customer
class should have a field for a customer number and a boolean field indicating whether the
customer wishes to be on a mailing list. Write one or more constructors and the appropriate
mutator and accessor methods for the class’s fields. Demonstrate an object of the Customer
class in a simple program.
8. PreferredCustomer Class
A retail store has a preferred customer plan where customers can earn discounts on all
their purchases. The amount of a customer’s discount is determined by the amount of the
customer’s cumulative purchases in the store, as follows:
? When a preferred customer spends $500, he or she gets a 5% discount on all future
purchases.
? When a preferred customer spends $1,000, he or she gets a 6% discount on all future
purchases.
? When a preferred customer spends $1,500, he or she gets a 7% discount on all future
purchases.
? When a preferred customer spends $2,000 or more, he or she gets a 10% discount on
all future purchases.
Design a class named PreferredCustomer , which inherits from the Customer class you created
in Programming Challenge 7. The PreferredCustomer class should have fields for
the amount of the customer’s purchases and the customer’s discount level. Write one or
more constructors and the appropriate mutator and accessor methods for the class’s fields.
Demonstrate the class in a simple program.
Explanation / Answer
Below is your code. I have added comments in the code to help you understand. Let me know in comments if you have any trouble understanding the solution..
Person.java
public class Person {
// Members of the class person
String name;
String address;
String telephoneNumber;
// Constructors for the data members
public Person(String name, String address, String telephoneNumber) {
this.name = name;
this.address = address;
this.telephoneNumber = telephoneNumber;
}
//Default Constructor
public Person() {
this.name = "";
this.address = "";
this.telephoneNumber = "";
}
// "setName" stores the parameter "name"
public void setName(String name) {
this.name = name;
}
// "setAddress" stores the parameter "address"
public void setAddress(String address) {
this.address = address;
}
// "setTelephoneNumber" stores the parameter value for "telephoneNumber"
public void setTelephoneNumber(String telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
// "getName" returns the parameter value "name"
public String getName() {
return name;
}
// "getAddress" returns the parameter value "address"
public String getAddress() {
return address;
}
// "getPhoneNumber" returns the parameter value "telephoneNumber"
public String getPhoneNumber() {
return telephoneNumber;
}
public String toString() {
String person = "Name: " + getName() + " Address: " + getAddress() + " PhoneNumber: " + getPhoneNumber();
return person;
}
}
Customer.java
public class Customer extends Person {
String customerNumber;
boolean mailingList;
// Constructors
public Customer(String name, String address, String telephoneNumber, String customerNumber, boolean mailingList) {
super(name, address, telephoneNumber);
this.customerNumber = customerNumber;
this.mailingList = mailingList;
}
// "setCustomerNumber" stores the parameter value "customerNumber"
public void setCustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
// "setMailOnOff" stores the parameter value for "mailingList"
public void setMailOnOff(boolean mailingList) {
this.mailingList = mailingList;
}
// "getMailOnOff" returns the parameter value "mailingList"
public boolean getMailOnOff() {
return mailingList;
}
// "getCustomerNumber" returns the parameter value "customerNumber"
public String getCustomerNumber() {
return customerNumber;
}
public String toString() {
String customer = super.toString() + " Customer Number: " + getCustomerNumber() + " Customer MailList: "
+ getMailOnOff();
return customer;
}
}
PreferredCustomer.java
public class PreferredCustomer extends Customer {
double purchase;
double discount;
// Constructor
public PreferredCustomer(String name, String address, String telephoneNumber, String customerNumber,
boolean mailingList, double purchase) {
super(name, address, telephoneNumber, customerNumber, mailingList);
this.purchase = purchase;
// "setDiscount" method
if (purchase >= 2000)
setDiscount(10);
else if (purchase >= 1500)
setDiscount(7);
else if (purchase == 1000)
setDiscount(6);
else if (purchase >= 500)
setDiscount(5);
}
// "setPurchase" stores the parameter value "setPurchase"
public void setPurchase(double purchase) {
this.purchase = purchase;
}
// "setPurchase" stores the parameter value "setPurchase"
private void setDiscount(double discount) {
this.discount = discount;
}
// "getPurchase" returns purchase value
public double getPurchase() {
return purchase;
}
// "getDiscount" returns discount value
public double getDiscount() {
return discount;
}
public String toString() {
String preferredCustomer = super.toString() + " Purchase" + getPurchase() + " Discount " + getDiscount();
return preferredCustomer;
}
}
PreferredCustomerDemo.java
public class PreferredCustomerDemo {
public static void main(String[] args) {
PreferredCustomer preferredCustomer = new PreferredCustomer("Julie James", "123 Main Street", "555-1212",
"147-A049", true, 1750);
System.out.println(preferredCustomer.toString());
}
}
Output
Name: Julie James
Address: 123 Main Street
PhoneNumber: 555-1212
Customer Number: 147-A049
Customer MailList: true
Purchase1750.0
Discount 7.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.