Question

In: Computer Science

Please see the java code below and amend it according to these requirements: * Do not...

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;
}
}

Solutions

Expert Solution

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.


Related Solutions

For c++, please do not recycle other peoples code as they do not satisfy the requirements....
For c++, please do not recycle other peoples code as they do not satisfy the requirements. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
Please explain this C++ below on how to get the following outputs (see code) #include <iostream>...
Please explain this C++ below on how to get the following outputs (see code) #include <iostream> using namespace std; // Tests for algorithm correctness // ------------------------------- // 1 gives output "1 3   7 15 31 63 127 255 511 1023 2047" // 2 gives output "2 5 11 23 47 95 191 383 767 1535 3071" // 3 gives output "3 7 15 31 63 127 255 511 1023 2047 4095" // 5 gives output "5 11 23 47 95...
It's Java; the code should be the same as the output sample below; please, thank you....
It's Java; the code should be the same as the output sample below; please, thank you. Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class. Create a class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
Please do in java with code available for copy and with comments so I can follow...
Please do in java with code available for copy and with comments so I can follow along :)\ Develop a program that prints out the sum of each column of a two-dimensional array. The program defines method sumColumn() takes a two-dimensional array of integers and returns a single-dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and then calls method sumColumns()....
In Java, code an intelligent system for diagnosis breast cancer. There are no special requirements, you...
In Java, code an intelligent system for diagnosis breast cancer. There are no special requirements, you have free range to do so, as this is a learning experience. Be sure to use block comments and it would be beneficial to provide explanation. Thanks
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT