Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Python-Shopping-Cart/python_shopping_cart.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
165 lines (143 sloc)
5.37 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemToPurchase: | |
item_name = '' | |
item_price = int(0) | |
item_quantity = int(0) | |
item_description = '' | |
def __init__(self, item_name = 'none', item_price = 0, item_quantity = 0, item_description='none'): | |
self.item_name = item_name | |
self.item_price = item_price | |
self.item_quantity = item_quantity | |
self.item_description = item_description | |
def print_item_cost(self): | |
print(self.item_name,self.item_quantity,'@ $', end='') | |
print(int(self.item_price),'= $', end='') | |
print((self.item_quantity*int(self.item_price))) | |
def print_item_description(self): | |
print(self.item_name,end='') | |
print(':',self.item_description) | |
class ShoppingCart: | |
def __init__(self,customer_name = 'none', current_date = 'January 1, 2016', cart_items=[]): | |
self.customer_name = customer_name | |
self.current_date = current_date | |
self.cart_items = cart_items | |
def add_item(self, item): | |
self.cart_items.append(item) | |
def remove_item(self): | |
print() | |
print("REMOVE ITEM FROM CART") | |
st = input("Enter name of item to remove:"'\n') | |
n = 0 | |
for i in self.cart_items: | |
if(i.item_name == st): | |
del self.cart_items[n] | |
n+=1 | |
yes = True | |
break | |
else: | |
yes=False | |
if(yes==False): | |
print("Item not found in cart. Nothing removed.") | |
def modify_item(self): | |
print('CHANGE ITEM QUANTITY') | |
name = input("Enter the item name:"'\n') | |
for i in self.cart_items: | |
if(i.item_name == name): | |
quant = int(input("Enter the new quantity:"'\n')) | |
i.item_quantity = quant | |
yes = True | |
break | |
else: | |
yes = False | |
if(yes==False): | |
print("Item not found in cart. Nothing modified.") | |
def get_num_items_in_cart(self): | |
numrep = 0 | |
for i in self.cart_items: | |
numrep += i.item_quantity | |
return numrep | |
def get_cost_of_cart(self): | |
tc = int(0) | |
cost = int(0) | |
for i in self.cart_items: | |
cost = int((i.item_price*i.item_quantity)) | |
tc = tc + cost | |
return tc | |
def print_total(): | |
tc = self.get_cost_of_cart() | |
if tc == 0: | |
print("shopping cart is empty") | |
else: | |
output_cart() | |
def print_descriptions(self): | |
print('Output item descriptions') | |
print(self.customer_name,end='') | |
print("\'s", end = '') | |
print(' Shopping cart - ', end = '') | |
print(self.current_date) | |
print('Item Dscriptions') | |
for i in self.cart_items: | |
print(i.item_name,end='') | |
print(': ', end = '') | |
print(i.item_description) | |
def output_cart(self): | |
ne = ShoppingCart() | |
print('OUTPUT SHOPPING CART') | |
print(self.customer_name,end='') | |
print("\'s Shopping Cart - ",end ='') | |
print(self.current_date) | |
print('Number of Items:', ne.get_num_items_in_cart()) | |
print() | |
self.total_cost = self.get_cost_of_cart() | |
if self.total_cost ==0: | |
print("SHOPPING CART IS EMPTY") | |
tc = 0 | |
for i in self.cart_items: | |
print('{} {} @ ${} = ${}'.format(i.item_name, i.item_quantity, i.item_price, (i.item_price*i.item_quantity))) | |
tc +=(i.item_quantity * i.item_price) | |
print('\nTotal: ${}'.format(tc),end='\n') | |
def print_menu(new_cart): | |
customer_Cart = newCart | |
st = '' | |
comm = '' | |
while comm!='q': | |
print('\nMENU\n' | |
'a - Add item to cart\n' | |
'r - Remove item from cart\n' | |
'c - Change item quantity\n' | |
'i - Output items\' descriptions\n' | |
'o - Output shopping cart\n' | |
'q - Quit\n', end='\n') | |
comm = input('Choose an option:') | |
print() | |
if(comm == 'a'): | |
print("ADD ITEM TO CART") | |
item_name = input('Enter the item name:') | |
print() | |
item_description = input('Enter the item description:') | |
print() | |
item_price = int(input("Enter the item price:")) | |
print() | |
item_quantity = int(input("Enter the item quantity:")) | |
print() | |
customer_Cart.add_item(ItemToPurchase(item_name,item_price,item_quantity,item_description)) | |
elif(comm == 'o'): | |
customer_Cart.output_cart() | |
elif(comm == 'i'): | |
customer_Cart.print_descriptions() | |
elif(comm == 'r'): | |
customer_Cart.remove_item() | |
elif(comm=='c'): | |
customer_Cart.modify_item() | |
elif(comm != 'a' and comm != 'o' and comm != 'i' and comm != 'r' and comm != 'c' and comm != 'q'): | |
while(comm != 'a' and comm != 'o' and comm != 'i' and comm != 'r' and comm != 'c' and comm != 'q'): | |
comm = input("Choose an option:\n") | |
if __name__ == "__main__": | |
customer_name = input("Enter customer\'s name:"'\n') | |
current_date = input("Enter today\'s date:") | |
print() | |
print() | |
print("Customer name:", customer_name) | |
print("Today\'s date:", current_date) | |
newCart = ShoppingCart(customer_name, current_date) | |
newCart.output_cart | |
print_menu(newCart) |