CashRegister Class JAVA Write a CashRegister class that can

CashRegister Class - JAVA

Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6’s Programming Challenge 4. The CashRegister class should simulate the sale of a retail item. It should have a constructor that accepts a RetailItem object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. In addition, the class should have the following methods:

• The getSubtotal method should return the subtotal of the sale, which is the quantity multiplied by the price. This method must get the price from the RetailItem object that was passed as an argument to the constructor.

• The getTax method should return the amount of sales tax on the purchase. The sales tax rate is 6 percent of a retail sale.

• The getTotal method should return the total of the sale, which is the subtotal plus the sales tax.

Demonstrate the class in a program that asks the user for the quantity of items being purchased, and then displays the sale’s subtotal, amount of sales tax, and total.

The output needs to be this:

Item being purchased: Candy bar

Units on hand: 50

Price: $0.75

How many candy bars are you buying? 100

Sorry, we only have 50 in stock.

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

Item being purchased: Candy bar

Units on hand: 50

Price: $0.75

How many candy bars are you buying? 10

Subtotal: $7.50

Sales tax: $0.45

Total: $7.95

Units on hand: 40

It needs to have two classes and one demo(tester)

Thank you in advance

Solution

public class RetailItem {

   String itemName;
   int unitsOnHand;
   double price;

   /**
   * @param itemName
   * @param unitsOnHand
   * @param price
   */
   public RetailItem(String itemName, int unitsOnHand, double price) {
       this.itemName = itemName;
       this.unitsOnHand = unitsOnHand;
       this.price = price;
   }

   /**
   * @return the itemName
   */
   public String getItemName() {
       return itemName;
   }

   /**
   * @param itemName
   * the itemName to set
   */
   public void setItemName(String itemName) {
       this.itemName = itemName;
   }

   /**
   * @return the unitsOnHand
   */
   public int getUnitsOnHand() {
       return unitsOnHand;
   }

   /**
   * @param unitsOnHand
   * the unitsOnHand to set
   */
   public void setUnitsOnHand(int unitsOnHand) {
       this.unitsOnHand = unitsOnHand;
   }

   /**
   * @return the price
   */
   public double getPrice() {
       return price;
   }

   /**
   * @param price
   * the price to set
   */
   public void setPrice(double price) {
       this.price = price;
   }

}

public class CashRegister {

   RetailItem item;
   int noOfUnitsBuying;

   /**
   * @param item
   * @param noOfUnitsBuying
   */
   public CashRegister(RetailItem item, int noOfUnitsBuying) {
       this.item = item;
       this.noOfUnitsBuying = noOfUnitsBuying;
   }

   /**
   * @return the item
   */
   public RetailItem getItem() {
       return item;
   }

   /**
   * @param item
   * the item to set
   */
   public void setItem(RetailItem item) {
       this.item = item;
   }

   /**
   * @return the noOfUnitsBuying
   */
   public int getNoOfUnitsBuying() {
       return noOfUnitsBuying;
   }

   /**
   * @param noOfUnitsBuying
   * the noOfUnitsBuying to set
   */
   public void setNoOfUnitsBuying(int noOfUnitsBuying) {
       this.noOfUnitsBuying = noOfUnitsBuying;
   }

   public double getSubtotal() {

       return item.getPrice() * noOfUnitsBuying;

   }

   public double getTax() {

       return getSubtotal() * 0.06;

   }

   public double getTotal() {

       return getSubtotal() + getTax();
   }
}

import java.util.Scanner;

public class demo {

   public static void main(String[] args) {

       Scanner scanner = null;
       try {
           String itemName;
           int unitsOnHand;
           double price;
           RetailItem item;
           int noOfUnitsBuying;
           scanner = new Scanner(System.in);
           System.out.print(\"Item being purchased:\");
           itemName = scanner.nextLine();
           System.out.print(\"Units on hand: \");
           unitsOnHand = scanner.nextInt();
           System.out.print(\"Price: $\");
           price = scanner.nextDouble();

           System.out.print(\"How many candy bars are you buying? \");
           noOfUnitsBuying = scanner.nextInt();
           item = new RetailItem(itemName, unitsOnHand, price);
           if (noOfUnitsBuying <= item.getUnitsOnHand()) {
               CashRegister cashRegister = new CashRegister(item,
                       noOfUnitsBuying);
               System.out.println(\"Subtotal: $\" + cashRegister.getSubtotal());
               System.out.println(\"Sales tax: $\" + cashRegister.getTax());
               System.out.println(\"Total: $\" + cashRegister.getTotal());
               item.setUnitsOnHand(unitsOnHand - noOfUnitsBuying);
               System.out.println(\"Units on hand: \" + item.getUnitsOnHand());

           } else {

               System.out.println(\"Sorry, we only have \"
                       + item.getUnitsOnHand() + \" in stock.\");

           }

       } catch (Exception e) {
           // TODO: handle exception
       }
   }
}

OUTPUT:

Item being purchased:Candy Bar
Units on hand: 50
Price: $0.75
How many candy bars are you buying? 100
Sorry, we only have 50 in stock.

Item being purchased:Candy Bar
Units on hand: 50
Price: $0.75
How many candy bars are you buying? 10
Subtotal: $7.5
Sales tax: $0.44999999999999996
Total: $7.95
Units on hand: 40

CashRegister Class - JAVA Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6’s Programming Challenge 4. The CashR
CashRegister Class - JAVA Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6’s Programming Challenge 4. The CashR
CashRegister Class - JAVA Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6’s Programming Challenge 4. The CashR
CashRegister Class - JAVA Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6’s Programming Challenge 4. The CashR

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site