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

Write a program to create and use gift cards. Write a separate class to represen

ID: 3875857 • Letter: W

Question

Write a program to create and use gift cards.

Write a separate class to represent a gift card, and name it appropriately.

A gift card can only be given a value when it is instantiated.

A gift card cannot have a negative balance.

A gift card has a debit method with a double parameter, which should reduce the current balance of the gift card by the amount of the parameter or the total amount of the current balance, whichever is less, and return the amount that the balance was reduced.

A gift card has a getBalance method which will return the current balance of the gift card.

Write a driver program to input an amount from the user and instantiate a gift card for that amount, then input amounts of purchases until the balance of the gift card is $0. For the last purchase, the remaining amount due should be reported.

Explanation / Answer

Following is the java program for this question:

//class GiftCards

public class GiftCards {
double amt;
public GiftCards(double value) { //contructor to instantiate the gift card
  amt = value;
}


public double debit(double val) {  //debit method
  double num = 0.0;
  if(val>amt) {
   num = amt;
   amt = 0;
  }
  else {
   amt -= val;
   num = val;
  }
  return num;
}


public double getBalance() {   // getbalance method
  return amt;
}
}

//class Driver

import java.util.Scanner;

public class Driver {       //driver class

public static void main(String[] args) {  //main method for driver
  System.out.println("Enter amount of gift card: ");
  Scanner sc = new Scanner(System.in);
  double amount = sc.nextDouble();
  GiftCards gc = new GiftCards(amount);
  while (gc.amt>0) {
   System.out.println("Enter amount of purchase: ");
   double purchaseAmount = sc.nextDouble();
   double remaining = purchaseAmount-gc.debit(purchaseAmount);
   if(remaining>0) {
    System.out.println("The amount due is: "+remaining);
   }
  }
  sc.close();
}

}

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