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

The following interface defines a method charge which takes some amount to be ap

ID: 3834547 • Letter: T

Question

The following interface defines a method charge which takes some amount to be applied to a payment method. this method should return true when the charge is successful, and false otherwise. if the charge is successful, funds should be removed from an associated source of money (or balance). If the charge is unsuccessful, no money should be removed.

public interface PaymentMethod { public boolean charge(double amount); }

Complete the implementation bellow representing a pre-paid gift card. All that is left is to implement the charge method. A gift card change should succeed only when it's current balance is at least as much as the amount being charged. A successful charge should decrease the gift card'd current balance.

public class GiftCard implements PaymentMethod{

private double balance;

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

public getRemainingBalance(double balance) { return balance; }

}

Write a secounf implementation of PaymentMethod called CreditCard (provide a complete class definition). It should represent a credit card that has a limit (supplied at construction) and a balance (the amount owed). A credit card charge should succeed only if doing so would not make the balance exceed the limit.

public class CreditCard{

}

Explanation / Answer

public class GiftCard implements PaymentMethod{
private double balance;
public GiftCard(double balance){
   this.balance = balance;
}
public double getRemainingBalance(){
   return balance;
}
   @Override
   public boolean charge(double amount) {
       if(amount>=balance){
           balance -= amount;
           return true;
       }
       return false;
   }
}

public class CreditCard implements PaymentMethod{
   private double limit;
   private double balance;
   public CreditCard(double limit) {
       this.setLimit(limit);
       balance =0;
   }
   public double getLimit() {
       return limit;
   }
   public void setLimit(double limit) {
       this.limit = limit;
   }
   public double getBalance() {
       return balance;
   }
   public void setBalance(double balance) {
       this.balance = balance;
   }
  
   @Override
   public boolean charge(double amount) {
       if((balance+amount)<=limit){
           balance += amount;
           return true;
       }
       return false;
   }
}