Cash Register Change Program


 
Thread Tools Search this Thread
Top Forums Programming Cash Register Change Program
# 8  
Old 12-07-2014
@ totoro125

I would make my class an empty template so I could use it over and over. Each time you use it you can initialize the values by passing parameters. The constructor should look something like this:
Code:
class CashRegister(object):
    """A class that makes big money."""
    def __init__(self, twenties, tens, fives, ones):
        self.twenties = twenties
        self.tens = tens
        self.fives = fives
        self.ones = ones

Make up some defs to test your classes while you are learning. Python has good error reporting built in.
Code:
    def description(self):
        print "I have %s twenties, %s tens and I have %s fives." % (self.twenties, self.tens, self.fives)

    def is_broke(self):
        if self.ones:
            print "Yep! I'm flush.\n"

        else:
            print "Please help me! I am broke.\n"

In the main body of your script is where you run the tests and other functions you built into the class.

Code:
Yesterday = CashRegister(4, 8, 16, 1)
Yesterday.description()
Yesterday.is_broke()

Today = CashRegister(5, 10, 20, False)
Today.description()
Today.is_broke()

# 9  
Old 12-13-2014
Quote:
Originally Posted by ongoto
@ totoro125

I would make my class an empty template so I could use it over and over. Each time you use it you can initialize the values by passing parameters. The constructor should look something like this:
Code:
class CashRegister(object):
    """A class that makes big money."""
    def __init__(self, twenties, tens, fives, ones):
        self.twenties = twenties
        self.tens = tens
        self.fives = fives
        self.ones = ones

Make up some defs to test your classes while you are learning. Python has good error reporting built in.
Code:
    def description(self):
        print "I have %s twenties, %s tens and I have %s fives." % (self.twenties, self.tens, self.fives)

    def is_broke(self):
        if self.ones:
            print "Yep! I'm flush.\n"

        else:
            print "Please help me! I am broke.\n"

In the main body of your script is where you run the tests and other functions you built into the class.

Code:
Yesterday = CashRegister(4, 8, 16, 1)
Yesterday.description()
Yesterday.is_broke()

Today = CashRegister(5, 10, 20, False)
Today.description()
Today.is_broke()

okay, I have this so far:

Code:
class CashRegister(object):
    def __init__(self, drawer):
        self.drawer = self.formatMoney(drawer)
    ## Purchase function
    def purchase(self, price, amount_tendered):
        change_required = self.sumMoney(self.formatMoney(amount_tendered)) - price
        if change_required == 0:
            return [0, 0, 0, 0]
        if change_required > self.sumMoney(self.drawer):
            return 3
        if self.sumMoney(self.formatMoney(amount_tendered)) < price:
            return 2
        ret = {'ones': 0, 'fives': 0, 'tens': 0, 'twenties': 0}
        if change_required >= 20:
            ret['twenties'] = change_required / 20
            change_required = change_required % 20
        if change_required >= 10:
            ret['tens'] = change_required / 10
            change_required = change_required % 10
        if change_required >= 5:
            ret['fives'] = change_required / 5
            change_required = change_required % 5
        ret['ones'] = change_required
        return ret
    ## Helper function
    def sumMoney(self, l):
        s = 0
        s += l['ones']
        s += l['fives'] * 5
        s += l['tens'] * 10
        s += l['twenties'] * 20
        return s
    def formatMoney(self, l):
        return {'ones': l[0], 'fives': l[1], 'tens': l[2], 'twenties': l[3]}

How can I change part of this so I can test it by typing:
$ Cashregister purchase 38 = 0 0 0 2
on the command line?
# 10  
Old 12-15-2014
You've been busy. Smilie
For something as advanced as this it would help you to no end to learn more about Python containers; dictionaries, tuples, lists, etc.
You are passing around lists of numbers here and you need to manage them as groups.

I liked this part, the rest I doctored a little.
Code:
def sumMoney(self, l):
        s = 0
        s += l['ones']
        s += l['fives'] * 5
        s += l['tens'] * 10
        s += l['twenties'] * 20
        return s

Not bad so far. We're still a ways from running from the command line though.
Keep pluggin'.

There are people out here better qualified than me, and there's probably many different ways to do this stuff, but here's what I've come up with; based on my understanding of what you want to do. You might have to work out a few bugs.
Code:
# You dont want function calls in __init__ because the functions haven't been
# initialized yet.  Use __init__ to assign values and such
# from the arguments you provide when you create an instance of the class.
class CashRegister(object):
    def __init__(self, ones, fives, tens, twenties):
        self.drawer = {'ones': ones, 'fives': fives, 'tens': tens, 'twenties': twenties}
        self.tender = {'ones': 0, 'fives': 0, 'tens': 0, 'twenties': 0}
        self.cash = {'ones': 0, 'fives': 0, 'tens': 0, 'twenties': 0}


## Purchase function
    def purchase(self, price, ones, fives, tens, twenties):
        self.tender = {'ones': ones, 'fives': fives, 'tens': tens, 'twenties': twenties}
        change_required = self.sum_tendered() - price

        if change_required == 0:
            return "err0" # distinguish error numbers from dollars

        if self.sum_tendered() < price:
            return "err2"

        # if change_required > self.sumMoney(self.drawer):
        if change_required > self.sum_drawer():
            return "err3"

        return change_required
 
## Helper functions
    def formatMoney(self, amount):
        self.cash = {'ones': 0, 'fives': 0, 'tens': 0, 'twenties': 0}
        if amount >= 20:
            self.cash['twenties'] = amount / 20
            amount = amount % 20

        if amount >= 10:
            self.cash['tens'] = amount / 10
            amount = amount % 10

        if amount >= 5:
            self.cash['fives'] = amount / 5
            amount = amount % 5
        self.cash['ones'] = amount

        return self.cash

    def sumMoney(self, fmt):
        self.cash = fmt
        s = 0
        s += self.cash['ones']
        s += self.cash['fives'] * 5
        s += self.cash['tens'] * 10
        s += self.cash['twenties'] * 20
        return s

    def cash_drawer(self):
        return self.drawer
    def cash_tendered(self):
        return self.tender
    def sum_drawer(self):
        return self.sumMoney(self.drawer)
    def sum_tendered(self):
        return self.sumMoney(self.tender)

############
# some random testing
############

d1 = {}
reg = CashRegister(20, 20, 10, 10)

change = reg.purchase(38, 0, 0, 0, 2)
d1 = reg.cash_tendered()
tender = reg.sum_tendered()
print 'tendered:$%s = %s, %s, %s, %s' % (tender, d1['ones'], d1['fives'], d1['tens'], d1['twenties'])
d1 = reg.formatMoney(change)
print 'change:$%s = %s, %s, %s, %s' % (change, d1['ones'], d1['fives'], d1['tens'], d1['twenties'])
d1 = reg.cash_drawer()
drawer = reg.sum_drawer()
print 'drawer:$%s = %s, %s, %s, %s' % (drawer, d1['ones'], d1['fives'], d1['tens'], d1['twenties'])

--------- Post updated at 08:18 PM ---------- Previous update was at 01:36 AM ----------

EDIT:
Found some ugly bugs.
Did some renaming because vars were getting clobbered in the Helper functions.
Errors 0, 2, 3 were causing confusion with dollar output.
Added a few helper functions to ease the use of the class in a progamming sense.
Made several other changes and reposted the code.

The above tests now produce decent output.

Code:
tendered:$40 = 0, 0, 0, 2 change:$2 = 2, 0, 0, 0 drawer:$420 = 20, 20, 10, 10

Last edited by ongoto; 12-15-2014 at 12:20 AM.. Reason: Noted many improvements to original posting
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