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

In this lab, you will be making a small class hierarchy for payment methods. A p

ID: 3855859 • Letter: I

Question

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

GiftCard "IS A" PaymentMethod

DebitCard "IS A" PaymentMethod

DebitCard "HAS A" Account

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


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

}

//--------------------------------------------------------------------------------------------


public class GiftCard extends PaymentMethod {

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

   @Override
   boolean Charge(double amount) {
       // TODO Auto-generated method stub
       if(amount>balance)
       return false;
       else{
           balance-=amount;
           return true;
       }
   }

  
}

//---------------------------------------------------------------------------------------------------


public class DebitCard extends PaymentMethod {
  
   Account account;

   public DebitCard(Account account) {
       super();
       this.account = account;
   }
  
   public Account getAccount(){
       return this.account;
   }
  
   @Override
   boolean Charge(double amount) {
       // TODO Auto-generated method stub
       if(amount>account.getBalance())
           return false;
       else{
           account.withdraw(amount);
           return true;
       }
   }

}

//----------------------------------------------------------------------------------------------------------


public class Account {
  
   double balance=0;
  
   public double getBalance(){
       return this.balance;
   }
  
   public boolean canWithDraw(double amount){
       if(amount<=balance)
           return true;
       else
           return false;
   }
  
   public void withdraw(double amount){
       if(amount<=balance){
           balance-=amount;
       }
       else
           System.out.println("Low balance");
   }
  
   public void deposit(double amount){
       balance+=amount;
   }

  
}

//-------------------------------------------------------------------------------------------------


public class Test {
   public static void main(String[] args) {
      
       GiftCard g = new GiftCard(2000);
      
       if(g.Charge(1000)){
           System.out.println("Successful to charge gift card $1000");
       }
       else
           System.out.println("$1000 charge declined on gift card");
      
       if(g.Charge(2000)){
           System.out.println("Successful to charge gift card $2000");
       }
       else
           System.out.println("$2000 charge declined on gift card");
      
       Account a = new Account();
       a.deposit(5000);
       DebitCard d = new DebitCard(a);
      
       if(d.Charge(6000)){
           System.out.println("Successful to charge debit card $6000");
       }
       else
           System.out.println("$6000 charge declined on debit card");
      
       if(d.Charge(2000)){
           System.out.println("Successful to charge debit card $2000. New "
                   + "balance= $"+d.getAccount().getBalance());
       }
       else
           System.out.println("$2000 charge declined on debit card");
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote