Question
Question 3: Polymorphism could play a role in the payment process. 1)The abstract method purchase is established in the Sale class, ensuring that child classes
Question 3: Polymorphism could play a role in the payment process.
1)The abstract method purchase is established in the Sale class, ensuring that child classes will implement it. That way, each class can have a definition of purchase that makes sense for it, as opposed to the computeTax method, which is implemented in Sale and used by all child classes as is. A program that manages the sales transactions as they occur could then conduct the transaction by making a polymorphic call to purchase no matter what the transaction type is.
2)So, in the driver class, please test purchase implementation for Cash, CreditCard and GiftCard classes.
import java.util.Date;
public class Transaction {
private int transactionId;
private double amount;
private Date date;
public Transaction(int transactionId, double amount, Date date) {
this.transactionId = transactionId;
this.amount = amount;
this.date = date;
}
public void generateReceipt() {
System.out.println("Receipt generated for transaction ID: " + transactionId);
}
public void processTransaction() {
System.out.println("Transaction processed with ID: " + transactionId);
}
// Getters and setters for properties can be added as needed.
}
public class Sale {
private double amount;
public Sale(double amount) {
this.amount = amount;
}
public void purchase() {
System.out.println("Purchased items worth: $" + amount);
}
public double computeTax() {
return amount * 0.10; // Assuming a 10% tax rate
}
// Getters and setters for properties can be added as needed.
}
public class Return {
private int originalTransaction;
private String returnReason;
public Return(int originalTransaction, String returnReason) {
this.originalTransaction = originalTransaction;
this.returnReason = returnReason;
}
public void processReturn() {
System.out.println("Processed return for transaction ID: " + originalTransaction
+ ". Reason: " + returnReason);
}
// Getters and setters for properties can be added as needed.
}
public abstract class PaymentMethod {
// Any common properties or methods for all payment methods can be added here.
}
public class Cash extends PaymentMethod {
private double receivedAmount;
public Cash(double receivedAmount) {
this.receivedAmount = receivedAmount;
}
public void processCashPayment() {
System.out.println("Processed cash payment of: $" + receivedAmount);
}
public double calculateChange(double totalAmount) {
return receivedAmount - totalAmount;
}
// Getters and setters can be added as needed.
}
public class CreditCard extends PaymentMethod {
private long cardNumber;
private String cardType;
public CreditCard(long cardNumber, String cardType) {
this.cardNumber = cardNumber;
this.cardType = cardType;
}
public void processCCPayment() {
System.out.println("Processed credit card payment using card: " + cardNumber);
}
// Getters and setters can be added as needed.
}
public class GiftCard extends PaymentMethod {
private String giftCardCode;
private double balance;
public GiftCard(String giftCardCode, double balance) {
this.giftCardCode = giftCardCode;
this.balance = balance;
}
public void processGCPayment(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Processed gift card payment of: $" + amount);
} else {
System.out.println("Insufficient gift card balance.");
}
}
public double checkBalance() {
return balance;
}
// Getters and setters can be added as needed.
}
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Create a Transaction instance
Transaction transaction = new Transaction(1, 100.0, new Date());
transaction.generateReceipt();
transaction.processTransaction();
// Create a Sale instance
Sale sale = new Sale(50.0);
sale.purchase();
System.out.println("Tax on sale: $" + sale.computeTax());
// Create a Return instance
Return returnItem = new Return(1, "Item was damaged");
returnItem.processReturn();
// Create a Cash payment instance
Cash cash = new Cash(120.0);
cash.processCashPayment();
System.out.println("Change from cash payment: $" + cash.calculateChange(100.0));
// Create a CreditCard payment instance
CreditCard creditCard = new CreditCard(1234567890123456L, "Visa");
creditCard.processCCPayment();
// Create a GiftCard payment instance
GiftCard giftCard = new GiftCard("GIFT1234", 50.0);
System.out.println("Gift card balance: $" + giftCard.checkBalance());
giftCard.processGCPayment(40.0);
System.out.println("Gift card balance after payment: $" + giftCard.checkBalance());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started