Python Forum
project shopping cart
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
project shopping cart
#1
I need to do three enhancement on this program and i'm not sure where to start the enhancement are
(2) Enhancement 2. The quantity of each item selected will be tracked and displayed. When an item is
selected for the cart the quantity is recorded as 1. However, when the item is selected again for inclusion in the
same cart, the program will detect that it is a duplicate of an already selected item. Instead of adding the
duplicate to the cart, the item already in the cart will have its quantity incremented (add one to it). That means
whenever an item is added to the cart the program must compare the item to those already in the cart and either
add the item to the cart or just increment the quantity of the item already there.

(3) Enhancement 3. The ‘display cart’ and ‘checkout’ features are to each be implemented as functions. Given
enhancement (2), they will have to display the item quantity and only include at most one line for each item
selected. An item’s total cost is to be displayed and can be computed using the ‘QTY x price’ formula.

(4) Enhancement 4. An activity log file will be created in which a summary of each cart and session will be
recorded. At the end of each checkout summary a record of the cart’s contents will be written to a log file as a
string with individual fields separated as a ‘;’ (semi-colon). A similar log record must be written at the end of
the entire session, when no more carts are to be processed. The log record to be written is to include these
fields:
A date/time stamp (see below on how to do this).
Type of log record: either “cart” or “session”
Cart number: starts at zero at the beginning of a session and is increments for each new cart, or number of carts
in the session summary log record.
Total number of items for the cart or entire session.
Total cost of the items for the cart or entire session

My Code
import os # include this import only if not done elsewhere in your code

more_carts = "y"
carts =0
cart_items = 0
cart_cost = 0.0
session_carts = 0
session_items = 0
session_cost = 0.0

def itemMenu (category, itemList):
    """itemMenu function - displays menu of variable number of shopping items.
       Inputs: category (books, etc.), list of item descriptions and prices.
       Returns: selected menu item (integer, 1 to n), d, or x
       ---------------------------------------------------------------------"""  
    os.system('cls')
    print ('\n\n\t\t ' + category + ' menu')
    print ('\n\t\t Select from the following items, display cart, or checkout: ')
    print('\n\t\t\t {0:3s}  {1:26s}  {2:8s}'.format('No.', 'Item Description', 'Price '))
    print('\t\t\t {0:3s}  {1:26s} {2:8s}'
          .format('===', '===========================', '========'))
    for n in range(0, len(itemList)):
        print('\t\t\t {0:>2s} - {1:26s}  ${2:8.2f}'.format(str(n+1), itemList[n][0], itemList [n][1]))
    print('\n\t\t\t {0:>2s} - {1:26s} '.format('d',  'display cart contents '))
    print('\t\t\t {0:>2s} - {1:26s} '.format('x', 'return to category menu '))
    menuPic = input('\n\nEnter Selection (1 to {0:>2s}, "d", or "x"): '.format(str(len(itemList))))
    return menuPic 

# Only cut/past the above code.  The code below does a basic demo of using
# the above function.  There is no reason to include it in your code.

category_menu = """\n\n
             Select an item from the following menu or checkout:
             
               1 = Books 
               2 = electronics
               3 = clothing
               d = show cart contents
               c = checkout
        
        """

# category items tuples - one per category --------------------------------
#------------books
infile = "books.txt"
fi = open(infile,"r")
book_item = {}
for line in fi:
    list1 = line.split(",")
    des = list1[0]
    price = list1[1]
    book_item[des] = float(price.strip())
fi.close()
book_list = []
for i in book_item:
    k = (i,book_item[i])
    book_list.append(k)
count = []
for x in range(len(book_list)):
    count.append(x)

#----------electronic   
infile1 = "electronics.txt"
fi = open(infile1,"r")
electronic_item = {}
for line in fi:
    list1 = line.split(",")
    des = list1[0]
    price = list1[1]
    electronic_item[des] = float(price.strip())
fi.close()
elect_list = []
for i in electronic_item:
    k = (i,electronic_item[i])
    elect_list.append(k)
count2 = []
for q in range(len(elect_list)):
    count2.append(q)
    
#-----------clothes    
infile2 = "clothing.txt"
fi = open(infile1,"r")
clothing_item = {}
for line in fi:
    list1 = line.split(",")
    des = list1[0]
    price = list1[1]
    clothing_item[des] = float(price.strip())
fi.close()
cloth_list = []
for i in clothing_item:
    k = (i,electronic_item[i])
    cloth_list.append(k)
count3 = []
for v in range(len(cloth_list)):
    count3.append(v)
    
# start of active code*********************************
while more_carts == "y":
    books_items = 0
    books_cost = 0.0
    elect_items = 0
    elect_cost = 0.0
    clothing_items = 0
    clothing_cost = 0.0
    cart = []

    more_items = "y"
    while more_items == "y":
        os.system('cls')
        print(category_menu)
        category = input('Select category (1 - 3), d, or c :')
        if category == "c":
            break
        elif category == "d":
            print("shopping cart ",cart)
        elif category not in "123":
            print("Invalid category selected")
            continue
        
        if category == '1':
            item_pic = "1"
            while item_pic != "x":
                os.system('cls')
                item_pic = itemMenu('Books', book_list)
                if item_pic in str(count):
                    response = input("Add "+book_list[int(item_pic)-1][0]+" y/n? ")
                    if response == "y":
                        cart.append(book_list[int(item_pic)-1])
                        print("shopping cart = ", cart)
                    else:
                        continue
                elif item_pic == "d":
                    print("shopping cart = ",cart)

                    
        elif category == '2':
            item_pic = "1"
            while item_pic != "x":
                os.system('cls')
                item_pic = itemMenu('Electronics', elect_list)
                if item_pic in str(count2):
                    response = input("Add "+elect_list[int(item_pic)-1][0]+" y/n? ")
                    if response == "y":
                        cart.append(elect_list[int(item_pic)-1])
                        print("shopping cart = ", cart)
                    else:
                        continue
                elif item_pic == "d":
                    print("shopping cart = ",cart)
              
                    
        elif category == "3":
               item_pic = "1"
               while item_pic != "x":
                   os.system('cls')
                   item_pic = itemMenu('clothing', cloth_list)
                   if item_pic in str(count3):
                       response = input("Add "+clothing_list[int(item_pic) -1][0]+" y/n? ")
                       if response == "y":
                           cart.append(clothing_list[int(item_pic)-1])
                           print("shopping cart = ", cart)
                       else:
                           continue
                   elif item_pic == "d":
                        print("shopping cart = ",cart)
#------------------------------------------------
    if category == "c":
        print("\nCheckout Selected.....")
        
    if len(cart) > 0:
        cart_total_cost = 0.0
        print("\n\t{0:20s}\t\t{1:4s}".format("items", "cost"))
        print("\t{0:20s}\t\t{1:9s}".format("====================","========="))
        for items in cart:
            print("\t{0:20s}\t\t${1:8.2f}".format(items[0],items[1]))
            cart_total_cost += items[1]
        print("\nTotal number of items: {0:5d}    cost: ${1:8.2f}".format(len(cart), cart_total_cost))
        session_carts += 1
        session_items += len(cart)
        session_cost +=cart_total_cost
    else:
        print("\nCart is empty\n")
        
more_carts = input("\nAre there more carts to process (y/n)?\n")
print("\n\n\tTotal number of carts: ", session_carts)
print("\tTotal number of items: ",session_items)
print("\tTotal cost of items: $%8.2f" %(session_cost))
input ("\n\nHit Enter to end program")
Reply
#2
i was able to create a dictionary for the cart - {itemName}: (price, qty)
how do i get access to individual value like just price or qty
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Shopping cart program Tofuboi03 7 18,837 Sep-07-2023, 09:41 AM
Last Post: shoesinquiry

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020