Question
I need to change my java test class, CashRegisterTester to function with the class CashRegister Here is my code for the CashRegister class import java.util.ArrayList;
I need to change my java test class, CashRegisterTester to function with the class CashRegister
Here is my code for the CashRegister class
import java.util.ArrayList;
/**
*
* A simulated cash register that tracks the item count and
*
* the total amount due.
*
*/
public class CashRegister
{
private static double totalSales = 0;
private static int totalSalesCount = 0;
private ArrayList
private ArrayList
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);
totalSalesCount++;
totalSales += (price);
double taxableTotal = 0;
if (taxable) {
taxableTotal = price;
totalSales = totalSales + (taxableTotal * taxRate / 100);
}
}
/**
*
* 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; i < prices.size(); ++i) {
totalPrice += prices.get(i);
if (taxables.get(i)) {
taxableTotal += prices.get(i);
}
}
return totalPrice + taxableTotal * taxRate / 100;
}
/**
*
* Gets the number of items in the current sale.
*
* @return the item count
*
*/
public int getCount()
{
return prices.size();
}
/**
*
* Clears the item count and the total.
*
*/
public void clear()
{
prices.clear();
taxables.clear();
}
/**
* Get total amount of business in a day.
*
* @return total sales
*/
public double getSalesTotal() {
return totalSales / 100;
}
/**
* Get total number of transactions in a day.
*
* @return number of all sales
*/
public int getSalesCount() {
return totalSalesCount;
}
public static void resetSales() {
totalSales = 0;
totalSalesCount = 0;
}
}
And here is my current CashRegisterTester class (the code that needs to be modified)
public class CashRegisterTester
{
public static void main(String[] args)
{
//Initialize all variables
// Construct a CashRegister object
CashRegister register1 = new CashRegister();
// Invoke a non-static method of the object
// since it is non-static, you must have an object
register1.addItem(1.95);
register1.addItem(2.00);
register1.addItem(2.05);
System.out.println(register1.getCount());
System.out.printf("%.2f ", register1.getTotal());
}//main
//more methods here
}//class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started