Skip to content

Instantly share code, notes, and snippets.

@0xpizza
Last active April 12, 2019 19:31
Show Gist options
  • Save 0xpizza/5dd9df644588200c7065fb900fd39fac to your computer and use it in GitHub Desktop.
Save 0xpizza/5dd9df644588200c7065fb900fd39fac to your computer and use it in GitHub Desktop.
class ItemToPurchase:
def __init__(self, item_name, item_description, item_price, item_quantity):
self.item_name = item_name
self.item_description = item_description
self.item_price = float(item_price)
self.item_quantity = float(item_quantity)
def print_item_cost(self):
totalCost = self.item_quantity * self.item_price
print('%s %d @ $%d = $%d' % (self.item_name, self.item_quantity, self.item_price, totalCost))
def print_item_description(self):
print('%s: %s' % (self.item_name, self.item_description))
@property
def fields(self):
return ( self.item_name
,self.item_description
,self.item_price
,self.item_quantity)
class ShoppingCart:
def __init__(self, customer_name, current_date):
self.customer_name = customer_name
self.current_date = current_date
self.cart_items = []
def add_item(self, item):
self.cart_items.append(item)
def remove_item(self, item_name):
found = False
for i in self.cart_items:
if i.item_name == item_name:
found = True
self.cart_items.remove(i)
if found == False:
print('Item not found in cart. Nothing removed.')
def modify_item(self, item):
found = False
for i in range(len(self.cart_items)):
if self.cart_items[i].item_name == item.item_name:
found = True
self.cart_items[i].item_quantity = item.item_quantity
if found == False:
print('Item not found in cart. Nothing modified.')
def get_num_items_in_cart(self):
totalNumItems = 0
for i in self.cart_items:
totalNumItems += i.item_quantity
return totalNumItems
def get_cost_of_cart(self):
totalCost = 0
for i in self.cart_items:
totalCost += (i.item_price * i.item_quantity)
return totalCost
def print_total(self):
print('Customer #%s Order Overview - %s\n' % (self.customer_name, self.current_date))
print('Number of Items: %d\n' % self.get_num_items_in_cart())
if len(self.cart_items) > 0:
for i in self.cart_items:
i.print_item_cost()
else:
print('SHOPPING CART IS EMPTY')
totalCost = self.get_cost_of_cart()
print('\nTotal: $%d' % totalCost)
def print_descriptions(self):
print('Customer #%s Order Overview - %s\n' % (self.customer_name, self.current_date))
print('Item Descriptions')
if len(self.cart_items) > 0:
for i in self.cart_items:
i.print_item_description()
else:
print('SHOPPING CART IS EMPTY')
def print_menu(theCart):
menuOp = ' '
print('MENU')
print('a - Add item to cart')
print('r - Remove item from cart')
print('c - Change item quantity')
print('i - Output items\' descriptions')
print('o - Output shopping cart')
print('q - Quit\n')
while menuOp != 'a' and menuOp != 'r' and menuOp != 'c' and menuOp != 'i' and menuOp != 'o' and menuOp != 'q':
menuOp = input('Choose an option:\n')
if menuOp == 'a':
print('ADD ITEM TO CART')
name = input('Enter the item name:\n')
description = input('Enter the item description:\n')
price = float(input('Enter the item price:\n'))
quantity = int(input('Enter the item quantity:\n'))
newItem = ItemToPurchase()
newItem.item_name = name
newItem.item_description = description
newItem.item_price = price
newItem.item_quantity = quantity
theCart.add_item(newItem)
elif menuOp == 'r':
print('REMOVE ITEM FROM CART')
name = input('Enter name of item to remove:\n')
theCart.remove_item(name)
elif menuOp == 'c':
print('CHANGE ITEM QUANTITY')
name = input('Enter the item name:\n')
quantity = int(input('Enter the new quantity:\n'))
item = ItemToPurchase()
item.item_name = name
item.item_quantity = quantity
theCart.modify_item(item)
elif menuOp == 'i':
print('OUTPUT ITEMS\' DESCRIPTIONS')
theCart.print_descriptions()
elif menuOp == 'o':
print('OUTPUT SHOPPING CART')
theCart.print_total()
return menuOp
def main():
menuChoice = ' '
custName = input('Enter customer number\n')
dayDate = input('Enter today\'s date:\n')
print('\nCustomer ID: %s' % custName)
print('Today\'s date: %s' % dayDate)
myCart = ShoppingCart(custName, dayDate)
while menuChoice != 'q':
print('')
menuChoice = print_menu(myCart)
import tkinter as tk
class App(tk.Tk):
'''
tkinter has 1 main window by default called "root" or "master".
this window is instatiated from the Tk class. it has a method called
`mainloop` which is the entry point for the tkinter module.
tkinter implements a programming paradigm called event-driven programming
through an event loop in the background. a programmer can interact with
the event loop in various ways, but the simplest is to use a Button with
its `command` parameter set to a function.
this class represents the cart
'''
class _EditableItem(tk.Frame):
def __init__(self, *args, **kwargs):
'''
this class represents each item in the cart.
'''
# we are going to have our cart item in here so we can edit it,
# but we dont want to give the item to the superclass's constructor
# so we pop it out first.
self.number = kwargs.pop()
super().__init__(*args, **kwargs)
self.bind('<Button-1>', self.popup_editor)
self.popup_editor()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry('600x400')
self.cart = ShoppingCart('joe mama', '1/1/1111')
# set up the GUI. this is the status area at the top which will have
# relevent information inside it. we aren't going to assign it to
# self because we only need to keep track of it for the scope of this
# function in order to populate it, then who cares.
status_area = tk.Frame(self)
status_area.pack(fill=tk.X) # read about pack on effbot.org it's very important
# Labels are static text you can put places. you can set their text via
# the text argument, or with a variable that it will read from. Only
# use the variable if you intend on changing the text later. the variables
# must be tkinter.StringVar objects. We are going to put the price of the
# cart at the top in the status bar, so we make it a stringvar. the
# person's name, however, doesn't change, so we set it to static via text=
tk.Label(status_area, text=self.cart.customer_name).pack(side=tk.LEFT)
tk.Label(status_area, text=self.cart.current_date).pack(side=tk.LEFT)
# here's the variable that has the text (we can edit this)
self.shopping_cart_value = tk.StringVar()
# and here is our label that displays the text (dont edit this)
tk.Label(status_area, textvar=self.shopping_cart_value).pack(side=tk.RIGHT)
# here is where we are going to put out shopping cart items. typically you
# want to designate areas inside their own frames just to keep everything
# nice and organized. it also allows you to display data in a grid since
# you can't mix packed widgets with gridded widgets.
self.item_area = tk.Frame(self)
self.item_area.pack(fill=tk.BOTH, expand=True)
# button for adding new items
tk.Button(self, text='New Item', command=self.add_item).pack()
self.update_cart() # show that the price and quantity is zero
def add_item(self):
# we are't keeping a reference to the item, but the item_area is
new_item = App._EditableItem(self.item_area)
if new_item.item: # make sure user didn't press the X or something
self.cart.add_item(new_item.item)
new_item.pack()
self.update_cart()
def remove_item(self):
'''
you can do this a few ways. one way would be to give the cart to the item
and it removes itself. another would be to pass this function in as
a callback of some sort. you can find the item in the cart and delete it
and the _EditableItem object associated with it. Or, you can just give each
item object a number and have a button that removes it or something.
I'll leave it up to you.
'''
def update_cart(self):
fmt = '{} items in cart (${})'
self.shopping_cart_value.set(fmt.format(
self.cart.get_num_items_in_cart()
,self.cart.get_cost_of_cart()
))
if __name__ == '__main__':
App().mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment