Write a CashRegister class that can be used with the RetailI

Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6. 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 the 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.

cashregister.java

public class cashregister
{
   private final double SALESTAX = .06;
   private retailitem item;
   private int numberofitems;
  
  
  
  
  
   public cashregister(retailitem item, int numberofitems)
   {
       this.item = item;
       this.numberofitems = numberofitems;
       item = retailitem.copy();
   }
  
   public double getSubtotal()
   {
      return numberofitems * item.getPrice();
   }
  
   public double getTax()
   {
          return SALESTAX * getSubtotal();
   }
  
   public double getTotal()
   {
       return getSubtotal() + getTax();
   }
  
}


retailitem.java

public class retailitem
{
    private String description;
    private int unitsOnHand;
    private double price;

    public retailitem(String description, int unitsOnHand, double price)
    {
        this.description = description;
        this.unitsOnHand = unitsOnHand;
        this.price = price;
    }

    public String getDescription()
    {
        return description;
    }

    public int getUnits()
    {
        return unitsOnHand;
    }

    public double getPrice()
    {
        return price;
    }
  
    public retailitem copy()
    {
       retailitem copy = new retailitem(description,unitsOnHand,price);
       return copy;  
   }
  
    public String toString()
    {
       return \"Item is\" + description + \"There are\"
       + unitsOnHand + \" \" + \"left in the store\" +
       \"It currently is selling for\" + \" \" + price;
   }


}


I keep on getting an error that says
\"non-static method copy() cannot be referenced from a static context\" in the cashregister.java SC

Solution

//*** Hello this is the solution of your problem and the error that you get is just beacause of using non-static method to copy. In your CashRegister Class constructor you are using item = retailitem.copy();, You are using the class directly without instatiate that\'s why you are getting the error.

public class HeadClass{

public static class retailitem
{
private String description;
private int unitsOnHand;
private double price;
public retailitem()
{
  
}
public retailitem(String description, int unitsOnHand, double price)
{
this.description = description;
this.unitsOnHand = unitsOnHand;
this.price = price;
}
public String getDescription()
{
return description;
}
public int getUnits()
{
return unitsOnHand;
}
public double getPrice()
{
return price;
}
  
public retailitem copy()
{
retailitem copy = new retailitem(description,unitsOnHand,price);
return copy;
}
  
public String toString()
{
return \"Item is\" + description + \"There are\"
+ unitsOnHand + \" \" + \"left in the store\" +
\"It currently is selling for\" + \" \" + price;
}

}
public static class cashregister
{
private final double SALESTAX = .06;
private retailitem item;
private int numberofitems;
public cashregister(retailitem item, int numberofitems)
{
retailitem Obj=new retailitem();
this.item = item;
this.numberofitems = numberofitems;
item = Obj.copy();
}
  
public double getSubtotal()
{
return numberofitems * item.getPrice();
}
  
public double getTax()
{
return SALESTAX * getSubtotal();
}
  
public double getTotal()
{
return getSubtotal() + getTax();
}
  
}

public static void main(String []args){
retailitem retailObj=new retailitem(\"Eggs\",10,5.25);
cashregister tempObj=new cashregister(retailObj,4);
System.out.println(\"Subtotal Of Items=\"+tempObj.getSubtotal());
System.out.println(\"Tax On Item=\"+tempObj.getTax());
System.out.println(\"Total Amount=\"+tempObj.getTotal());
}
}

Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6. The CashRegister class should simulate the sale of a retail i
Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6. The CashRegister class should simulate the sale of a retail i
Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6. The CashRegister class should simulate the sale of a retail i
Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6. The CashRegister class should simulate the sale of a retail i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site