In this paper, we share the example of python supermarket merchandising management system specific code for your reference, the details are as follows
Needs Analysis:Supermarket Sales Management System Features
1. Welcome users to use the supermarket sales management system, prompting the user to log in, if the user name is admin, password 123456, the administrator identity. If it is other users (you can set up their own can also be received at will), is the identity of the customer.
2. If the user is an administrator, prompt the user to enter a number to select the corresponding function
Input number "1": displays information about the product (number, product name, price).
Input No. "2": Add product information (Input No., Product Name, Price)
Input number "3": Delete product information (Input the number and delete the corresponding name and price.)
Input number "4":Exit system function
3. If the user is a customer and has only one function: to buy goods.
Display all product information, the user cycle to enter the product number and purchase quantity, enter n, exit the system, and prompt the user the total price.
Knowledge Points Involved
Programming Language Variables, Statements, Functions
Object-oriented thinking combined with a programming language for class encapsulation and method invocation
Use of common data containers lists and dictionaries
Operation of files in the program
Because it involves the reading and writing of files, it is recommended that you start the program before the first to determine the format of the file content, effective and reasonable expression of the contents of the goods, I take the following format, in the project project directory to create a folder
Logic analysis:
Add, delete, check and buy are for the operation of goods, commodity information, including the number of names and prices, you can encapsulate the information into the object, you can create the object belongs to the class Goods, and then for the operation of adding and deleting should be part of the management system, so the relevant functions encapsulated in the ShopManager class. When entering the system, you should first check whether the previous storage information, so read the file written to memory, add and delete operations are through the number as an index, so you can choose the dictionary dict data structure as a memory storage container, and then add and delete are for the operation of the dictionary, when the system exits, in the data updated to write to the file to avoid malicious modification of the file, malicious submission.
Function implementation code:
First, create the Goods class that expresses the product object
class Goods(object): def __init__(self,id,name,price): = id = name = price def __str__(self): info = "No.:%s\t Commodity Name:%s\t\t Price:%d"%(,,) return info
Put the function for the product operation into the ShopManager class, the function includes the administrator as well as the general user, after logging in the triage selection.
class ShopManager(object): def __init__(self,path): # path: indicates the path to read the file shopdic: indicates the container where the memory is stored = path = () def readFileToDic(self): # Read the file, write to the dictionary # f = open(, 'r', encoding='utf-8') clist = () () index = 0 shopdic = {} while index < len(clist): # Split the strings on each line and store them in a new list ctlist = clist[index].replace('\n', "").split("|") # Store the contents of each line in an object good = Goods(ctlist[0],ctlist[1],int(ctlist[2])) # Store pairs of directions in collections shopdic[] = good index = index + 1 return shopdic def writeContentFile(self): # Write information from memory to a file str1 = '' for key in (): good = [key] ele = +"|"++"|"+str()+"\n" str1 = str1 + ele f = open(, 'w', encoding='utf-8') (str1) () def addGoods(self): # Methods for adding products id = input("Please enter add item number:>") if (id): print("Item number already exists, please re-select!") return name = input("Please enter the name of the product to be added: >") price = int(input("Please enter the add item price:>")) good = Goods(id,name,price) [id] = good print("Added successfully!") def deleteGoods(self): # Methods for deleting products id = input("Please enter the deleted item number:>") if (id): del [id] print("Delete successful!") else: print("Item number does not exist!") def showGoods(self): # Display all product information print("="*40) for key in (): good = [key] print(good) print("="*40) def adminWork(self): info = """ ==========Welcome to the Hao Hai Oh shopping mall.========== Enter the function number,You can choose from the following functions: importation“1”:Display information about the product importation“2”:Add product information importation“3”:Deleting product information importation“4”:Exit system function ========================================== """ print(info) while True: code = input("Please enter function number:>") if code == "1": () elif code == "2": () elif code == "3": () elif code == "4": print("Thank you for using the system, it is exiting the system!!!") () break else: print("There was an error entering the number, please re-enter!!!") def userWork(self): print(" ==============Welcome to the Hao Hai Oh shopping mall.==============") print("You can shop for items by entering the number and quantity purchased, and check out by entering the number n.") () total = 0 while True: id = input("Please enter the purchase item number:>") if id == "n": print("A total of %d$ was spent on this purchase, thank you for your visit!"%(total)) break if (id): good = [id] num = int(input("Please enter the quantity purchased:>")) total = total+*num else: print("There was an error entering the item number, please check and re-enter!") def login(self): # Login function print("==========Welcome to Hao Hai Oh Shopping Mall==========") uname = input("Please enter username:>") password = input("Please enter password:>") if uname == "admin": if password == "123456": print("Welcome, administrator.") () else: print("Administrator password error, login failed!") else: print("Welcome, %s user."%(uname)) # Execute the user's purchase function ()
Finally we can call the login method in the main statement, which will automatically select the relevant function.
if __name__ == '__main__': shopManage = ShopManager("") ()
The effect that will be achieved when the above code is run is:
The above project examples unify and synthesize the learning content, and I believe that students successfully understand and knock out this part of the code.
More study materials are available on the topic ofManagement system development》。
This is the whole content of this article.