網站首頁 編程語言 正文
本文實例為大家分享了Python實現倉庫管理系統的具體代碼,供大家參考,具體內容如下
注意:在Linux環境運行
代碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName ?:store_system.py
# @Time ? ? ?:2020/3/3 23:10
# @Author ? ?:anqixiang
# @Function ?:模擬倉庫管理系統
'''
1.商品清單保存在/opt/shop_info.txt文件中
2.可以查看、增加、刪除商品和修改商品價格
3.在任何位置輸入b返回上級菜單,輸入q退出
'''
import os
from subprocess import run
#輸出顏色
def cecho(num,content):
? ? print('\033[%sm%s\033[0m' %(num, content))
#選b返回上一層,選q退出
def choice_action(action):
? ? while action != "b":
? ? ? ? if action == "q":
? ? ? ? ? ? exit(0)
? ? ? ? else:
? ? ? ? ? ? break
? ? return action
#展示商品
def view_shop(file_name):
? ? commodity = [] ? ? ? ? ? ? ?#所有商品保存到該列表
? ? if not os.path.isfile(file_name):
? ? ? ? os.mknod(file_name)
? ? else:
? ? ? ? with open(file_name, 'r') as file:
? ? ? ? ? ? for each in file:
? ? ? ? ? ? ? ? commodity.append(each.splitlines())
? ? if len(commodity) == 0:
? ? ? ? cecho(35, "貨倉空空如也,請速速添加商品!")
? ? ? ? #打印商品信息
? ? else:
? ? ? ? print('%-10s%-8s%-12s' % ('序號', '名字', '價格'))
? ? ? ? for index, value in enumerate(commodity):
? ? ? ? ? ? alist = value[0].split(":") ? ? ? ? #把字符串轉成列表,以“:”分割
? ? ? ? ? ? print('%-12s%-10s%-8s' % (index + 1, alist[0], alist[1]))
? ? return commodity
#增加商品,每增加一個就保存到文件
def add_shop(file_name):
? ? while True:
? ? ? ? add_dict = {}
? ? ? ? shop_name = input(">>>輸入商品名:").strip()
? ? ? ? if choice_action(shop_name) == "b":
? ? ? ? ? ? break
? ? ? ? shop_price = input(">>>輸入商品價格(元):").strip()
? ? ? ? if choice_action(shop_price) == "b":
? ? ? ? ? ? break
? ? ? ? elif shop_price.isdigit():
? ? ? ? ? ? add_dict[shop_name] = shop_price ? ? ? ?#商品名作key,價格作值,存入字典
? ? ? ? ? ? for i in add_dict:
? ? ? ? ? ? ? ? with open(file_name, 'a+')as file:
? ? ? ? ? ? ? ? ? ? file.write('%s:%s\n' % (i, add_dict[i]))
? ? ? ? ? ? ? ? ? ? print("\033[92m%s存入成功\033[0m" % shop_name)
? ? ? ? ? ? ? ? view_shop(file_name)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")
#刪除商品
def del_shop(file_name):
? ? menu_info = "商品清單"
? ? print(menu_info.center(26,'-'))
? ? commodity = view_shop(file_name)
? ? while True:
? ? ? ? del_num = input(">>>商品序號:").strip()
? ? ? ? if choice_action(del_num) == "b":
? ? ? ? ? ? break
? ? ? ? elif del_num.isdigit():
? ? ? ? ? ? del_num = int(del_num)
? ? ? ? ? ? rc = run("sed -i '/%s/d' %s" % (commodity[del_num-1][0], file_name), shell=True)
? ? ? ? ? ? if not rc.returncode:
? ? ? ? ? ? ? ? cecho(92, "刪除成功")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? cecho(31,"刪除失敗")
? ? ? ? ? ? view_shop(file_name)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")
#修改商品價格
def update_price(file_name):
? ? menu_info = "商品清單"
? ? print(menu_info.center(26,'-'))
? ? commodity = view_shop(file_name)
? ? while True:
? ? ? ? update_num = input(">>>商品序號:").strip()
? ? ? ? if choice_action(update_num) == "b":
? ? ? ? ? ? break
? ? ? ? elif update_num.isdigit():
? ? ? ? ? ? update_num = int(update_num)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")
? ? ? ? new_price = input(">>>新的價格(元):").strip()
? ? ? ? if choice_action(new_price) == "b":
? ? ? ? ? ? break
? ? ? ? elif new_price.isdigit():
? ? ? ? ? ? new_price = int(new_price)
? ? ? ? ? ? alist = commodity[update_num-1][0].split(':') ? #將商品名和價格轉成一個列表,如['coffee', '30']
? ? ? ? ? ? alist[1] = new_price ? ? ? ?#修改價格
? ? ? ? ? ? rc = run("sed -i '/%s/c %s:%s' %s" % (alist[0], alist[0], alist[1], file_name), shell=True)
? ? ? ? ? ? if not rc.returncode:
? ? ? ? ? ? ? ? cecho(92, "修改成功")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? cecho(31,"修改失敗")
? ? ? ? ? ? view_shop(file_name)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")
#主程序
def show_menu():
? ? cmds = {'0': view_shop, '1': add_shop, '2': del_shop, '3': update_price}
? ? prompt = '''(0)查看商品信息
(1)增加商品
(2)刪除商品
(3)修改商品價格
(b)返回上級菜單
(q)退出
輸入(0/1/2/3/b/q):'''
? ? fname='/opt/shop_info.txt' ? ? ?#保存商品信息
? ? while True:
? ? ? ? choice = input(prompt).strip()
? ? ? ? if choice not in '0123bq':
? ? ? ? ? ? cecho(31, "Invalid Option")
? ? ? ? elif choice_action(choice) == "b":
? ? ? ? ? ? cecho(31, "已經是第一級菜單")
? ? ? ? else:
? ? ? ? ? ? cmds[choice](fname)
if __name__ == "__main__":
? ? try:
? ? ? ? show_menu()
? ? except KeyboardInterrupt as e:
? ? ? ? print()
? ? ? ? cecho(31, "非正常退出,請下次輸入字母q進行退出!")
效果圖
原文鏈接:https://blog.csdn.net/anqixiang/article/details/104644727
相關推薦
- 2022-11-21 QT通過C++線程池運行Lambda自定義函數流程詳解_C 語言
- 2022-12-12 封裝flutter狀態管理工具示例詳解_Android
- 2022-11-17 Go語言中常用的基礎方法總結_Golang
- 2022-10-05 ASP.NET?Core在Task中使用IServiceProvider的問題解析_實用技巧
- 2022-04-18 詳解OpenGL?Shader抗鋸齒的實現_Android
- 2022-07-28 C++圖文并茂講解類型轉換函數_C 語言
- 2022-10-12 python?time時間庫詳解_python
- 2022-01-05 el-select使用了多選時,選中多個會撐開原始高度,樣式錯亂,使用tag展示,一行顯示全部內容,
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支