I need help with question #8 (Scanned and attached from text book). Also include
ID: 3619789 • Letter: I
Question
I need help with question #8 (Scanned and attached from text book). Also included is question 7 because 8 adds onto work done on a classes (called person.java and customer.java(attached)) in Question 7. class Person{ //fields private String person_name; private String person_address; private String person_telnum; //constructor public Person(String person_name, String person_address, String person_telnum) { setName (person_name); setAddress (person_address); setTelephoneNumber (person_telnum); } //accessors functions //Returns name of person public String getName() { return person_name; } //Returns address of person public String getAddress() { return person_address; } //Returns telephone number of person public String getTelephoneNumber() { return person_telnum; } //mutator functions //sets name of person public void setName(String n) { person_name = n; } //sets address of person public void setAddress(String a) { person_address = a; } //sets telephone number of person public void setTelephoneNumber(String n) { person_telnum = n; }} class Customer extends Person{ //fields private int cust_number; private boolean cust_mailinglist; // constructor public Customer(String person_name, String person_address, String person_telnum, int cust_number, boolean mailinglist) { super (person_name, person_address, person_telnum); setCusNumber (cust_number); setMailList (mailinglist); } //accessor functions //returns customer ID public int getCusNumber() { return cust_number; } //returns mail list status public boolean getMailList() { return cust_mailinglist; } //mutator functions //sets customer number public void setCusNumber (int n) { cust_number = n; } //sets mail list status public void setMailList (boolean l) { cust_mailinglist = l; } public String toString() { return "Cust Name: " + getName() + " Cust ID: " + cust_number + " Mail List Status: " + cust_mailinglist; }} //end customer class
Explanation / Answer
class Person{
private String c_Name;
private String c_Address;
private String c_Phone;
public Person(String name, String address, String phone)
{
c_Name = name;
c_Address = address;
c_Phone = phone;
}
public String getName()
{
return c_Name;
}
public String getAddress()
{
return c_Address;
}
public String getPhone()
{
return c_Phone;
}
public void setName(String name)
{
c_Name = name;
}
public void setAddress(String address)
{
c_Address = address;
}
public void setPhone(String phone)
{
c_Phone = phone;
}
public void showObj()
{
System.out.println("Name: " + this.getName());
System.out.println("Adress: " + this.getAddress());
System.out.println("Phone number: " + this.getPhone());
}
}
class Customer extends Person
{
private String c_Number;
private boolean c_Mail;
public Customer(String name, String address, String phone,
String number, boolean mail)
{
super(name, address, phone);
c_Number = number;
c_Mail = mail;
}
public String getNumber()
{
return c_Number;
}
public boolean getMail()
{
return c_Mail;
}
public void setNumber(String number)
{
c_Number = number;
}
public void setMail(boolean mail)
{
c_Mail = mail;
}
public void showObj()
{
super.showObj();
System.out.println("Customer number: " + this.getNumber());
System.out.println("On mailing list: " + (this.getMail() ? "Yes" : "No"));
}
}
class PreferredCustomer extends Customer
{
private double c_Amount;
private int c_Discount;
private void setDiscount()
{
if(c_Amount < 500.00)
c_Discount = 0;
else if(c_Amount >= 500.00 && c_Amount < 1000.00)
c_Discount = 5;
else if(c_Amount >= 1000.00 && c_Amount < 1500.00)
c_Discount = 6;
else if(c_Amount >= 1500.00 && c_Amount < 2000.00)
c_Discount = 7;
else
c_Discount = 10;
}
public PreferredCustomer(String name, String address, String phone,
String number, boolean mail, double amount)
{
super(name, address, phone, number, mail);
c_Amount = amount;
setDiscount();
}
public double getAmount()
{
return c_Amount;
}
public int getDiscount()
{
return c_Discount;
}
public void setAmount(double amount)
{
c_Amount = amount;
setDiscount();
}
public void showObj()
{
super.showObj();
System.out.printf("Purchase amount: $%.2f ", this.getAmount());
System.out.printf("Discount: %d%s ", this.getDiscount(), "%");
}
}
public class Store
{
public static void main(String[] args)
{
PreferredCustomer pc = new
PreferredCustomer("Bob Jones", "1 Pine, Somewhere, CA 22202",
"517-220-8422", "001613467", true, 1000.00);
System.out.println();
pc.showObj();
}
}