Skip to content

S4_ChangeComputation_Medium

Wil Simpson edited this page Jul 30, 2021 · 5 revisions

Portfolio Home Page

Problem Statement

To make things easier for cashiers and also to minimize errors, most modern cash registers automatically calculate the change to be given. In this problem, you will be creating a program to support this feature. In this problem, you must create a program that allows the user to enter in price of an item and the amount the customer paid, and then displays the amount of change to be given. The program must also display how many of each unit of currency must be given in change. The cash register contains the following currency units:

20 Dollar bills
10 Dollar bills
5 Dollar bills
1 Dollar bills
Quarters
Dimes
Nickels
Pennies

Initialize the number of available currency units to be random numbers between 0-15. In this case, you may not always be able to give change in the most efficient manner. If your program is unable to give the correct amount of change, your program must display an appropriate error message. Your program must allow the cashier to input many transactions, thus increasing and decreasing the number of each currency unit that exists in the cash register. Your program must display the current amounts of each currency unit before each transaction so that the user knows how many of each is in the cash register.

User Documentation

To start processing transaction run the driver class ChangeComputation. A drawer with random amounts of bills between 0 and 15 inclusively will be created. You will then be asked to define the price of the item to be sold. Once that is given the program will start to process transactions. To process a transaction enter the number of each bill given for each transaction. If a transaction is unable to be processed due to insufficient funds given or in the drawer then you will be notified and the drawer will not be changed due to that transaction. The contents of the drawer will be printed to the screen after every transaction. To stop processing transactions simply answer "No" after a transaction has been entered.

Developer Documentation

Running the driver class ChangeComputation will create a new instance of ChangeSimulation and make a call to its run() method. The ChangeSimulation class handles the processing of transaction and interaction between the user and the console. The run() method creates a new instance of Drawer and then attempts to get a price for the item from the user. The user will be prompted to enter the price for the item until a valid amount is given. Once a valid amount is given the price of the drawer will be set with a call to Drawer#setItemPrice(float) and the simulation will start by making a call to ChangeSimulation#runSimulation(Drawer). This method continuously gets information from the user, checks the validity and processes each transaction until the user asks to stop.

Currency is an enum of all bills used for transaction from penny to the twenty dollar bill which consists of a string name of the currency and the value worth stored in a float. A Comparator is created which compares the value of two Currency.

Currencies contains a tree sorted map of each Currency mapped to an Integer which is sorted by the Comparator defined in Currency. Currencies acts as a drawer or a cash transaction with methods to increment a currency, reset and many other methods to easily handle transactions. Upon initialization currency fills the TreeMap<Currency, Integer> with every Currency and assigns it to the default value of 0. If Currencies constructor is given one or more instances of Currencies then the amount of each currency will be added together and the TreeMap<Currency, Integer> will be initialized with this amount.

Drawer contains an item price for the drawer and the number of each bill stored in Currencies which is randomized with bills inclusively between 0 and 15. Drawer has methods to set the item price, print the contents and processes transactions. If any errors occur while processing a transaction then the drawer will not be effected and a TransactionException will be thrown. To proccess the transaction efficiently, the drawer uses the largest currency to the smallest currency to give change. If change is unable to be given then an error is thrown. If a transaction is successful the proccessTransaction(Currencies) method will return the change to be given and the drawer will be modified accordingly.

UML Diagram: UML Diagram

JavaDocs

The java documents are served from a local web server on this machine. To start the web server, navigate to the directory immediately above where the source code is checked out (i.e. ~/git ) and then use "python -m SimpleHTTPServer" in that directory.

cd ~/git
python -m SimpleHTTPServer&

Note: if you are running python 3 (which you can check via opening a terminal and typing: python --version), then the command is:

python3 -m http.server

Click Here to View JavaDocs

Source Code

Link to the source code