Java Help Design a class named Person with fields for holding a person’s name, a
ID: 3867253 • Letter: J
Question
Java Help
Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the mutator and accessor methods for the fields, along with a toString( ) method. Next, design a class named Customer, which inherits from the Person class, The Customer class should have a field for a customer id number and a boolean field indicating if the customer wishes to be on the mailing list. Write one or more constructors and the appropriate mutor and accessor methods for this class. Write a toString( ) method that also calls the super( ) toString method. • A retail store has a preferred customer plan where customers can earn discounts on all their purchases. The amount of the customer’s discout is determined by the amount of the customers 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 get a 7% 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. The PreferredCustomer should have fields for the amount of the customer’s purchases and the customer’s discount level. Write one or more constructors and the mutator and accessor methods along with a toString( ) method that calls the super toString( ) method. Create a Driver class that creates an array of four Persons. Populate the array with Customers and PreferredCustomer object references. Test the classes you wrote by calling the methods and displaying the results.
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.august;
/**
*
* @author Sam
*/
class Person {
private String name;
private String address;
private String telephoneNo;
public Person(String name) {
this.name = name;
}
public Person(String name, String address, String telephoneNo) {
this.name = name;
this.address = address;
this.telephoneNo = telephoneNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephoneNo() {
return telephoneNo;
}
public void setTelephoneNo(String telephoneNo) {
this.telephoneNo = telephoneNo;
}
@Override
public String toString() {
return "Person{" + "name=" + name + ", address=" + address + ", telephoneNo=" + telephoneNo + '}';
}
}
class Customer extends Person {
String custNo;
boolean isInMailingList;
public Customer(String custNo, String name) {
super(name);
this.custNo = custNo;
}
public Customer(String custNo, boolean isInMailingList, String name, String address, String telephoneNo) {
super(name, address, telephoneNo);
this.custNo = custNo;
this.isInMailingList = isInMailingList;
}
public String getCustNo() {
return custNo;
}
public void setCustNo(String custNo) {
this.custNo = custNo;
}
public boolean isIsInMailingList() {
return isInMailingList;
}
public void setIsInMailingList(boolean isInMailingList) {
this.isInMailingList = isInMailingList;
}
@Override
public String toString() {
return "Customer{" + " Person" + super.toString() + " custNo=" + custNo + " isInMailingList=" + isInMailingList + '}';
}
}
class PreferredCustomer extends Customer {
double amountBought;
int discount;
public PreferredCustomer(double amountBought, String custNo, String name) {
super(custNo, name);
this.amountBought = amountBought;
}
public PreferredCustomer(double amountBought, String custNo, boolean isInMailingList, String name, String address, String telephoneNo) {
super(custNo, isInMailingList, name, address, telephoneNo);
this.amountBought = amountBought;
}
public double getAmountBought() {
return amountBought;
}
public void setAmountBought(double amountBought) {
this.amountBought = amountBought;
if (amountBought > 2000)
discount = 10;
else if (amountBought > 1500)
discount = 7;
else if (amountBought > 1000)
discount = 6;
else if (amountBought > 500)
discount = 5;
else
discount = 0;
}
public int getDiscount() {
return discount;
}
@Override
public String toString() {
return "PreferredCustomer{" + " " + super.toString() + " amountBought=" + amountBought + ", discount=" + discount + '}';
}
}
public class PrefferedCustomerDriver {
public static void main(String[] args) {
Person[] persons = new Person[4];
persons[0] = new Customer("C1441", true, "Aska", "Haora", "8902757555");
persons[1] = new PreferredCustomer(1005,"C2231", true, "Jonty", "Adams", "6535-5571");
persons[1] = new PreferredCustomer(314,"C1000", false, "Md. Hashan", "Pinky", "---");
persons[3] = new Customer("C1001", true, "Posu", "Haora", "477125");
for (Person person : persons)
System.out.println(person);
}
}
Here you go champ.. like the code?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.