python写酒店管理系统报告_酒店管理系统学生工作-部门管理Python,作品,python
# Author: cybersnow
# 開發時間:2020-17-07
# 開發工具:PyCharm
import re # 導入正則表達式模塊
import os # 導入操作系統模塊
database_filename = "cyberwinhotel.txt" # 部門信息保存文件
def menu():
# 輸出菜單
print('''
---------------酒店息管理系統------------
==================功能菜單================
1 錄入部門信息
2 查找部門信息
3 刪除部門信息
4 修改部門信息
5 排序
6 統計部門總數
7 顯示所有部門信息
0 退出系統
=======================================
說明:通過數字選擇菜單
=======================================
''')
def main():
ctrl = True # 標記是否退出系統
while (ctrl):
menu() # 顯示菜單
option = input("請選擇:") # 選擇菜單項
option_str = re.sub("\D", "", option) # 提取數字
if option_str in ['0', '1', '2', '3', '4', '5', '6', '7']:
option_int = int(option_str)
if option_int == 0: # 退出系統
print('您已退出酒店管理系統!')
ctrl = False
elif option_int == 1: # 錄入酒店成績信息
insert()
elif option_int == 2: # 查找酒店成績信息
search()
elif option_int == 3: # 刪除酒店成績信息
delete()
elif option_int == 4: # 修改酒店成績信息
modify()
elif option_int == 5: # 排序
sort()
elif option_int == 6: # 統計酒店總數
total()
elif option_int == 7: # 顯示所有酒店信息
show()
'''錄入酒店信息'''
def insert():
depList = [] # 保存酒店信息的列表
mark = True # 是否繼續添加
while mark:
id = input("請輸入部門ID(如1):")
if not id:
break
dep_name = input("請輸入部門名稱:")
if not dep_name:
break
# 將輸入的幾點信息保存到字典
deptone = {"id": id, "dep_name": dep_name}
depList.append(deptone) # 將部門字典添加到列表中
inputList = input("是否繼續添加?(y/n):")
if inputList == 'y': # 繼續添加
mark = True
else:
mark = False
save(depList) # 將部門信息保存到文件
print("部門信息錄入完畢!!!")
'''將部門信息保存到文件'''
def save(depList):
try:
deptlist_txt = open(database_filename, 'a') # 以追加模式打開
except Exception as e:
deptlist_txt = open(database_filename, 'w') # 文件不存在,創建文件并打開
for info in depList:
deptlist_txt.write(str(info) + "\n") # 執行存儲,添加換行符
deptlist_txt.close() # 關閉文件
'''查詢部門信息'''
def search():
mark = True
dept_query = []
while mark:
id = ""
dep_name = ""
if os.path.exists(database_filename):
mode = input("按ID查詢輸入1:按名稱查詢輸入2:")
if mode == "1":
id = input("請輸入部門ID:")
elif mode == "2":
dep_name = input("請輸入部門名稱:")
else:
print("您輸入有誤,請重新輸入!")
search()
with open(database_filename, "r") as file:
deplistr = file.readlines()
for list in deplistr:
d = dict(eval(list))
if id is not "":
if d['id'] == id:
dept_query.append(d)
elif dep_name is not "":
if d['dep_name'] == dep_name:
dept_query.append(d)
show_dept(dept_query)
dept_query.clear()
inputMark = input("是否繼續查詢?(y/n):")
if inputMark == "y":
mark = True
else:
mark = False
else:
print("暫未保存數據信息...")
return
'''將保存在列表中的部門信息顯示出來'''
def show_dept(depList):
if not depList:
print("無效的數據")
return
format_title = "{:^6}{:^12}\t"#\t{:^8}\t{:^10}\t{:^10}\t{:10}"
print(format_title.format("ID", "部門名稱"))
format_data = "{:^6}{:^12}\t"#"{:^10}\t{:^10}\t{:^10}\t{:10}"
print("部門信息開始")
for info in depList:
{
print(format_data.format(info.get("id"), info.get("dep_name"))),
print("部門信息結束")
}
'''刪除部門信息'''
def delete():
mark = True # 標記是否循環
while mark:
depttId = input("請輸入要刪除的部門ID:")
if depttId is not "": # 判斷要刪除的部門ID是否存在
if os.path.exists(database_filename):
with open(database_filename, 'r') as rfile:
deptlist_old = rfile.readlines()
else:
deptlist_old = []
ifdel = False # 標記是否刪除
if deptlist_old: # 如果存在部門信息
with open(database_filename, 'w') as wfile:
d = {} # 定義空字典
for list in deptlist_old:
d = dict(eval(list)) # 字符串轉字典
if d['id'] != depttId:
wfile.write(str(d) + "\n") # 將一條信息寫入文件
else:
ifdel = True # 標記已經刪除
if ifdel:
print("ID為%s的部門信息已經被刪除..." % depttId)
else:
print("沒有找到ID為%s的部門信息..." % depttId)
else:
print("不存在部門信息")
break
show() # 顯示全部部門信息
inputMark = input("是否繼續刪除?(y/n):")
if inputMark == "y":
mark = True # 繼續刪除
else:
mark = False # 退出刪除部門信息操作
'''修改部門信息'''
def modify():
show() # 顯示全部部門信息
if os.path.exists(database_filename):
with open(database_filename, 'r') as rfile:
deptlist_old = rfile.readlines()
else:
return
depttId = input("請輸入要修改的部門ID:")
with open(database_filename, 'w') as wfile:
for deptone in deptlist_old:
d = dict(eval(deptone))
if d['id'] == depttId:
print("找到了這個部門,可以修改他的信息")
while True: # 輸入要修改的信息
try:
d["name"] = input("請輸入部門名稱:")
except:
print("您輸入有誤,請重新輸入!")
else:
break
deptone = str(d) # 將字典轉為字符串
wfile.write(deptone + "\n") # 將修改信息寫入到文件
print("修改成功")
else:
wfile.write(deptone) # 將未修改的信息寫入文件
mark = input("是否繼續修改其他部門信息?(y/n):")
if mark == "y":
modify()
'''排序'''
def sort():
show()
if os.path.exists(database_filename):
with open(database_filename, 'r') as file:
deptlist_old = file.readlines()
deptlist_new = []
for list in deptlist_old:
d = dict(eval(list))
deptlist_new.append(d)
else:
return
ascORdesc = input("請選擇(0升序;1降序)")
if ascORdesc == "0":
ascORdescBool = False # 標記變量,為False時表示升序,為True時表示降序
elif ascORdesc == "1":
ascORdescBool = True
else:
print("您輸入的信息有誤,請重新輸入!")
sort()
mode = input("請選擇排序方式(1):")
if mode == "1": # 按英語成績排序
deptlist_new.sort(key=lambda x: x["id"], reverse=ascORdescBool)
else:
print("您的輸入有誤,請重新輸入!")
sort()
show_dept(deptlist_new) # 顯示排序結果
'''統計部門總數'''
def total():
if os.path.exists(database_filename):
with open(database_filename, 'r') as rfile:
dept_old = rfile.readlines()
if dept_old:
{
print("部門統計"),
print("一共有%d個部門!" % len(dept_old)),
print("--"),
}
else:
print("還沒有錄入部門信息")
else:
print("暫未保存數據信息")
'''顯示所有部門信息'''
def show():
dept_new = []
if os.path.exists(database_filename):
with open(database_filename, 'r') as rfile:
deptlist_old = rfile.readlines()
for list in deptlist_old:
dept_new.append(eval(list))
if dept_new:
show_dept(dept_new)
else:
print("暫未保存數據信息")
if __name__ == '__main__':
main()
總結
以上是生活随笔為你收集整理的python写酒店管理系统报告_酒店管理系统学生工作-部门管理Python,作品,python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消息提醒:您有新的订单,请及时处理
- 下一篇: VC++ 创建桌面、开始菜单快捷方式(附