make changes to the program given bellow: reimplement the class so that it keeps
ID: 3632959 • Letter: M
Question
make changes to the program given bellow: reimplement the class so that it keeps track of the price of each added item in an ArrayList<double>. remove the itemCount and the totalPrice instance variables (no longer needed). reimplement the clear, addItem, getCount and getTotal methods. add a method public void displayAll() that displays the prices of all items in the current sale. use the same main() as in the original TestCashRegister class. add to it a call to displayAll().import java.util.*;
import java.io.*;
/** A simulated cash register that tracks the item count
* and the total amount due
*/
class CashRegister {
private int itemCount;
private double totalPrice;
/* constructs a cash register with cleared itemnumber and total
*/
public CashRegister(){
itemCount = 0;
totalPrice=0;
}
/* clears the item count and the total
*/
public void clear(){
itemCount=0;
totalPrice=0;
}
/* Adds an item to the cash register
*/
public void addItem(double price){
itemCount++;
totalPrice = totalPrice + price;
}
/* get the number of items in the current sale
*/
public int getCount(){
return itemCount;
}
/* gets the price of all items in the current sale
*/
public double getTotal(){
return totalPrice;
}
}
public class TestCashRegister{
public static void main(String[] args){
CashRegister cash1 = new CashRegister();
CashRegister cash2 = new CashRegister();
Scanner console = new Scanner (System.in);
cash1.addItem(1.99);
cash1.addItem(5.99);
cash2.addItem(10.99);
System.out.printf("%35s%15s ", "register1","register2");
System.out.printf("%15s%20d%15d ","Number of items",cash1.getCount(), cash2.getCount());
System.out.printf("%15s%20.2f%15.2f ","Total",cash1.getTotal(), cash2.getTotal());
}
}
Explanation / Answer
//Here you go //Didn't really know if the constructor should change so I left it empty //Hope this helps. import java.util.*; import java.io.*; /** A simulated cash register that tracks the item count * and the total amount due */ class CashRegister { private ArrayList list = new ArrayList(); /* constructs a cash register with cleared itemnumber and total */ public CashRegister(){ } /* clears the item count and the total */ public void clear(){ int size = list.size(); for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.