Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. import collections
  2.  
  3. ItemToPurchase = collections.namedtuple('ItemToPurchase', 'item_name item_quantity item_price item_description')
  4.  
  5. def construct_item():
  6. global new_item
  7. new_item = ItemToPurchase(item_name = 'none', item_quantity = 0, item_price = 0, item_description = 'none' )
  8. #print(new_item)
  9. return new_item
  10.  
  11. def print_item_cost(new_item1,new_item2):
  12. total1 = new_item1[1] * new_item1[2]
  13. total2 = new_item2[1] * new_item2[2]
  14. print()
  15. print('TOTAL COST')
  16. print('%s %.0f @ $%d = $' % new_item1[0:3] + str(int(total1)))
  17. print('%s %.0f @ $%d = $' % new_item2[0:3] + str(int(total2)))
  18. print()
  19. added_total = int(total1) + int(total2)
  20. print('Total: $%d' % added_total)
  21.  
  22. #def print_item_description():
  23. # print('%s : %s' % new_item1[0] + newitem1[4])
  24.  
  25. ShoppingCart = collections.namedtuple('ShoppingCart', 'customer_name current_date cart_items')
  26. def construct_cart():
  27. global ShoppingCart
  28. ShoppingCart = ShoppingCart(customer_name = 'none', current_date = 'January 1, 2016', cart_items = [])
  29. return ShoppingCart
  30.  
  31. def print_menu():
  32. menu = '''MENU
  33. a - Add item to cart
  34. r - Remove item from cart
  35. c - Change item quantity
  36. i - Output items\' descriptions
  37. o - Output shopping cart
  38. q - Quit'''
  39. print(menu)
  40. print()
  41. menu_option = input('Choose an option:\n')
  42.  
  43. while menu_option != 'q':
  44. if menu_option == 'a':
  45. add_item(ShoppingCart,ItemToPurchase)
  46. print()
  47. print(menu)
  48. print()
  49. elif menu_option == 'o':
  50. print_total(ShoppingCart)
  51. print(menu)
  52. print()
  53.  
  54. menu_option = input('Choose an option:\n')
  55.  
  56. def add_item(ShoppingCart, ItemToPurchase):
  57. new_item = construct_item()
  58. cart_items = ShoppingCart[2]
  59. print('ADD ITEM TO CART')
  60. add_name = input('Enter the item name:\n')
  61. add_description = input('Enter the item description:\n')
  62. add_price = int(input('Enter the item price:\n'))
  63. add_quantity = int(input('Enter the item quantity:\n'))
  64. new_item = new_item._replace(item_name = add_name, item_price = add_price, item_quantity = add_quantity, item_description = add_description)
  65. cart_items.append(new_item)
  66. #print(ShoppingCart)
  67.  
  68. def print_total(ShoppingCart):
  69. cart_items = ShoppingCart[2]
  70. print('OUTPUT SHOPPING CART')
  71. print("%s's Shopping Cart - %s" % (ShoppingCart[0],ShoppingCart[1]))
  72. if cart_items == []:
  73. print('Number of Items: %d\n' % len(ShoppingCart[2]))
  74. print('SHOPPING CART IS EMPTY\n')
  75. print('Total: $0\n')
  76. else:
  77. print('Number of Items: %d\n' % cart_items[0][1])
  78. #FIXME Write a For loop so that it goes through all the carts items.
  79. total = cart_items[0][1] * cart_items[0][2]
  80. print('%s %d @ $%d = $' % cart_items[0][0:3] + str(total))
  81. print()
  82. print('Total: $%d' % total)
  83. print()
  84.  
  85. if __name__ == "__main__":
  86. # Type main section of code here
  87. #print_item_cost(new_item)
  88. new_customer = input('Enter customer\'s name:\n')
  89. date = input('Enter today\'s date:\n')
  90. construct_cart()
  91. ShoppingCart = ShoppingCart._replace(customer_name = new_customer, current_date = date)
  92. #print(ShoppingCart)
  93. print()
  94. print('Customer name:', new_customer)
  95. print('Today\'s date:', date)
  96. print()
  97. print_menu()
  98. '''
  99. new_item1 = construct_item()
  100. new_item2 = construct_item()
  101. print('Item 1')
  102. item_name1 = input('Enter the item name:\n')
  103. item_price1 = float(input('Enter the item price:\n'))
  104. item_quantity1 = int(input('Enter the item quantity:\n'))
  105. new_item1 = new_item1._replace(item_name = item_name1, item_price = item_price1, item_quantity = item_quantity1)
  106. print()
  107. print('Item 2')
  108. item_name2 = input('Enter the item name:\n')
  109. item_price2 = float(input('Enter the item price:\n'))
  110. item_quantity2 = int(input('Enter the item quantity:\n'))
  111. new_item2 = new_item1._replace(item_name = item_name2, item_price = item_price2, item_quantity = item_quantity2)
  112. print_item_cost(new_item1,new_item2)
  113. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement