In: Computer Science
Please see the java code below and amend it according
to these requirements:
* Do not remove any of the code within the public
interface to the class, only add code to test the additional
functionality
*Limit items in each instance
*Allow the last item entered to be deleted
The CashRegister class should be modified to limit the
number of items that can be added to an instance of the
CashRegister. The limit should be declared as a
constant in such a way that all instances of the
CashRegisterFixedSize have the same limit. The
CashRegisterFixedSize class should also have a new instance method,
undo(), that removes the last item entered into an
instance.
Hints: To implement the additional
functionality required, it is recommended that you use an
array to store each item’s price within the
CashRegisterFixedSize class.
You should adapt the count variable so it
can be used to set the index of the array – this will be used store
each entry, calculate the total price and when removing the last
entered item. The getTotal() method will need to be
modified to calculate the total of all values stored in
the array. A loop construct is ideal for this. When implementing
the undo()method, the value stored in the variable count should be
considered.
The code:
public class CashRegister
{
private int itemCount;
private double totalPrice;
public void addItem (double price)
{
itemCount ++;
totalPrice = totalPrice + price;
}
public void clear ()
{
itemCount = 0;
totalPrice = 0;
}
public double getTotal()
{
return totalPrice;
}
public int getCount()
{
return itemCount;
}
public CashRegister()
{
itemCount = 0;
totalPrice = 0;
}
}
Hi,
Please find the java classes below:
CashRegisterFixedSize.java
//----------------------------------------------
//CashRegisterFixedSize.java
//----------------------------------------------
public class CashRegisterFixedSize {
//constant LIMIT of number of items
public static final int LIMIT = 5;
//instance variables
double itemPrices[];
private int itemCount;
private double totalPrice;
//Constructor
public CashRegisterFixedSize() {
itemPrices = new
double[LIMIT];
itemCount = 0;
totalPrice = 0;
}
//---------------------------------------
// addItem() method
//----------------------------------------
public void addItem(double price) {
try {
itemCount++;
totalPrice = totalPrice +
price;
itemPrices[itemCount] =
price;
}catch(ArrayIndexOutOfBoundsException ae) {
System.out.println("Register full!!");
}
catch(Exception e) {
}
}
//---------------------------------------
// clear() method
//----------------------------------------
public void clear() {
itemPrices = new
double[LIMIT];
itemCount = 0;
totalPrice = 0;
}
//---------------------------------------
// getTotal() method
//----------------------------------------
public double getTotal() {
return totalPrice;
}
//---------------------------------------
// getCount() method
//----------------------------------------
public int getCount() {
return itemCount;
}
//---------------------------------------
// undo() method
//----------------------------------------
public void undo() {
if (itemCount > 0)
itemCount--;
}
}
//////////////////////////////////////////////
TestDriver.java
import java.util.Scanner;
//--------------------------------
// TestDriver.java
//---------------------------------
public class TestDriver {
public static void main(String[] args)
{
Scanner input = new
Scanner(System.in);
CashRegisterFixedSize crfs = new
CashRegisterFixedSize();
System.out.println("Enter item
price: ");
double price =
input.nextDouble();
crfs.addItem(price);
System.out.println("Enter item
price: ");
price = input.nextDouble();
crfs.addItem(price);
System.out.println("Number of
items =: " + crfs.getCount());
System.out.println("Total cost =: "
+ crfs.getTotal());
System.out.println("Enter item
price: ");
//Exception
price = input.nextDouble();
crfs.addItem(price);
System.out.println("Number of
items =: " + crfs.getCount());
System.out.println("Total cost =: "
+ crfs.getTotal());
}
}
/////////////////////////////////////////
Screenshot
Sample Output
Enter item price:
100
Enter item price:
200
Number of items =: 2
Total cost =: 300.0
Enter item price:
550
Number of items =: 3
Total cost =: 850.0
--------------------------------------------------
Hope this helps.