Design a class named Person with fields for holding a person\'s name, address, a
ID: 3631243 • Letter: D
Question
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. 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 RM500, he or she gets a 5% discount on all future purchases. When a preferred customer spends RM1000, he or she gets a 6% discount on all future purchases. When a preferred customer spends RM1500, he or she gets a 7% discount on all future purchases. When a preferred customer spends RM2000 or more, he or she gets a 10% discount on all future purchases. Design a class named Preferred Customer, which inherits from the Customer class. The Preferred Customer 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.Explanation / Answer
Updated and corrected version
1 // class Person
2 public class Person
3 {
4 // declare variables
5 private String name;
6 private String address;
7 private int telNumber;
8
9 // constructor
10 public Person(String n, String addr, int no)
11 {
12 name = n;
13 address = addr;
14 telNumber = no;
15 }
16
17 // set the name of Person
18 public void setName(String n)
19 {
20 name = n; // assign the value of name to variable
21 }
22
23 // get the name of Person
24 public String getName()
25 {
26 return name; // return the value of name
27 }
28
29 // set the home address of Person
30 public void setAddr(String addr)
31 {
32 address = addr; // assign the value of address to variable
33 }
34
35 // get the home address of Person
36 public String getAddr()
37 {
38 return address; // return the value of address
39 }
40
41 // set the telephone number of Person
42 public void setTelNo(int no)
43 {
44 telNumber= no; // assign the value of telephone number to variable
45 }
46
47 // get the telephone number of Person
48 public int getTelNo()
49 {
50 return telNumber; // return the value of telephone number
51 }
52 } // end of class Person
1 // class Customer
2 public class Customer extends Person
3 {
4 // declare variables
5 private int custNumber;
6 private boolean onMail;
7
8 // constructor
9 public Customer(String n, String addr, int no, int cno, boolean m)
10 {
11 super(n, addr, no); // inherit the characteristics of Person
12 custNumber = cno;
13 onMail = m;
14 }
15
16 // set the customer number of Customer
17 public void setCustNo(int cno)
18 {
19 custNumber = cno; // assign the value of customer number to variable
20 }
21
22 // get the customer number of Customer
23 public int getCustNo()
24 {
25 return custNumber; // return the value of customer number
26 }
27
28 // set the mailing subscription for Customer
29 public void setOnMail(boolean m)
30 {
31 onMail = m; // assign the value of mailing subscription to variable
32 }
33
34 // get the mailing subscription for Customer
35 public boolean getOnMail()
36 {
37 return onMail; // return the value of mailing subscription
38 }
39 } // end of class Customer
1 // class PreferredCustomer
2 public class PreferredCustomer extends Customer
3 {
4 // declare variables
5 private double amtPurchase;
6 private double discountLvl;
7
8 // constructor
9 public PreferredCustomer(String n, String addr, int no, int cno, boolean m, double amt, double disc)
10 {
11 super(n, addr, no, cno, m); // inherit characteristics of Customer
12 amtPurchase = amt;
13 discountLvl = disc;
14 }
15 // set the purchases' amount paid by favoured/preferred customer
16 public void setAmtPurchase(double amt)
17 {
18 amtPurchase = amt; // assign the value of purchases' amount to variable
19 }
20
21 // get the purchases' amount paid by favoured/preferred customer
22 public double getAmtPurchase()
23 {
24 return amtPurchase; // return the value of purchases' amount
25 }
26
27 // set the discount for favoured/preferred customer
28 public void setDiscountLvl(double disc)
29 {
30 discountLvl = disc; // assign the value of discount to variable
31 }
32
33 // get the discount for favoured/preferred customer
34 public double getDiscountLvl()
35 {
36 return discountLvl; // return the value of discount
37 }
38
39 // compare the favoured/preferred customer's purchases' amount for discount
40 public double checkAmtPurchaseForDiscountLvl()
41 {
42 // get purchases' amount made by favoured/preferred customers
43 double amt = getAmtPurchase();
44
45 // compare purchases' amount and tie it to discount level
46 if (amt >= 2000)
47 {
48 discountLvl = 0.1; // 0.1 equals 10%
49 }
50 else if (amt >= 1500)
51 {
52 discountLvl = 0.07; // 0.07 equals 7%
53 }
54 else if (amt >= 1000)
55 {
56 discountLvl = 0.06; // 0.06 equals 6%
57 }
58 else
59 discountLvl = 0.05; // 0.05 equals 5%
60
61 return discountLvl;
62 }
63 } // end of class PreferredCustomer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.