Apr-28-2020, 04:12 PM
The inventory of the items for sale is growing and will continue to do so. It’s now no
longer practical to change the code’s internals every time an item is added (or deleted) from the system.
Instead, another group will provide the inventory items in a file that the program will read and use to populate
the category items. Each category will have its own file that contains the list of items in that category.
For example, instead of only displaying three items in the book category, a list of book descriptions and their
prices will be given in a text file (-.txt). The program will read and use that file to build its internal “book_list”
(list or tuple) and book category display menu. The items will be given one per line, each line consisting of the
item description, followed by the item’s price. A comma (“,”) will separate the description from the price. A
sample -.txt file for the electronics category follows:
LinkSys Router, 49.95
HP Laptop, 350.00
Altec Lansing Speakers, 195.95
EyePhone 10,795.00
First Alert Smoke Alarm, 29.95
LG 55 UDTV,350.00
Sony Prtable Radio, 15.00
Dell All-in-One PC, 495.00
Brother Laser Printer, 99.00
The number of items in each category will vary, although they will all fit on one screen (assume no more than
20 items per category). An item menu for clothing would look like this:
<start menu>
Clothing menu
Select from the following items, display cart, or checkout:
No. Item Description Price
=== =========================== ========
1 - Vasque Hiking Boots $ 109.00
2 - Wool Hat $ 14.00
3 - Pants $ 39.95
4 - Wrangler Jeans $ 24.50
5 - Armani Suit $ 800.00
6 - Nike T-shirt $ 19.00
7 - New Balance Trail Runners $ 69.95
8 - Gore-Tex Gloves $ 39.00
9 - North Face Fleece Jacket $ 89.95
10 - Nationals Logo Sweatshirt $ 49.00
d - display cart contents
x - return to category menu
Enter Selection (1 to 10, "d", or "x"):
<end menu>
The number of categories for this assignment will remain at three. One file for each category will be provided
for testing, although the GTA will use other files for evaluating the submissions. The files will be named:
books.txt
electronics.txt
clothing.txt
Additional instructions will be given on how your program will access those files. For now, I suggest you
assign a variable name inside your program for each file and assign the filename to that string. Then open the
file by referring to that string. For example:
infile = ‘C:/Files/books.txt’
fi = open(infile,"r")
itemfile = fi.readlines() # items = list of item descriptions + price
‘C:/Files/books.txt’ is the fully qualified name of the file as it exists on your computer. “books.txt” should be
the same, but the “C:/Files/” is a prefix that will be specific to your system. As an alternative, you can prompt
the user to give the file name as an input string and use that. I will be showing a function to handle some of this
later today or during the next class.
Besides using the basic open and read functions (“readlines()” is suggested), you will have to use the string
“split” method shown in class to separate the item description and price and assign them to two separate
variables. You should also use the “strip” method to remove extraneous leading and trailing spaces and newline
characters.
longer practical to change the code’s internals every time an item is added (or deleted) from the system.
Instead, another group will provide the inventory items in a file that the program will read and use to populate
the category items. Each category will have its own file that contains the list of items in that category.
For example, instead of only displaying three items in the book category, a list of book descriptions and their
prices will be given in a text file (-.txt). The program will read and use that file to build its internal “book_list”
(list or tuple) and book category display menu. The items will be given one per line, each line consisting of the
item description, followed by the item’s price. A comma (“,”) will separate the description from the price. A
sample -.txt file for the electronics category follows:
LinkSys Router, 49.95
HP Laptop, 350.00
Altec Lansing Speakers, 195.95
EyePhone 10,795.00
First Alert Smoke Alarm, 29.95
LG 55 UDTV,350.00
Sony Prtable Radio, 15.00
Dell All-in-One PC, 495.00
Brother Laser Printer, 99.00
The number of items in each category will vary, although they will all fit on one screen (assume no more than
20 items per category). An item menu for clothing would look like this:
<start menu>
Clothing menu
Select from the following items, display cart, or checkout:
No. Item Description Price
=== =========================== ========
1 - Vasque Hiking Boots $ 109.00
2 - Wool Hat $ 14.00
3 - Pants $ 39.95
4 - Wrangler Jeans $ 24.50
5 - Armani Suit $ 800.00
6 - Nike T-shirt $ 19.00
7 - New Balance Trail Runners $ 69.95
8 - Gore-Tex Gloves $ 39.00
9 - North Face Fleece Jacket $ 89.95
10 - Nationals Logo Sweatshirt $ 49.00
d - display cart contents
x - return to category menu
Enter Selection (1 to 10, "d", or "x"):
<end menu>
The number of categories for this assignment will remain at three. One file for each category will be provided
for testing, although the GTA will use other files for evaluating the submissions. The files will be named:
books.txt
electronics.txt
clothing.txt
Additional instructions will be given on how your program will access those files. For now, I suggest you
assign a variable name inside your program for each file and assign the filename to that string. Then open the
file by referring to that string. For example:
infile = ‘C:/Files/books.txt’
fi = open(infile,"r")
itemfile = fi.readlines() # items = list of item descriptions + price
‘C:/Files/books.txt’ is the fully qualified name of the file as it exists on your computer. “books.txt” should be
the same, but the “C:/Files/” is a prefix that will be specific to your system. As an alternative, you can prompt
the user to give the file name as an input string and use that. I will be showing a function to handle some of this
later today or during the next class.
Besides using the basic open and read functions (“readlines()” is suggested), you will have to use the string
“split” method shown in class to separate the item description and price and assign them to two separate
variables. You should also use the “strip” method to remove extraneous leading and trailing spaces and newline
characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
import os more_carts = "y" carts = 0 cart_items = 0 cart_cost = 0.0 session_carts = 0 session_items = 0 session_cost = 0.0 menu = """ 1 - Books 2 - Electronics 3 - Clothes d - show cart contents c - Checkout Select one of the categories or checkout (1 – 3 or ‘c’ or 'd'): """ 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 = [] book_list = [[ "Origin" , 19.95 ], [ "Grant" , 24.50 ], [ "Prairie Fires" , 18.95 ]] elect_list = [[ "HP Laptop" , 429.50 ],[ "EyePhone 10" , 790.00 ],[ "Bose 20 Speakers" , 220.00 ]] clothing_list = [[ "T-shirt" , 9.50 ],[ "Shoes" , 45.00 ],[ "Pants" , 24.00 ]] more_items = "y" while more_items = = "y" : os.system( 'cls' ) category = input (menu) if category = = "c" : break elif category = = "d" : print ( "shopping cart " ,cart) elif category not in "123" : print ( "Invalid category selected" ) continue item_pic = " " if int (category) = = 1 : item_pic = " " while item_pic ! = "x" : os.system( 'cls' ) item_pic = input (book_menu) if item_pic in ( "1" , "2" , "3" ): 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 item_pic ! = "x" : print ( "Invalid item selected" ) elif category = = "2" : item_pic = " " while item_pic ! = "x" : os.system( 'cls' ) item_pic = input (elect_menu) if item_pic in ( "1" , "2" , "3" ): 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 item_pic ! = "x" : print ( "Invalid item selected" ) elif category = = "3" : item_pic = " " while item_pic ! = "x" : os.system( 'cls' ) item_pic = input (clothing_menu) if item_pic in ( "1" , "2" , "3" ): 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) elif item_pic ! = "x" : print ( "Invalid item selected" ) 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" ) |