Cash Register Change Program


 
Thread Tools Search this Thread
Top Forums Programming Cash Register Change Program
# 1  
Old 11-30-2014
Cash Register Change Program

I just need some suggestions on how to start this program I wanted to work on for practice.
I want to make a cash register program that gives change and only works with $1's, $5's, $10's, and $20's (I might add cents in later for more practice).
Change should always be made with the largest available denominations.

Here's an example of the output I want it to do.
Code:
# Initialize the till with a two five dollar bills and five ones.
$ cashreg init 50 = 20 4 1 0 
  # Verify input for init (amounts don't match):
$ cashreg init 10 = 5 2 0 0 
  # Note, drawer hasn't changed
  # Purchase $38 with two $20s:
$ cashreg purchase 38 = 0 0 0 2
2 0 0 0
  # Change a five for 5 ones
$ cashreg change 0 1 = 5 
5 0 0 0
  # Change a 20 for a ten, five and five ones:
$ cashreg change 0 0 0 1 = 5 1 1 0
5 1 1 0 
  # Report the contents of the till
$ cashreg report
38 : 88 = 8 4 0 3

Do you think Python or Java would be easier to do this in? I was also thinking about AWK or Bash? I am a beginner in all these languages.
This User Gave Thanks to totoro125 For This Post:
# 2  
Old 12-01-2014
I would use what you are most comfortable in, or even use this as practice for writing in them all. The key thing to get you logic documented so you can then write the code without having to think of the whole thing at the same time.

I would suggest that you write down a process using a flow chart or structure diagram that you would use manually and keep refining each step to more and more detail, including loops and conditions, to (for instance) try a $5 and work out how much is still required, loop round and try another $5 if you have one etc.

One question that you will need to answer for yourself is "How are you planning to store the cash?" Are you thinking of simple variables, arrays or even using a file perhaps?



I suppose convention might suggest putting the largest value denomination first, so your input/output would be in the form of twenties, tens, fives and ones, i.e.:-
Code:
$ cashreg init 50 = 0 1 4 20

$ cashreg purchase 38 = 2 0 0 0
0 0 0 2


I hope that this gives you a bit of a steer. Let us know how you get on. If you get stuck on specific coding, paste it to the thread and we will see if someone can help.


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 12-01-2014
You might want to start with how do you make change. You can use a floor or truncate function with division to tell how much of a certain currency you will give as change. Subtract the value of that currency from the change that needs to be given and continue on with the next lower currency until you get to cents. Which is where you stop. So if you owe $61.97 in change see what steps you need to follow to give proper change. With SQL I would do the following:

Code:
SQL> select floor(61.97/20) number_of_twenties,
  2  61.97-(20*floor(61.97/20)) remaining_change from dual;

NUMBER_OF_TWENTIES REMAINING_CHANGE
------------------ ----------------
                 3             1.97
SQL> select floor(1.97/10) number_of_tens,
  2  1.97-(10*floor(1.97/10)) remaining_change from dual;

NUMBER_OF_TENS REMAINING_CHANGE
-------------- ----------------
             0             1.97
SQL> select floor(1.97/5) number_of_fives,
  2  1.97-(5*floor(1.97/5)) remaining_change from dual;

NUMBER_OF_FIVES REMAINING_CHANGE
--------------- ----------------
              0             1.97
SQL> select floor(1.97/1) number_of_ones,
  2  1.97-(1*floor(1.97/1)) remaining_change from dual;

NUMBER_OF_ONES REMAINING_CHANGE
-------------- ----------------
             1              .97
SQL> select floor(.97/.25) number_of_quarters,
  2  .97-(.25*floor(.97/.25)) remaining_change from dual;

NUMBER_OF_QUARTERS REMAINING_CHANGE
------------------ ----------------
                 3              .22

SQL> select floor(.22/.10) number_of_dimes,
  2  .22-(.1*floor(.22/.10)) remaining_change from dual;

NUMBER_OF_DIMES REMAINING_CHANGE
--------------- ----------------
              2              .02

SQL> select floor(.02/.05) number_of_nickels,
  2  .02-(.05*floor(.02/.05)) remaining_change from dual;

NUMBER_OF_NICKELS REMAINING_CHANGE
----------------- ----------------
                0              .02

At this point multiply by 100 for the number of pennies. Once you know the method of figuring out change, then figure out what language you can code it in. You might try Perl or Python this. You can use an array or set of arrays to store the currency and the proper order to do the computation with.
This User Gave Thanks to gandolf989 For This Post:
# 4  
Old 12-01-2014
Eventhough BASH is my currently my most favorite language, for (possibly) 'real' applications, specialy when it comes with numbers handling as in do math, i would choose something else... like python.

AFAIK, awk is no programing language, but a 'streaming/parsing' tool.
(simple said)

Me quite confused by your syntax.
There is:
Code:
init 50 = 0 1 4 20
purchase 38 = 2 0 0 0

But there is also
Code:
change 0 0 0 1 = 5 1 1 0
change 0 1 = 5

To me this looks like you want to do 2 things at once, while the programing itself, would might be logical, think of what the cashier must do to enter this syntax!

Usualy the chashiers just type in the amount of cash the items costs, and the 'added up' amount the buyer pays. They do NOT count every single piece of cash, and enter 2x5$ 1x10$, 3x20$, etc...
They simply type: 78$ cost -> 80$ paid, returns -> 2$ returns.

I agree, it WOULD be a nice-to-have feature, but for the cashier its very unpractical to count the cash (not the value) each and every time, all day long, when there are 50 people at the register waiting..

Get my point? Smilie
In the end, a cash register is more like a big calculator, sure, it contains cash, but in the end, it just sums up the values, and not the counts of cash-items.
That is done at the evenings, manualy. (unless they have a cash counter- another device)

So my only suggestion is to make the syntax simpler.

Change only: VALUE
Pay: COST GIVEN
-> Returns: return/change

So you would only need to catch 2 arguments.


However, if you plan to do this as a 'backend', as in you then could make the GUI having fields to enter the amount of every single cash-item, which then, if there is something within the text field, pass this value as optional value to the script.
As in:
Pay: -twenty 1 -five 1 COST(22) paid(sumy up the textfield=25)
Returns: Return 3
(since there were optionas passed, show) Flow: +10 +5 -2 -1
While the italic text would be generated by the GUI, since there were only 2 (optional) textfield filled)

Hope this helps

EDIT:
Alternativly, you could expect:
Pay: COST / GIVEN
And simply split up the GIVEN to a possiblity of cash combinations.
This User Gave Thanks to sea For This Post:
# 5  
Old 12-06-2014
Thanks guys for all your suggestions. I decided to use Python and I am really stuck. I started to create classes but I am not sure if I even have to. I am not that great in programming and I want to know if I am starting this correctly.


Code:
#!/usr/bin/python

class CashRegister:

   ones = 1;
   fives = 5;
   tens = 10;
   twenties = 20;

   #Initializes drawer, using counts of bills provided
   def _init_(self, amt):
      self._amt = ones [fives [tens [twenties]]]

   def purchase_price(price, amount_tendered):

Here is what I am attempting to do for my first two commands:

-init amt = ones [fives [tens [twenties]]]

-Initialize drawer, using counts of bills provided
-If a drawer already exists, this will overwrite it.

-purchase price = amount_tendered

-price
-Integer
-amount_tendered
-Number and type of bills given to register
-amt = ones [fives [tens [twenties]]], where amounts are integers, counts
-Output will be a single line, if successful:
-change returned , where change returned has the same format: ones fives tens twenties

Example output for first two commands:
Code:
# Initialize the till with a two five dollar bills and five ones.
$ cashreg init 50 = 20 4 1 0 
  # Verify input for init (amounts don't match):
$ cashreg init 10 = 5 2 0 0 
  # Note, drawer hasn't changed
  # Purchase $38 with two $20s:
$ cashreg purchase 38 = 0 0 0 2
2 0 0 0

So do I make the classes and implement the commands? Then in main() just take the user input and call the functions?
# 6  
Old 12-07-2014
@ totoro125
You give me all kinds of ideas. Smilie

You could have two classes and keep transactions separated.

Class CashRegister
An accounting of the bills/coins that should be in your drawer.
date
ones = 1;
fives = 5;
tens = 10;
twenties = 20;
coins...

def bills - add/subtract

def coins - add/subtract

def report

Class Transaction
Keeping track of bills/coins recieved for each exchange.
record
transaction_id
date
sale_amount
tendered_amount
change_amount
tender
bills
coins
change
bills
coins

def update register

def calculate change

Figure out the largest bills/coins to give as change amount.
Update the register.
---------
To start (instantiate) a transaction you would give each a unique name -- something like...

tx1 = Transaction()
call the methods using the new instance name..
tx1.method(param)
...and then append them to a list
txList.append(tx1)

Do the register the same way...
regToday = CashRegister()
This User Gave Thanks to ongoto For This Post:
# 7  
Old 12-07-2014
Quote:
Originally Posted by ongoto
@ totoro125
You give me all kinds of ideas. Smilie

You could have two classes and keep transactions separated.

Class CashRegister
An accounting of the bills/coins that should be in your drawer.
date
ones = 1;
fives = 5;
tens = 10;
twenties = 20;
coins...

def bills - add/subtract

def coins - add/subtract

def report

Class Transaction
Keeping track of bills/coins recieved for each exchange.
record
transaction_id
date
sale_amount
tendered_amount
change_amount
tender
bills
coins
change
bills
coins

def update register

def calculate change

Figure out the largest bills/coins to give as change amount.
Update the register.
---------
To start (instantiate) a transaction you would give each a unique name -- something like...

tx1 = Transaction()
call the methods using the new instance name..
tx1.method(param)
...and then append them to a list
txList.append(tx1)

Do the register the same way...
regToday = CashRegister()
Thank you! That helps a lot, but is how I started correct? And did I do the syntax correctly?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Change to different user id inside a program

Hi, There is a process ( built in C/C++) which starts with my user id and I need to execute a specific function with a different user id. Is there any api so that I provide userid, passwd and the next instance the process will have the new user id privileges. - Pranav (3 Replies)
Discussion started by: k_pranava
3 Replies

2. Debian

Change the privileges needed to run a program

Hi everyone, I have an issue with a project of mine. I have to run a program on a terminal which requires to be logged in as su to have it run it. Given that I'm having problem to use expect to give the password I'd like to change the privilege of that program from SU to normal user ( I have the SU... (13 Replies)
Discussion started by: gaisselick87
13 Replies

3. Homework & Coursework Questions

Simulate a ATM (cash machine) on UNIX(Putty) using scritps

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to create a simple ATM (Cash machine simulation in unix) which shows a welcome screen, then a login screen... (2 Replies)
Discussion started by: oobla01
2 Replies

4. UNIX for Dummies Questions & Answers

Create a simple ATM (Cash machine simulation)

I need to create a simple ATM (Cash machine simulation in unix) which shows a welcome screen, then a login screen to enter 3 users details. help please on the coding The users details need to be in a txt file: the details are: (PIN No, First name last name, Account number, Balance, Histrosy) ... (1 Reply)
Discussion started by: oobla01
1 Replies

5. Programming

Change Pseudo Code to C Program (print and fork)

I am very new at programming and this is probably an easy question, but I am desperate. I need to change this low level code into a C program that I can run so I can print out every "A" that appears with the fork() command. You help is greatly appreciated. PRINT A p=fork() if( p == 0) {... (0 Replies)
Discussion started by: tpommm
0 Replies

6. UNIX for Advanced & Expert Users

Finding register set being used in program

How can i find( or list) contents of all registers being used by my program? Is there any system call or library available for this?:confused: At runtime in my c/c++ program. At runtime using may be some assembly hack!!!!!!!!!!! (2 Replies)
Discussion started by: amit gangarade
2 Replies

7. Shell Programming and Scripting

script/program to change the password ?

hi, Somebody have or known where i can find a perl small perl program to change the password. The point: First it verify is the user exist, checking the old typed password and replace it with new. The passwords must be encoded. Thanks, very much! (0 Replies)
Discussion started by: kad
0 Replies

8. UNIX for Dummies Questions & Answers

UNIX Program Change Question

I am an IT auditor with a big 4 accounting firm and am cur the process of conducting an application program change audit for a large public company related to Sarbanes Oxley. This is my first time using this forum and need some assistance in verifying a fact that I believe to be true. My client... (1 Reply)
Discussion started by: mpp122
1 Replies
Login or Register to Ask a Question