COMP 1005/1405A – S19 – A4 Due Saturday, June 8th at 11:59 PM 1 Assignment 4 Lists, String Operations, and Functions Submit a single zip file called assignment4.zip. This assignment has 50 marks. See...

1 answer below »
Description in the specification.


COMP 1005/1405A – S19 – A4 Due Saturday, June 8th at 11:59 PM 1 Assignment 4 Lists, String Operations, and Functions Submit a single zip file called assignment4.zip. This assignment has 50 marks. See the marking scheme that is posted on the course webpage. Assignment Background In the previous two assignment, you developed an ordering system and a sales analysis program for an electronics store. For this assignment, you will use lists to create a more detailed ordering system that will allow products to be added/removed from the stores’ catalogue, track the stock levels of products, and provide the capability to search for products. While you are free to store the data in any way you want, so long as the functions operate correctly, the assignment description will use the suggested 2D list structure included below: [[product#1 name, product#1 description, product#1 price, product#1 stock], [product#2 name, product#2 description, product#2 price, product#2 stock], etc.] This structure consists of a product list that contains a single 4-item list to represent each product’s information. As you will be using this list (or whatever storage mechanism you choose) across most/all of the function in your program, it should be defined outside of any function body. You should use string operations to perform all of the searches and name comparisons in a case-insensitive manner. This means that all of the following strings would be considered matches: “Tablet”, “tablet”, “TABLET”, “TaBLeT”, etc.. All your code for this assignment should be placed in a file call a4.py. As with the previous assignment, it is necessary to follow the naming scheme of the files/functions precisely. Note that you do not need to complete the functions in the order they are specified in this document. If I were to recommend an order, I would implement the skeleton of the menu function (problem 8) first and then add and test each of the additional functions in the following order: load inventory (problem 1), list products (problem 7), add product (problem 3), save inventory (problem 2), add product stock (problem 5), remove product (problem 4), sell product (problem 6). COMP 1005/1405A – S19 – A4 Due Saturday, June 8th at 11:59 PM 2 Problem 1 (Load Inventory Function) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file any way you like, but a suggested line-by-line structure like what was used in assignment #3 is given below: Product #1 Name Product #1 Description Product #1 Price Product #1 Stock Product #2 Name Product #2 Description Product #2 Price Product #2 Stock …etc… Problem 2 (Save Inventory Function) Add another function to the a4.py file called save_inventory(filename). This function should save all the product list information into the file with the specified name. It should also overwrite any data that is already in the file. Once you have defined both the save and load inventory functions, you will be able to load a stored information, perform some changes using the other functions, then save those changes back to the file so they are remembered when the program restarts and the file is loaded again. Problem 3 (Add Product Function) Add another function to the a4.py file called add_new_product(name, desc, price, stock). This function accepts four arguments representing the information for a new product. It should add that new product to the store’s product list only if no other product in the store’s product list has the same name (case should be ignored, so “Tablet” and “tablet” would be considered the same name). This will ensure the names of products in the store are unique, which will be important to remove ambiguity in other functions (e.g., one that removes products by name). This function must return True to indicate the product was successfully added or False to indicate the product was not added. COMP 1005/1405A – S19 – A4 Due Saturday, June 8th at 11:59 PM 3 Problem 4 (Remove Product Function) Add another function to the a4.py file called remove_product(name). This function must go through the store’s product list and remove a product from the list if that product’s name matches the given name argument (again, this should not be case sensitive). The function should return True to indicate a product was removed and False if a product was not removed. Problem 5 (Add Product Stock Function) Add another function to the a4.py file called add_product_stock(name, units). This function must search through the store’s product list to find a product with the matching name. If a matching product is found, the stock of that product in the store should be increased by the given number of units. The function should return True to indicate the stock level of a product was updated and False to indicate no updates were made (e.g., if a product was not found with that name). Problem 6 (Sell Product Function) Add another function to the a4.py file called sell_product(name, units). This function must search through the store’s product list to find a product with the matching name. If a matching product is found and the stock level of that product is at least the number of units specified (i.e., the store has that many units available to sell), then the stock level of that product should be decreased by the specified number of units. This function must return True to indicate that a product was found and the stock was updated. If a product with that name is not found or there are not enough units in stock to complete the sale, the function must return False. Problem 7 (List Products Function) Add another function to the a4.py file called list_products(keyword). This function will search through the list of products and print out those that match the given keyword argument. If the keyword is “*”, then all products should be considered a match (essentially, this prints the entire store inventory). Otherwise, a product should match the keyword if that keyword is contained either in the product name or the product description (not case sensitive, “tablet” should match “Tablet”, and “TABLET”, etc.). The printout should format the product information in an easy to read fashion that includes the name, description, price and number of units in stock. Problem 8 (System Menu) Add a final function to the a4.py file called main(). This function must print out a menu with the 9 options detailed in the table below and allow the user to repeatedly enter choices until they chose to quit. An example of what the output might look like is included on the assignment page in the menu-demo.txt file. COMP 1005/1405A – S19 – A4 Due Saturday, June 8th at 11:59 PM 4 Option Number Option Text Description 1 Load Inventory Asks the user for a filename and then uses the load_inventory function to perform the load operation. 2 Save Inventory Asks the user for a filename and then uses the save_inventory function to perform the save operation. 3 Add New Product Ask the user to enter the four pieces of product information and then use the add_new_product function to add the product to the catalogue. Should print out a message indicating whether the product was added successfully or not (use the return value of the function to decide). 4 Add Product Stock Ask the user to enter the product name and a number of units, then use the add_product_stock function to update the stock levels of the product. Should print out a message indicating whether the update was successful or not (use the return value of the function to decide). 5 List Products Use the list_products function to print all the products in the store catalogue. 6 Search Products Ask the user to enter a keyword to search for. Use the list_products function to print any matching products. 7 Sell Products Ask the user for a product name and a number of units to sell. Use the sell_product function to perform the sell operation. Should print out a message indicating whether the sale was successful or not (use the return value of the function to decide). 8 Remove Products Ask the user for a product name and then use the remove_product function to take the product out of the store inventory. Should print out a message indicating whether the removal was successful or not (use the return value of the function to decide). 9 Quit Exits the loop and the program stops. Desktop Computer 3Ghz, 16GB RAM, 250GB Hard Drive 1200 10 Laptop Computer 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen 1500 15 This is example output created by the main() function when it is run. The inventory.txt file that is loaded in this example is included on the assignment. You can use it as a starting point for your own file or create your own by adding products through the menu. Choose an option below to proceed: 1. Load Inventory 2. Save Inventory 3. Add New Product 4. Add Product Stock 5. List Products 6. Search Products 7. Sell Products 8. Remove Product 9. Quit >5 There are no products that match. Choose an option below to proceed: 1. Load Inventory 2. Save Inventory 3. Add New Product 4. Add Product Stock 5. List Products 6. Search Products 7. Sell Products 8. Remove Product 9. Quit >1 Enter the file name to load from:inventory.txt Choose an option below to proceed: 1. Load Inventory 2. Save Inventory 3. Add New Product 4. Add Product Stock 5. List Products 6. Search Products 7. Sell Products 8. Remove Product 9. Quit >5 1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock 2. Laptop Computer, 2
Answered Same DayJun 04, 2021COMP1005

Answer To: COMP 1005/1405A – S19 – A4 Due Saturday, June 8th at 11:59 PM 1 Assignment 4 Lists, String...

Shivinder answered on Jun 08 2021
160 Votes
# -*- coding: utf-8 -*-
import re
inv_info = []
def load_inventory(filename):

global inv_info
file = open(filename, 'r')
inv_val = file.read().splitlin
es()
file.close()
inv_info = [inv_val[x:x+4] for x in range(0, len(inv_val), 4)]
print('Inventory details loaded successfully from file {}'.format(filename))

def save_inventory(filename):

global inv_info

file = open(filename, 'a')
for inv in inv_info:
for detail in inv:
file.write(detail+'\n')
print('Inventory details saved successfully in file {}'.format(filename))
file.close()
def add_new_product(name, desc, price, stock):

global inv_info

for inv in inv_info:
if name.strip().lower() == inv[0].lower():
return False

inv_info.append([name, desc, str(price), str(stock)])
return True

def add_product_stock(name, units):

global inv_info

found = False
for inv in inv_info:
if name.strip().lower() == inv[0].lower():
index_val = inv_info.index(inv)
found = True

if found == True:
inv_info[index_val][3] = str(int(inv_info[index_val][3])+units)

return found

def list_products(inv_info):

if len(inv_info) > 0:

for pos, i in enumerate([' '.join(i) for i in inv_info]):
if int(inv_info[pos][3]) > 0:
print('{}. {} units in stock'.format(pos+1,i))

else:
print('There are no products that match.')

def search_product(keyword):

global inv_info

index_val = []
found = False
for inv in inv_info:
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here