Write three classes to represent a payment. A payment class represents a payment
ID: 3589422 • Letter: W
Question
Write three classes to represent a payment.
A payment class represents a payment by the amount of money (as a double) and the date of the payment.
A cash payment represents a payment by the amount and date.
A credit card payment represents a payment by the amount, date, and also the name and credit card number.
In each class, include (or use inherited versions of):
instance data variables
getters and setters
a toString method
In the credit card payment class, include an equals method. Two payments are logically equivalent if they have the same amount, date, name, and credit card number.
Explanation / Answer
class Payment //base class
{
private double amount;
private String date;
public Payment(double amount,String date) //argument constructor
{
this.amount = amount;
this.date = date;
}
//get methods and set methods
public double getAmount()
{
return amount;
}
public String getDate()
{
return date;
}
public void setAmount(double amount)
{
this.amount = amount;
}
public void setDate()
{
this.date = date;
}
//overridetoString method
public String toString()
{
return " Payment : Amount : "+amount +" Date : "+date;
}
}
class CashPayment extends Payment //derived class
{
public CashPayment(double amount,String date)
{
super(amount,date); //call to base class constructor
}
public String toString()
{
return " Cash Payment : Amount : "+ getAmount() +" Date : "+ getDate();
}
}
class CreditCardPayment extends Payment //derived class
{
private String name;
private int ccNumber;
public CreditCardPayment(double amount,String date, String name,int ccNumber)
{
super(amount,date); //call to base class constructor
this.name = name;
this.ccNumber = ccNumber;
}
public String toString()
{
return " Credit Card Payment : Amount : "+ getAmount() +" Date : "+ getDate() +" Name : "+ name + " Credit Card Number : "+ ccNumber;
}
//equals method to compare two credit card payments
public boolean equals(CreditCardPayment ccp)
{
if(this.getAmount() == ccp.getAmount() && this.getDate() == ccp.getDate() && this.name == ccp.name && this.ccNumber == ccp.ccNumber)
return true;
else
return false;
}
}
class TestPayment
{
public static void main (String[] args)
{
CreditCardPayment ccp1 = new CreditCardPayment(456.72,"01/12/2016","John Jones",7676878);
CreditCardPayment ccp2 = new CreditCardPayment(456.72,"01/12/2016","John Jones",7676878);
if(ccp1.equals(ccp2))
System.out.println(" Both Credit card Payments are equal");
else
System.out.println(" The two credit card payments are not equal");
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.