Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Lab8_Driver { public static void main(String[] args) { // test gift

ID: 3857393 • Letter: P

Question

public class Lab8_Driver {

   public static void main(String[] args) {

       // test gift card
       System.out.println("Test GiftCard Class ");
       GiftCard giftCard = new GiftCard(500);
       System.out.println("Balance of Gift Card is : $" + giftCard.getBalance());
       System.out.print("The gift card has been charged about $600.00? ");
       if (giftCard.charge(600)) {
           System.out.println("successful. ");

       } else
           System.out.println("unsuccessful. ");

       System.out.print("The gift card has been charged about $200.00? ");
       if (giftCard.charge(200)) {
           System.out.println("successful. ");

       } else
           System.out.println("unsuccessful. ");

       System.out.println("The remaining balance of this Gift Card is $" + giftCard.getBalance());

       System.out.print(" ");

       // test account
       System.out.println("Test Account Class");
       Account a = new Account();
       System.out.println("Balance of new created account is : $" + a.getBalance());
       System.out.print("Withdraw $100 from this account was ");
       if (a.canWithdraw(100)) {
           System.out.println("successful. ");

       } else
           System.out.println("unsuccessful. ");

       System.out.println("The new balance is $" + a.getBalance());
       a.deposit(200);
       System.out.println("Deposit $200 to this account is done. ");
       System.out.println("The new balance is $" + a.getBalance());
       a.deposit(500);
       System.out.println("Deposit $500 to this account is done. ");
       System.out.println("The new balance is $" + a.getBalance());

       // test DebitCard
       System.out.println(" ");
       System.out.println("Test DebitCard Class");
       DebitCard d = new DebitCard(a);
       System.out.println("The new debit card is issued for perivous account. ");
       System.out.println("New debitcard balance is : $" + d.getAccount().getBalance());
       System.out.print("Withdraw $100 from this account was ");
       if (d.charge(100)) {
           System.out.println("successful. ");

       } else
           System.out.println("unsuccessful. ");
       System.out.println("Debitcard balance is now : $" + d.getAccount().getBalance());
       System.out.print("Withdraw $9000 from this account was ");
       if (d.charge(9000)) {
           System.out.println("successful. ");

       } else
           System.out.println("unsuccessful. ");
       System.out.println("Debitcard balance is now : $" + d.getAccount().getBalance());
       System.out.println("Deposit $800 to this account was successful");
       d.getAccount().deposit(800);
       System.out.println("Debitcard balance is now : $" + d.getAccount().getBalance());

   }

}

In this lab, you will be making a small class hierarchy for payment methods. A payment method is responsible for charging some amount to an account or card. A charge can either be accepted (in which case debt is added or funds are removed from an associated object), or be declined (in which case the associated object should not be altered). The charge method should return true if the charge is accepted and false otherwise. Again, no state should change in any object if the charge is not successful Before starting implementation, identify the type of object relationship in the diagram below (there are three). Categorize the relationship into inheritance, aggregation, and composition. Explain your choice to your lab instructor before you are checked off The gift card has a pre-loaded amount supplied by the constructor. A gift card can be charged as long as its balance remains non-negative. A debit card does not have a pre-loaded amount, but is linked to a bank account. The debit card can be charged as long as the bank account has a sufficient amount for the charge Once again you are responsible for writing a small test driver to show that your code works in some common cases. You should write the driver as a small scenario that a user is likely to enocunter. You do not need to utilize user input for the driver.

Explanation / Answer

Please find the required program and output below: Please find the comments against each line for the description:

abstract class PaymentMethod {
  
   abstract public boolean charge(double amount);

}

class GiftCard extends PaymentMethod{
  
   double balance;

   public GiftCard(double balance) {
       this.balance = balance;
   }

   public double getBalance() {
       return balance;
   }
  
   @Override
   public boolean charge(double amount) {
       if(balance >= amount){
           balance = balance - amount;
           return true;
       }else {
           return false;
       }          
   }
}

class Account {
  
   double balance = 0;
  
   public double getBalance() {
       return balance;
   }
  
   public boolean canWithdraw(double amount) {
       return (balance >= amount);
   }
  
   public void withdraw(double amount) {
       if(canWithdraw(amount)){
           balance = balance - amount;
           System.out.println("Amount withdrawn.");
       }          
   }

   public void deposit(double amount) {
       balance = balance + amount;
   }
}

class DebitCard extends PaymentMethod{
  
   Account account;

   public DebitCard(Account account) {
       this.account = account;
   }

   public Account getAccount() {
       return account;
   }
  
   @Override
   public boolean charge(double amount) {
       if(account.canWithdraw(amount)){
           account.deposit(amount);
           return true;
       }else {
           return false;
       }          
   }
}


public class Lab8_Driver {
public static void main(String[] args) {
// test gift card
System.out.println("Test GiftCard Class ");
GiftCard giftCard = new GiftCard(500);
System.out.println("Balance of Gift Card is : $" + giftCard.getBalance());
System.out.print("The gift card has been charged about $600.00? ");
if (giftCard.charge(600)) {
System.out.println("successful. ");
} else
System.out.println("unsuccessful. ");
System.out.print("The gift card has been charged about $200.00? ");
if (giftCard.charge(200)) {
System.out.println("successful. ");
} else
System.out.println("unsuccessful. ");
System.out.println("The remaining balance of this Gift Card is $" + giftCard.getBalance());
System.out.print(" ");
// test account
System.out.println("Test Account Class");
Account a = new Account();
System.out.println("Balance of new created account is : $" + a.getBalance());
System.out.print("Withdraw $100 from this account was ");
if (a.canWithdraw(100)) {
System.out.println("successful. ");
} else
System.out.println("unsuccessful. ");
System.out.println("The new balance is $" + a.getBalance());
a.deposit(200);
System.out.println("Deposit $200 to this account is done. ");
System.out.println("The new balance is $" + a.getBalance());
a.deposit(500);
System.out.println("Deposit $500 to this account is done. ");
System.out.println("The new balance is $" + a.getBalance());
// test DebitCard
System.out.println(" ");
System.out.println("Test DebitCard Class");
DebitCard d = new DebitCard(a);
System.out.println("The new debit card is issued for perivous account. ");
System.out.println("New debitcard balance is : $" + d.getAccount().getBalance());
System.out.print("Withdraw $100 from this account was ");
if (d.charge(100)) {
System.out.println("successful. ");
} else
System.out.println("unsuccessful. ");
System.out.println("Debitcard balance is now : $" + d.getAccount().getBalance());
System.out.print("Withdraw $9000 from this account was ");
if (d.charge(9000)) {
System.out.println("successful. ");
} else
System.out.println("unsuccessful. ");
System.out.println("Debitcard balance is now : $" + d.getAccount().getBalance());
System.out.println("Deposit $800 to this account was successful");
d.getAccount().deposit(800);
System.out.println("Debitcard balance is now : $" + d.getAccount().getBalance());
}
}

------------------------------------------------------------------------------

OUTPUT :

Test GiftCard Class
Balance of Gift Card is : $500.0
The gift card has been charged about $600.00? unsuccessful.
The gift card has been charged about $200.00? successful.
The remaining balance of this Gift Card is $300.0


Test Account Class
Balance of new created account is : $0.0
Withdraw $100 from this account was unsuccessful.
The new balance is $0.0
Deposit $200 to this account is done.
The new balance is $200.0
Deposit $500 to this account is done.
The new balance is $700.0

Test DebitCard Class
The new debit card is issued for perivous account.
New debitcard balance is : $700.0
Withdraw $100 from this account was successful.
Debitcard balance is now : $800.0
Withdraw $9000 from this account was unsuccessful.
Debitcard balance is now : $800.0
Deposit $800 to this account was successful
Debitcard balance is now : $1600.0