1. 程式人生 > >python 函數寫商城管理系統

python 函數寫商城管理系統

false 運行程序 程序 password 獲取用戶信息 退出 行程 () python 函數

首先我們要想到一個商城管理系統有什麽模塊?

1.登陸 2添加商品 3刪除商品 4 查詢商品 5 程序退出

但是以上模塊還是不夠,一個管理系統應該還有一個具有更高權限的管理帳號,它具備以上的功能之外 還能看誰做了什麽操作,刪除,添加用戶等等。

所以我們再添加以下模塊:

1 添加用戶 2 刪除用戶 3 修改用戶 4 監控日誌 5讀寫文件模塊

整理大致的需求後,我們需要的是一個 1。當帳號為管理員admin 登陸的情況下,他能做以下操作,添加/修改/刪除用戶 添加/查詢/刪除商品 的功能;

需要做以上功能的話 再寫函數前 我們應該寫讀文件 寫文件,寫監控日誌。下面就直接放代碼,其中常量的作用就是當你需要更改文件名時,就不用改動代碼,直接在常量更改,避免了代碼紊亂情況。

import time
USER_FILENAME = ‘users‘
#常量,存的是存儲用戶信息的文件名
LOG_FILENAME = ‘shop.log‘
#常量,存的是日誌文件名
PRODUCT_FILENAME = ‘products‘
#常量,存的是商品信息的文件名
def read_file(filename):
‘‘‘
用來讀取文件內容,返回一個字典
:param filename: 文件名
:return: 文件N內容的字典
:
‘‘‘
with open(filename,‘a+‘) as fr:
fr.seek(0)
content = fr.read()
if len(content):#這裏判斷文件內容是否為空的,如果不為0的話就為是真
return eval(content)
return {}

def write_file(filename,content):
‘‘‘
用來讀取文件內容,返回一個字典
:param filename: 文件名
:return: 文件N內容的字典
‘‘‘
with open(filename,‘a+‘) as fw:
fw.seek(0)
fw.truncate()
fw.write(str(content))

def write_log(username,operation):
‘‘‘
寫日誌函數
:param username:用戶名
:param operation:用戶的操作信息
: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)

接下來我們就先寫公共功能,登陸之後的添加商品,查詢商品,刪除商品,程序退出。除此之外,當添加商品操作進行時 我們要對商品的價格進行檢驗,避免出現負價格的情況
登陸函數:
def login():
‘‘‘
登錄函數,如果登錄成功返回登錄用戶名,登錄失敗返回None
:return:
‘‘‘
print(‘歡迎登錄白羊座商品系統‘.center(50,‘*‘))
username = input(‘請輸入用戶名:‘).strip()
password = input(‘請輸入密碼:‘).strip()
user_dic = read_file(USER_FILENAME)#獲取到所有的用戶信息
if username==‘‘ or password ==‘‘:
print(‘賬號或者密碼不能為空!‘)
else:
if username in user_dic:
if user_dic[username][‘password‘] == password: # 登錄成功
write_log(username, ‘登錄成功!‘)
return username
else:
write_log(username, ‘密碼不正確!‘)
print(‘密碼不對!‘)

else:
print(‘用戶不存在‘)

價格校驗:
def is_price(s):
‘‘‘
這個函數的作用是用來判斷價格是否合法,
:param s:
:return:
‘‘‘

s = str(s)
if s.count(‘.‘)==1:#判斷小數點個數
sl = s.split(‘.‘)#按照小數點進行分割
left = sl[0]#小數點前面的
right = sl[1]#小數點後面的
if left.startswith(‘-‘) and left.count(‘-‘)==1 and right.isdigit():
lleft = left.split(‘-‘)[1]#按照-分割,然後取負號後面的數字
if lleft.isdigit():
return False
elif left.isdigit() and right.isdigit():
#判斷是否為正小數
return True
elif s.isdigit():
s = int(s)
if s!=0:
return True
return False

添加商品:
def add_product():
products_dic = read_file(PRODUCT_FILENAME)#獲取商品信息
p_name = input(‘請輸入商品名稱:‘).strip()
p_id = input(‘請輸入商品id:‘).strip()
p_price = input(‘請輸入商品價格:‘).strip()
if p_name != ‘‘ and p_id != ‘‘ and p_price != ‘‘:
# if和elif都是條件為真的時候才走的
if p_name in products_dic:
print(‘商品已存在!‘)
elif not is_price(p_price):
# not True是flase,指定走不到這裏
# not Flase,就是true,就走這了
print(‘商品價格不合法!‘)
else:
products_dic[p_name] = {‘id‘: p_id, ‘price‘: p_price}
# products是存最新所有商品,給這個字典添加商品
write_file(PRODUCT_FILENAME,products_dic)
#調用寫文件的函數,把商品信息寫入到文件中
write_log(username,‘添加了商品信息 商品名【%s】 商品價格【%s】 商品id【%s】‘
%(p_name,p_price,p_id))
print(‘商品添加成功‘)
else:
print(‘商品名稱、商品id、商品價格都不能為空‘)

刪除商品/查詢商品以及程序退出:
def del_product():
‘‘‘
刪除商品
:return:
‘‘‘
products_dic = read_file(PRODUCT_FILENAME) # 獲取商品信息
print(‘可以刪除的有‘,products_dic.keys())
p_name = n_input(‘請輸入你要刪除的商品名稱:‘)
if p_name !=‘‘:
if p_name in products_dic:
products_dic.pop(p_name)
write_file(PRODUCT_FILENAME,products_dic)
print(‘刪除成功‘)
write_log(username,‘刪除了【%s】‘%p_name)
else:
print(‘商品名稱不存在!‘)
else:
print(‘商品名稱不能為空‘)
def query_product():
‘‘‘
查詢商品
:return:
‘‘‘
products_dic = read_file(PRODUCT_FILENAME)
p_name = n_input(‘請輸入你要查詢的商品名稱:‘)
if p_name in products_dic:
p_id = products_dic[p_name][‘id‘]
p_price = products_dic[p_name][‘price‘]
msg = ‘商品名稱是:【%s】,商品id是【%s】,商品價格是【%s】‘ % (p_name, p_id, p_price)
print(msg)
write_log(username,msg)
else:
print(‘你輸入的商品不存在!‘)
def n_exit():
exit(‘程序退出‘)
用戶admin的添加刪除修改用戶跟上面的代碼大同小異:
def add_user():
users_dic = read_file(USER_FILENAME)#獲取用戶信息
username = input(‘用戶名:‘).strip()
passwd = input(‘用戶密碼:‘).strip()
blance = input(‘用戶的錢:‘).strip()
if username != ‘‘ and passwd != ‘‘ and blance != ‘‘:
# if和elif都是條件為真的時候才走的
if username in users_dic:
print(‘用戶名已存在!‘)
elif not is_price(blance):
# not True是flase,指定走不到這裏
# not Flase,就是true,就走這了
print(‘錢不合法!‘)
else:
users_dic[username] = {‘password‘: passwd, ‘price‘: blance}
# products是存最新所有商品,給這個字典添加商品
write_file(USER_FILENAME,users_dic)
#調用寫文件的函數,把商品信息寫入到文件中
write_log(username,‘添加了用戶信息 用戶名【%s】 錢是【%s】‘
%(username,blance))
print(‘用戶添加成功‘)

def del_user():
‘‘‘
刪除用戶
:return:
‘‘‘
users_dic = read_file(USER_FILENAME) # 獲取商品信息
print(‘可以刪除的有‘, users_dic.keys())
username = n_input(‘請輸入你要刪除的用戶名:‘)
if username != ‘‘:
if username in users_dic:
if username!=‘admin‘:
users_dic.pop(username)
write_file(USER_FILENAME, users_dic)
print(‘刪除成功‘)
write_log(username, ‘刪除了【%s】‘ % username)
else:
print(‘admin用戶不能被刪除!‘)
else:
print(‘用戶不存在!‘)
else:
print(‘用戶名不能為空‘)

def modify_user():
users_dic = read_file(USER_FILENAME) # 獲取商品信息
username = n_input(‘請輸入要修改的用戶名:‘)
blance = n_input(‘請輸入你要修改的金額:‘)
passwd = n_input(‘請輸入你要修改的密碼:‘)
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,‘修改了%s用戶‘%username)
else:
print(‘用戶不存在‘)
else:
print(‘用戶名不能為空,金額和密碼至少一個不能為空!‘)

現在我們根據非admin 與admin 做映射:
manager_user_menu  = {
"1":add_user,
"2":del_user,
"3":modify_user,
"0":n_exit
}#這個用戶管理函數做的映射
product_manger = {
"1":add_product,
"2":del_product,
"3":query_product,
"0":n_exit,
}#這個是產品管理
admin_menu = {"4":manager_user}
admin_menu.update(product_manger)
#admin的菜單,為了普通用戶操作用戶管理

根據映射我們應該再寫2個函數:
def modify_user():
users_dic = read_file(USER_FILENAME) # 獲取商品信息
username = n_input(‘請輸入要修改的用戶名:‘)
blance = n_input(‘請輸入你要修改的金額:‘)
passwd = n_input(‘請輸入你要修改的密碼:‘)
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,‘修改了%s用戶‘%username)
else:
print(‘用戶不存在‘)
else:
print(‘用戶名不能為空,金額和密碼至少一個不能為空!‘)


def manager_user():
choice = n_input(‘1、添加用戶 2、刪除 3、修改用戶 0退出:‘)
if choice in manager_user_menu:
manager_user_menu[choice]()
else:
print(‘請請輸入0-3的選項!‘)

這樣 我們需要的函數都已經齊全了,現在我們就調用就行了

def welcome():
global username
username = login()#調用登錄函數,獲取登錄狀態
if username:
if username==‘admin‘:
choice = input(‘1 添加商品、2刪除商品、3查詢商品、4用戶管理、0退出‘).strip()
if choice in admin_menu:
admin_menu[choice]()
else:
print(‘請請輸入0-4的選項!‘)
else:
choice = n_input(‘1 添加商品、2刪除商品、3查詢商品、0退出:‘)
if choice in product_manger:
product_manger[choice]()
else:
print(‘請請輸入0-3的選項!‘)

welcome()#運行程序

python 函數寫商城管理系統