Reimplement the CashRegister class so that it keeps track of the price of each a
ID: 3708821 • Letter: R
Question
Reimplement the CashRegister class so that it keeps track of the price of each added item in an ArrayList<Double> for price and an ArrayList<Boolean> for taxable. Remove the itemCount and totalPrice instance variables. Reimplement the clear(), addItem(), getTotal(), and getCount() methods. Add a method displayAll() that displays the prices of all items in the current sale.
For your reference, the original CashRegister class looks like this
/**
A simulated cash register that tracks the item count and
the total amount due.
*/
public class CashRegister
{
private int itemCount;
private double totalPrice;
private double taxableTotal;
private double taxRate;
/**
Constructs a cash register with cleared item count and total.
@param aTaxRate the tax rate to use with this cash register.
*/
public CashRegister(double aTaxRate)
{
itemCount = 0;
totalPrice = 0;
taxableTotal = 0;
taxRate = aTaxRate;
}
/**
Adds an item to this cash register.
@param price the price of this item
@param taxable true if this item is taxable
*/
public void addItem(double price, boolean taxable)
{
itemCount++;
totalPrice = totalPrice + price;
if (taxable)
{
taxableTotal = taxableTotal + price;
}
}
/**
Gets the price of all items in the current sale.
@return the total amount
*/
public double getTotal()
{
return totalPrice + taxableTotal * taxRate / 100;
}
/**
Gets the number of items in the current sale.
@return the item count
*/
public int getCount()
{
return itemCount;
}
/**
Clears the item count and the total.
*/
public void clear()
{
itemCount = 0;
totalPrice = 0;
taxableTotal = 0;
}
}
Explanation / Answer
import java.util.ArrayList; /** A simulated cash register that tracks the item count and the total amount due. */ public class CashRegister { private ArrayList prices; private ArrayList taxables; private double taxRate; /** Constructs a cash register with cleared item count and total. @param aTaxRate the tax rate to use with this cash register. */ public CashRegister(double aTaxRate) { prices = new ArrayList(); taxables = new ArrayList(); taxRate = aTaxRate; } /** Adds an item to this cash register. @param price the price of this item @param taxable true if this item is taxable */ public void addItem(double price, boolean taxable) { prices.add(price); taxables.add(taxable); } /** Gets the price of all items in the current sale. @return the total amount */ public double getTotal() { double totalPrice = 0; double taxableTotal = 0; for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.