Question: Java The question is: Reimplement the CashRegister class so that it keeps track of the total price as an integer: the total cents of the

Java

The question is:

Reimplement the CashRegister class so that it keeps track of the total price as an integer: the total cents of the price. For example, instead of storing 17.29, store the integer 1729. Such an implementation is commonly used because it avoids the accumulation of round off errors. Do not change the public interface of the class.

Here is my code. Please reimplement the class using the following code to answer the above question. Please include your code comments. Please let me know if the following is wrong in anyway and cannot be used for answer.

/** This is a simulated cash register that keeps track of the price of each added item in an ArrayList. **/

import java.util.ArrayList;

public class Question46 { private ArrayList prices; private ArrayList taxables; private double taxRate;

// Constructing a cash register with cleared item count and total with tax rate // public Question46(double aTaxRate) { prices = new ArrayList<>(); taxables = new ArrayList<>(); taxRate = aTaxRate; }

// Adding item to this cash register with price and if tax is due // public void addItem(double price, boolean taxable) { prices.add(price); taxables.add(taxable); }

// Get the price of all items in the current sale and return total // 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; }

// Get the number of items in the current sale. // public int getCount() { return prices.size(); }

// Clear the item count and the total /// public void clear() { prices.clear(); taxables.clear(); } }

---------

If it helps, here was an answer I got from someone on Chegg but I am not sure if this works at all. You may use it as reference if it helps or discard it like I did.

/** * Re-implement the CashRegister class so that it keeps track of the total price as an integer: the total cents of the price. **/

import java.util.ArrayList; import java.util.Scanner;

public class CashRegister2 { ArrayList prices = new ArrayList(); // add item // public void additem(int num) { prices.add(num); } // calculating total // public int getTotal() { int total = 0; for (int i = 0; i < prices.size(); i++) { total = total + (int) prices.get(i); } return total; } // get total objects stored so far // public int getCount() { return prices.size(); } // printing all prices // public void display() { System.out.println("Prices stored are: " + prices); }

// clear data // public void clear() { prices.clear(); }

public static void main(String args[]) { // scanner object declared to read input from user // // printing menu // CashRegister2 pricesStore = new CashRegister2(); Scanner sc = new Scanner(System.in); while (true) { System.out.println("1.addItem 2.getTotal 3.getCount 4.display 5.clear 6.exit"); int choice = sc.nextInt();// reading choice if (choice == 1) { System.out.println("Enter price: "); int price = sc.nextInt(); pricesStore.additem(price); } else if (choice == 2) { System.out.println("The total is: " + pricesStore.getTotal()); } else if (choice == 3) { System.out.println("The count is: " + pricesStore.getCount()); } else if (choice == 4) { pricesStore.display(); } else if (choice == 5) { pricesStore.clear(); } else {// safely exiting System.out.println("Byee!!"); break; } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!