Python function write Mall management system

Source: Internet
Author: User

First of all we have to think of a mall management system what module?

1. Login 2 Add Item 3 Delete Item 4 query Item 5 program exit

But the above module is not enough, a management system should have a higher authority of the management account, it has the above functions can also see who did what operations, delete, add users and so on.

So let's add the next module:

1 Add user 2 Delete User 3 Modify User 4 monitoring log 5 read-write file module

After finishing the rough demand, what we need is a 1. When the account for admin admin login, he can do the following actions, add/modify/delete the user Add/query/delete products function;

If you need to do the above functions, we should write the file and write the monitoring log before we write the function. The following directly put the code, where the function of the constant is when you need to change the file name, you do not have to change the code, directly in the constant changes, to avoid the code chaos situation.

Import time
User_filename = ' users '
#常量, the file name where the user information is stored is saved
Log_filename = ' Shop.log '
#常量, the log file name is stored
Product_filename = ' products '
#常量, the file name of the product information is stored
def read_file (filename):
‘‘‘
Used to read the contents of a file and return a dictionary
:p Aram FileName: file name
: return: Dictionary of file N content
:
‘‘‘
with open (filename, ' A + ') as fr:
Fr.seek (0)
Content = Fr.read ()
If Len (content): #这里判断文件内容是否为空的, if it's not 0, it's true.
Return eval (content)
return {}

def write_file (filename,content):
‘‘‘
Used to read the contents of a file and return a dictionary
:p Aram FileName: file name
: return: Dictionary of file N content
‘‘‘
with open (filename, ' A + ') as FW:
Fw.seek (0)
Fw.truncate ()
Fw.write (str (content))

def write_log (username,operation):
‘‘‘
Write log function
:p Aram Username: User Name
:p Aram Operation: User's Operation information
: return:
‘‘‘
W_time = Time.strftime ('%y-%m-%d%h%m%s ')
With open (Log_filename, ' A + ') as FW:
log_content = '%s%s%s \ n '% (w_time,username,operation)
Fw.write (log_content)

Next we will write the public function, after landing the addition of goods, query products, delete products, program exit. In addition, when the addition of the operation of the goods we have to check the price of the goods, to avoid negative price situation
Login function:
def login ():
‘‘‘
Login function, logon failure returns none if login successfully returns login user name
: return:
‘‘‘
Print (' Welcome to Aries Product System '. Center (50, ' * '))
Username = input (' Please enter user name: '). Strip ()
Password = input (' Please enter password: '). Strip ()
User_dic = Read_file (user_filename) #获取到所有的用户信息
If username== ' or password = = ':
Print (' Account or password cannot be empty! ‘)
Else
If username in User_dic:
If user_dic[username][' password '] = = password: # Login Successful
Write_log (username, ' Login successful! ‘)
return username
Else
Write_log (username, ' Password is incorrect! ‘)
Print (' Wrong password! ‘)

Else
Print (' User not present ')

Price check:
 def is_price (s): 
' '
This function is used to determine whether the price is legitimate,
:p Aram S:
: return:
"'

s = str (s)
If S.count ('. ') ==1: #判断小数点个数
SL = S.split ('. ') #按照小数点进行分割
left = sl[0] #小数点前面的
right = sl[1] #小数点后面的
If Left.startswith ('-') and Left.coun T ('-') ==1 and Right.isdigit ():
Lleft = Left.split ('-') [1] #按照-split, then take the number following the minus sign
if Lleft.isdigit ( ):
return False
Elif left.isdigit () and Right.isdigit ():
#判断是否为正小数
return True
Elif s.isdigit ():
s = Int (s)
if s!=0:
return True
Retu RN False

Add Product:
 def add_product (): 
Products_dic = Read_file (product_filename) #获取商品信息
P_name = input (' Please enter the product Name: '). Strip ()
p_id = input (' Enter Product ID: '). Strip ()
P_price = input (' Please enter commodity price: '). Strip ()
if p_name! = ' and p_id! = ' and P_price! = ':
# If and elif are all when the condition is true
if p_name in Products_dic:
Print (' Product already exists! ')
Elif not Is_price (p_price):
# is not true is flase, the specified cannot go here
# not flase, that is true, it's gone print (' Commodity price is not legal! ')
Else:
Products_dic[p_name] = {' id ': p_id, ' Price ': p_price}
# product is the latest store of all products , add the Product
Write_file (product_filename,products_dic)
#调用写文件的函数 to the dictionary, and write the product information to the file
WR Ite_log (username, ' added product information trade name '%s ' Commodity price '%s ' Product ID '%s ' '
% (p_name,p_price,p_id)
Print (' product added successfully ')
Else:
Print (' Product name, Product ID, commodity price cannot be null ')

Delete Item/query item and program exit:
Def del_product ():
‘‘‘
Delete Item
: return:
‘‘‘
Products_dic = Read_file (product_filename) # Get product info
Print (' can be removed with ', Products_dic.keys ())
P_name = n_input (' Please enter the name of the product you want to delete: ')
If p_name! = ":
If P_name in Products_dic:
Products_dic.pop (P_name)
Write_file (Product_filename,products_dic)
Print (' delete succeeded ')
Write_log (username, '%s ' removed '%p_name)
Else
Print (' Product name does not exist! ‘)
Else
Print (' Product name cannot be empty ')
Def query_product ():
‘‘‘
Product Enquiry
: return:
‘‘‘
Products_dic = Read_file (product_filename)
P_name = n_input (' Please enter the name of the product you want to query: ')
If P_name in Products_dic:
p_id = products_dic[p_name][' id ']
P_price = products_dic[p_name][' price ']
msg = ' Product name is: '%s ', Product ID is '%s ', commodity price is '%s '% (P_name, p_id, P_price)
Print (msg)
Write_log (USERNAME,MSG)
Else
Print (' The item you entered does not exist! ‘)
Def n_exit ():
Exit (' program exit ')
User admin Add Delete modify user is similar to the above code:
Def add_user ():
Users_dic = Read_file (user_filename) #获取用户信息
Username = input (' User name: '). Strip ()
passwd = input (' User password: '). Strip ()
Blance = input (' User's money: '). Strip ()
If username! = "and passwd! =" and blance! = ":
# if and elif are the conditions are true when the time to go
If username in Users_dic:
Print (' User name already exists! ‘)
Elif not Is_price (blance):
# not true is Flase, specify not to go here
# not Flase, that's true, that's it.
Print (' Money is illegal! ‘)
Else
Users_dic[username] = {' Password ': passwd, ' Price ': blance}
# products are all the latest products, add items to this dictionary
Write_file (User_filename,users_dic)
#调用写文件的函数, write the product information to the file
Write_log (username, ' added user information user name '%s ' money is '%s '
% (username,blance))
Print (' user added success ')

Def del_user ():
‘‘‘
Delete User
: return:
‘‘‘
Users_dic = Read_file (user_filename) # Get product info
Print (' can be removed with ', Users_dic.keys ())
Username = n_input (' Please enter the user name you want to delete: ')
If username! = ":
If username in Users_dic:
If username!= ' admin ':
Users_dic.pop (username)
Write_file (User_filename, Users_dic)
Print (' delete succeeded ')
Write_log (username, ' deleted '%s '% username)
Else
Print (' Admin user cannot be deleted! ‘)
Else
Print (' user does not exist! ‘)
Else
Print (' User name cannot be null ')

Def modify_user ():
Users_dic = Read_file (user_filename) # Get product info
Username = n_input (' Please enter the user name to be modified: ')
Blance = n_input (' Please enter the amount you want to modify: ')
passwd = n_input (' Please enter the password you want to change: ')
If username!= ' and (blance!= ' or passwd!= '):
If username in Users_dic:
If blance!= ':
users_dic[username][' Moeny ']=blance
Elif passwd!= ':
users_dic[username][' password '] = passwd
Else
users_dic[username][' money '] = blance
users_dic[username][' password '] = passwd
Write_file (user_filename,users_dic) #写用户信息
Write_log (username, ' modified%s user '%username)
Else
Print (' User not present ')
Else
Print (' username cannot be empty, amount and password at least one cannot be empty! ‘)

Now we do the mapping based on non-admin and admin:
Manager_user_menu  = {
"1": Add_user,
"2":d El_user,
"3": Modify_user,
"0": n_exit
} #这个用户管理函数做的映射
Product_manger = {
"1": Add_product,
"2":d el_product,
"3": Query_product,
"0": N_exit,
} #这个是产品管理
Admin_menu = {"4": Manager_user}
Admin_menu.update (Product_manger)
#admin的菜单, user management for normal user operation

According to the mapping we should write 2 more functions:
Def modify_user ():
Users_dic = Read_file (user_filename) # Get product info
Username = n_input (' Please enter the user name to be modified: ')
Blance = n_input (' Please enter the amount you want to modify: ')
passwd = n_input (' Please enter the password you want to change: ')
If username!= ' and (blance!= ' or passwd!= '):
If username in Users_dic:
If blance!= ':
users_dic[username][' Moeny ']=blance
Elif passwd!= ':
users_dic[username][' password '] = passwd
Else
users_dic[username][' money '] = blance
users_dic[username][' password '] = passwd
Write_file (user_filename,users_dic) #写用户信息
Write_log (username, ' modified%s user '%username)
Else
Print (' User not present ')
Else
Print (' username cannot be empty, amount and password at least one cannot be empty! ‘)


Def manager_user ():
Choice = N_input (' 1, add user 2, delete 3, modify user 0 exit: ')
If choice in Manager_user_menu:
Manager_user_menu[choice] ()
Else
Print (' Please enter the 0-3 option! ‘)

So we've got all the functions we need, and now we're going to call it.

Def welcome ():
Global username
Username = login () #调用登录函数, get login status
If username:
If username== ' admin ':
Choice = input (' 1 Add Item, 2 delete item, 3 query item, 4 user management, 0 exit '). Strip ()
If choice in Admin_menu:
Admin_menu[choice] ()
Else
Print (' Please enter the 0-4 option! ‘)
Else
Choice = N_input (' 1 Add Item, 2 delete item, 3 query item, 0 exit: ')
If choice in Product_manger:
Product_manger[choice] ()
Else
Print (' Please enter the 0-3 option! ‘)

Welcome () #运行程序

Python function write Mall management system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.