__author__ = 'jsgreenwell' from random import randrange class RegisterFunctions: """ Basic Class example, includes additional methods and attributes that are not used now but will be expanded. Current initialization is with or without sales_rep (sets a default value when not passed). """ def __init__(self, sales_rep=12515): """ Sets basic variables and initiates class. payment is set so it can be accessed directly from main for calculations purchases may be needed later (Tk will display a screen of just purchases) total is also used for calculates :param sales_rep: defaults to 12515; not used at the moment """ self.transactionNum = randrange(1222, 4000) #TODO: Unique identifier self.purchases = {} self.rep = sales_rep self.payment = 0.0 self.total = 0.0 def gettransaction(self): """ :return: transactionNum """ return self.transactionNum def addpurchase(self, item_id, item_name, item_qty, item_price): """ Adds a purchased item to the purchases attribute :param item_id: id of item being purchased :param item_name: name of the item :param item_qty: number of items being purchased :param item_price: price of the item being purchased """ #This will probably be added as (self, item_id, list) with * self.purchases[item_id] = {"name": item_name, "qty": item_qty, "price": item_price} def getpurchases(self): """ :return: stringified version of the purchases dict for error testing mostly """ return str(self.purchases) def changeqty(self, item_id, change, amount): """ Increase or decrease the amount of items purchased :param item_id: id of item to change :param change: type of change - increase qty or decrease :param amount: number to change :return: """ if change == "inc": self.purchases[item_id]["qty"] += amount elif change == "decr": self.purchases[item_id]["qty"] -= amount def voiditem(self, item_id): """ Removes passed item from purchases list """ self.purchases.pop(item_id, None) def voidpurchase(self): """ Empties purchases (voids transaction) """ self.purchases.clear() def pricecorrection(self, item_id, correct_type, new_price): """ Manually corrects a price for special promotions, errors, and other reasons :param item_id: id of item :param correct_type: price = set price to given price, discount = subtract from price, percent = take off percentage :param new_price: amount to decrease price """ #add a log print here with some passed given reason and self.rep if correct_type == "price": self.purchases[item_id]["price"] = new_price elif correct_type == "discount": self.purchases[item_id]["price"] -= new_price elif correct_type == "percent": self.purchases[item_id]["price"] -= self.purchases[item_id]["price"] * new_price def changerep(self, new_rep): #also log print self.rep = new_rep def calctotalsale(self): """ Calculates the total price due. :return: totalsale: the Sale Price Total """ totalsale = 0.0 for items in self.purchases.keys(): totalsale += self.purchases[items]["price"] * self.purchases[items]["qty"] self.total = totalsale return totalsale def addpayment(self, pay_amount): """ Adds passed pay_amount to the total payment attribute """ self.payment += pay_amount def getchange(self): """ Reduce diff_owed until less than each denomination and add to a count of each denomination showing how much of each bill and coinage to give as change. This is the actual knapsack problem. :return: String displaying the change due or if an Error has occurred """ change = 0.0 denominations = (20.0, 10.0, 5.0, 1.0, .25, .1, .05, .01) if self.payment == self.total: return 'Change due: $0.00' elif self.payment > self.total: diff_owed = self.payment - self.total return_string = "Change due is ${0:.2f}. Need:\n".format(diff_owed) for den in denominations: den_count = 0 while diff_owed >= den: den_count += 1 diff_owed -= den return_string += " {0:.2f}: {1}\n".format(den, den_count) return return_string return "Error processing payment"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question