In: Computer Science
You will write several classes for this program in Java. The name of all classes for this program will start with y2y3.
Concepts to be applied :
Classes and objects, inheritance, polymorphism
Assignment:
An electronic store, named carrefoure, services many customers. The customers’ orders for electronics are delivered. Carrefoure bills their customers once each month. At the end of each month, the regional store manager requests a report of all customers. In this program, you will develop the inheritance hierarchy for the program. The set of classes developed for the program will be used for future assignments.
The superclass for this assignment will represent a Customer of Carrefoure.
Some of the Customers will be tax exempt customers (will not pay taxes on the) but others will be non tax exempt customers (will pay tax on the bill balance at a given percent).
The fields are the instance variables of the classes in the hierarchy:
o phone number (numeric integer)
o Amount paid (numeric)
o Reason for tax exemption (only for tax-exempt-customers) – String, for example: education, non-profit, etc
o Tax percent (only for non-tax-exempt customers) – decimal, for example: .08 for 8%, .075 for 7.5%
The customers served by careffoure are of two types: tax-exempt or non-tax-exempt. The tax-exempt customer will have an instance field to store the reason for the tax exemptions: education, non-profit, government, other (String). The non-tax exempt customers, will have an instance field to store the percent of tax that the customer will pay (numeric) on the bill balance.
From the information provided, write a solution that includes the following:
A suitable inheritance hierarchy which represents the customers serviced by the office supply company. There should be 3 classes for this program. I suggest a Customer class and two suitable subclasses. The tax percent field only applies for a non-tax exempt customer. The reason for tax exemption only applies to a tax exempt customer.
For all classes include the following:
o Instance variables
o Constructors
o Accessor and mutator methods
o Suitable toString() methods
Write a class y2y3 which does the following in the main method:
Create two objects for customers who are tax exempt and create two objects for customers who are non-tax exempt.
Print all the information about the objects as shown below:
Output:
Non tax exempt customers:
1 PWC $750 18002500830 0.08% $60
2 E&Y $970 18003409845 0.08% $77.6
Tax exempt customers:
3 AHS $255.50 19734508345 Non-profit
4 PIO $500 18002708855 Non-profit
Here this question is very simple but there is one thing that i didn't get is in the output of non tax exempt customers like for first customer $750*0.08% = $0.6 not $60. Also what i understand is the only main class needs to be named as y2y3 not all class needs to prepend this one.
Now coming to the solution, since we are not going to create instance of customer class directly we can make that class as abstract class and all the fields are instance one hence every time we create object of inherited class value of those common fields in parent class will hold different value.
Below is the Customer class :
public abstract class Customer {
private String custName;
private int custId;
private long custPhoneNum;
private double amountPaid;
public Customer(String custName, int custId, long custPhoneNum, double amountPaid) {
this.custName = custName;
this.custId = custId;
this.custPhoneNum = custPhoneNum;
this.amountPaid = amountPaid;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public long getCustPhoneNum() {
return custPhoneNum;
}
public void setCustPhoneNum(long custPhoneNum) {
this.custPhoneNum = custPhoneNum;
}
public double getAmountPaid() {
return amountPaid;
}
public void setAmountPaid(double amountPaid) {
this.amountPaid = amountPaid;
}
@Override
public String toString() {
return custId+" " +custName +" $"+amountPaid+" "+custPhoneNum;
}
}
class for taxExemptCustomer :
public class TaxExemptCustomer extends Customer{
private String reason;
public TaxExemptCustomer(String custName, int custId, long custPhoneNum, double amountPaid, String reason) {
super(custName, custId, custPhoneNum, amountPaid);
this.reason = reason;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
@Override
public String toString() {
return super.toString()+ " "+reason;
}
}
class for NonTaxExemptCustomer :
public class NonTaxExemptCustomer extends Customer{
private double taxPer;
public NonTaxExemptCustomer(String custName, int custId, long custPhoneNum, double amountPaid, double taxPer) {
super(custName, custId, custPhoneNum, amountPaid);
this.taxPer = taxPer;
}
public double getTaxPer() {
return taxPer;
}
public void setTaxPer(double taxPer) {
this.taxPer = taxPer;
}
@Override
public String toString() {
double taxPaid = (getAmountPaid()*taxPer)/100;
String res = String.format("%.2f", taxPaid);
return super.toString()+" " + taxPer+" $"+res;
}
}
main class Y2Y3 :
public class Y2Y3 {
public static void main(String args[]) {
Customer taxExempt1 = new TaxExemptCustomer("AHS", 3, 19734508345L, 255.50, "Non-profit");
Customer taxExempt2 = new TaxExemptCustomer("PIO", 4, 18002708855L, 500, "Non-profit");
Customer nonTaxExempt1 = new NonTaxExemptCustomer("PWC", 1, 18002500830L, 750, 0.08d);
Customer nonTaxExempt2 = new NonTaxExemptCustomer("E&Y", 2, 18003409845L, 970, 0.08);
System.out.println("Non tax exempt customers:");
System.out.println(nonTaxExempt1);
System.out.println(nonTaxExempt2);
System.out.println("Tax exempt customers:");
System.out.println(taxExempt1);
System.out.println(taxExempt2);
}
}