In: Computer Science
In JAVA please...
Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails().
Define a class named CashPayment that is derived from Payment. This class will have an additional instance variable of type String (a non-static member variable) that specifies a type of currency, such as "dollar", "euro", "peso", "yen", etc. This class should override the paymentDetails method to indicate that the payment is in cash, and the type of currency used. Include appropriate constructor(s) and accessor/mutator methods for all instance variablesand. Override toString() with contents of cash payment details (should still call paymentDetails()).
Define a class named CreditCardPayment that is derived from Payment. This class should contain instance variables (member variables) for the name on the card, expiration date, and credit card number. Include appropriate constructor(s) and accessor/mutator methods for all instance variables. Finally, redefine thepaymentDetails method to include all credit card information in the printout. Override toString() method with contents of credit payment details (should still call paymentDetails()).
Create a main method within Payment that creates at least two CashPayment and two CreditCardPayment objects with different values and payment amounts. Call toString() methods from each object to print the details of each payment.
public class Payment {
double paymentAmount;
public Payment(double paymentAmount) {
super();
this.paymentAmount =
paymentAmount;
}
public double getPaymentAmount() {
return paymentAmount;
}
public void setPaymentAmount(double paymentAmount)
{
this.paymentAmount =
paymentAmount;
}
public String paymentDetails()
{
return "The Payment Amount is
"+paymentAmount+"\n";
}
public String toString() {
return paymentDetails();
}
public static void main(String[] args)
{
CashPayment cash1 = new
CashPayment(250.00,"dollar");
CashPayment cash2 = new
CashPayment(520.00,"peso");
CreditCardPayment credit1 = new
CreditCardPayment(125.25,"Peter","May-2025","2541 2568 1258
4568");
CreditCardPayment credit2 = new
CreditCardPayment(285.00,"Stark","Sep-2030","5684 2256 1445
2589");
System.out.println(cash1.toString());
System.out.println(cash2.toString());
System.out.println(credit1.toString());
System.out.println(credit2.toString());
}
}
class CashPayment extends Payment{
String currency;
public CashPayment(double paymentAmount, String
currency) {
super(paymentAmount);
this.currency = currency;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String paymentDetails()
{
return "The Payment Amount is
"+paymentAmount+" "+currency+"\n";
}
public String toString() {
return paymentDetails();
}
}
class CreditCardPayment extends Payment{
String nameOnCard;
String expDate;
String cardNumber;
public CreditCardPayment(double paymentAmount, String
nameOnCard, String expDate, String cardNumber) {
super(paymentAmount);
this.nameOnCard = nameOnCard;
this.expDate = expDate;
this.cardNumber = cardNumber;
}
public String getNameOnCard() {
return nameOnCard;
}
public void setNameOnCard(String nameOnCard)
{
this.nameOnCard = nameOnCard;
}
public String getExpDate() {
return expDate;
}
public void setExpDate(String expDate) {
this.expDate = expDate;
}
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber)
{
this.cardNumber = cardNumber;
}
public String paymentDetails()
{
return "The Payment Amount is
"+paymentAmount+"\nName on the card: "+nameOnCard+"\nExpiry date:
"+expDate+"\nCredit card number: "+cardNumber+"\n";
}
public String toString() {
return paymentDetails();
}
}
/*Output:
The Payment Amount is 250.0 dollar
The Payment Amount is 520.0 peso
The Payment Amount is 125.25
Name on the card: Peter
Expiry date: May-2025
Credit card number: 2541 2568 1258 4568
The Payment Amount is 285.0
Name on the card: Stark
Expiry date: Sep-2030
Credit card number: 5684 2256 1445 2589
*/