Payment Problem (Java language): Define a class called Payment which contains an
ID: 3588814 • Letter: P
Question
Payment Problem (Java language):
Define a class called Payment which contains an instance variable of type double that stores the amount of a payment. Include appropriate getter/setter methods in your class. Include a method named paymentDetails which outputs an English sentence describing the payment.
Next define a class named CashPayment which is derived from Payment. The class should redefine the paymentDetails method to indicate that the payment is cash. Include necessary constructors for this class.
In addition to this, define a third class called CreditCardPayment that is derived from Payment. This class should contain instance variables for the: Cardholder’s Name Expiration Date Credit Card Number
Lastly modify the paymentDetails method on this class to include all credit card information in the printout. Include a main method in your submission (either in one of the three classes or in a separate class) which demonstrates two instances of CashPayment and at least two instances of CreditCardPayment and the different values of paymentDetails for each.
Explanation / Answer
class Payment //base class
{
private double amount;
public Payment(double amount) //constructor
{
this.amount = amount;
}
//set and get methods
public double getAmount()
{
return amount;
}
public void setAmount(double amount)
{
this.amount = amount;
}
public void paymentDetails()
{
System.out.println(" Payment of $"+amount+ " is done.");
}
}
class CashPayment extends Payment
{
public CashPayment(double amount)
{
super(amount); // call to base class constructor
}
public void paymentDetails() //override base class constrcutor
{
System.out.println(" Payment of $"+getAmount()+ " is done by cash.");
}
}
class CreditCardPayment extends Payment
{
private String cardHolderName;
private String expirationDate;
private int cardNumber;
public CreditCardPayment(double amount,String cardHolderName,String expirationDate,int cardNumber)
{
super(amount); //call to base class constructor
this.cardHolderName = cardHolderName;
this.expirationDate = expirationDate;
this.cardNumber = cardNumber;
}
public void paymentDetails() // override base class constructor
{
System.out.println(" Payment of $"+ getAmount()+ " is done by card number "+cardNumber +" to be expired on "+expirationDate + " for holder "+cardHolderName);
}
}
class TestPayment
{
public static void main (String[] args)
{
CashPayment cp1 = new CashPayment(459.45);
cp1.paymentDetails();
CashPayment cp2 = new CashPayment(1003.56);
cp2.paymentDetails();
CreditCardPayment ccp1 = new CreditCardPayment(768.66,"John Jones","08/12/2018",8575757);
ccp1.paymentDetails();
CreditCardPayment ccp2 = new CreditCardPayment(443.34,"Marry Charles","17/09/2020",4546463);
ccp2.paymentDetails();
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.