python面向过程编程小程序- 模拟超市收银系统
6.16自我总结
功能介绍
程序功能介绍:
商品信息再读取修改买卖均已xlsx格式
且生成购物记录也按/用户名/购买时间.xlsx格式生成
账号密码输入错误三次按照时间进行冻结
用户信息已json格式保存
程序写的过程
先生成功能模块和运行模块
再写功能模块中用到的固定的文件目录全放在setting.py文件中
与商品交互全放在shop.py中
与用户交互全放在user.py中
一些返回界面延迟动画全放在辅助模块中
1.程序设计目录
思路
- 生成xlsx文档就要导入pands模块
- 用户信息用josn保存导入json模块
- 把路径什么通用不变的丢入setting中
- 把所有文件存取丢入接口层中api中
- 其他增减根据功能来
- 一些通用的方法可以全部放入common中
2.run.py
from core.src import run
if __name__ == '__main__':
run()
3.src.py
from conf.setting import *
from lib.common import *
from api.user import *
from api.shop import *
import re
import numpy as np
goods_count = [1]
#注册
def register():
print('\033[47;;m\t\t欢迎使用注册功能\t\t\033[0m')
count = 0
while count == 0:
register_name = input('请输入名字按Q退出程序\n'
'\033[31;;m账号由汉字,字母,数字,下划线组成\033[0m'
'\n请输入:')
if register_name == 'Q':
go_to_run()
return False
if decide_user_name(register_name):
print('\033[31;;m账号存在\033[0m')
continue
register_name_1 = re.findall('\w+',register_name)
if len(register_name) == 0:
print('\033[31;;m请好好输入\033[0m')
continue
elif register_name_1[0] != register_name:
print('\033[31;;m账号由汉字,字母,数字,下划线组成\033[0m')
continue
else:
count =1
while True:
register_pwd = input('请输入密码按Q退出程序\n'
'\033[31;;m密码由汉字,字母,数字,下划线组成\033[0m\n'
'请输入')
if register_pwd == 'Q':
go_to_run()
return False
register_pwd_1 = re.findall('\w+', register_pwd)
if len(register_pwd) == 0:
print('\033[31;;m请好好输入\033[0m')
continue
elif register_pwd_1[0] != register_pwd:
print('\033[31;;m密码由汉字,字母,数字,下划线组成\033[0m')
continue
else:
save_info(register_name, register_pwd)
print('\033[32;;m注册成功\033[0m')
go_to_run()
return True
#登入
def login():
print('\033[47;;m\t\t欢迎使用登入功能\t\t\033[0m')
if not LOGIN_NAME[0]:
pwd_count =0
login_count = 0
while login_count==0:
login_name = input('请输入名字按Q退出程序\n请输入:')
if login_name == 'Q':
go_to_run()
return False
if not decide_user_name(login_name):
print('\033[31;;m账号不存在\033[0m')
continue
if not load_freeze_user(login_name):
go_to_run()
return False
else:
login_count = 1
while pwd_count<3:
login_pwd = input('\033[41;;m请输入密码输错三次会被冻结五分钟\033[0m\n请输入:')
if not load_info(login_name,login_pwd):
pwd_count +=1
print(f'\033[31;;m还有{3-pwd_count}次机会\033[0m')
continue
else:
print('\033[32;;m登入成功\033[0m')
LOGIN_NAME[0] = login_name
go_to_run()
return True
if pwd_count == 3:
freeze_user(login_name)
print(f'\033[31;;m账号{login_name}由于密码输入太多次暂时被冻结\033[0m')
go_to_run()
return False
else:
print(f'\033[32;;m账号{LOGIN_NAME[0]}以及登入')
login_chiose = input('输入0为退出当前程序\n'
'输入1为退出当前账号重新登入\n'
'输入2为退出当前账号并退出程序\n'
'请选择:')
if login_chiose not in ['0','1','2']:
print('\033[31;;m请好好输入\033[0m')
elif login_chiose == '0':
go_to_run()
return False
elif login_chiose == '1':
df = load_goods_pach()
dump_goods(df)
LOGIN_NAME[0] = None
login()
elif login_chiose == '2':
df = load_goods_pach()
dump_goods(df)
LOGIN_NAME[0] = None
print('\033[41;;m账号以退出\033[0m')
go_to_run()
return False
#充值
@login_deco
def top_up():
print('\033[47;;m\t\t欢迎使用充值功能\t\t\033[0m')
while True:
chiose = input('请输入充值金额\n输入Q退出功能\n请输入')
if chiose == 'Q':
go_to_run()
return False
elif not chiose.isdigit():
print('\033[31;;m请输入阿拉伯数字\033[0m')
continue
else:
info = load_login_info(LOGIN_NAME[0])
info["balance"] += int(chiose)
dump_login_info(LOGIN_NAME[0],info)
print(f'\033[42;;m账号{LOGIN_NAME[0]}充值成功\n当前余额{info["balance"]}元\033[0m')
go_to_run()
return True
#余额查询
@login_deco
def balance():
print('\033[47;;m\t\t欢迎使用余额查询功能\t\t\033[0m')
info = load_login_info(LOGIN_NAME[0])
print(f'\033[42;;m账号{LOGIN_NAME[0]}\n当前余额{info["balance"]}\033[0m')
go_to_run()
return True
#提现
@login_deco
def withdraw():
print('\033[47;;m\t\t欢迎使用提现功能\t\t\033[0m')
while True:
chiose = input('\033[32;;m请输入提现金额\n输入Q退出功能\n请输入')
if chiose == 'Q':
go_to_run()
return False
elif not chiose.isdigit():
print('\033[31;;m请输入阿拉伯数字\033[0m')
continue
else:
info = load_login_info(LOGIN_NAME[0])
if info["balance"] < int(chiose):
print('\033[31;;m余额不足\033[0m')
continue
info["balance"] -= int(chiose)
dump_login_info(LOGIN_NAME[0],info)
print(f'\033[41;;m账号{LOGIN_NAME[0]}提现成功\n当前余额{info["balance"]}元\033[0m')
go_to_run()
return True
#转账
@login_deco
def transfer():
print('\033[47;;m\t\t欢迎使用转账功能\t\t\033[0m')
count = 0
while count ==0:
chiose = input('\033[32;;m请输入转账金额\n输入Q退出功能\n请输入')
if chiose == 'Q':
go_to_run()
return False
elif not chiose.isdigit():
print('\033[31;;m[请输入阿拉伯数字\033[0m')
continue
else:
info = load_login_info(LOGIN_NAME[0])
if info["balance"] < int(chiose):
print('\033[31;;m余额不足\033[0m')
continue
info["balance"] -= int(chiose)
while True:
count =1
transfer_name = input('\033[32;;m转账的名字\n输入Q退出')
if transfer_name == 'Q':
go_to_run()
return False
if not decide_user_name(transfer_name):
print('\033[31;;m转账账号不存在\033[0m')
continue
if LOGIN_NAME[0] == transfer_name:
print('\033[31;;m不能转给自己\033[0m')
continue
transfer_name_info = load_login_info(transfer_name)
transfer_name_info["balance"] += int(chiose)
dump_login_info(transfer_name, transfer_name_info)
dump_login_info(LOGIN_NAME[0], info)
print(f'\033[42;;m账号{LOGIN_NAME[0]}转账成功\n当前余额{info["balance"]}元\033[0m')
go_to_run()
return True
#流水
@login_deco
def user_history():
print('\033[47;;m\t\t欢迎使用流水功能\t\t\033[0m')
if not history(LOGIN_NAME[0]):
print('\033[31;;m没有购买记录\033[0m')
go_to_run()
return False
while True:
date_lis = show_history(LOGIN_NAME[0])
chiose = input('\033[32;;m请输入你要查看的日期\n'
'输入Q退出')
if chiose not in date_lis:
print('\033[31;;m没有日期\033[0m')
continue
print(f'{chiose}')
print(load_goods_history(LOGIN_NAME[0],chiose))
history_count = 0
while history_count == 0:
next_chiose = input('\033[32;;m请输入Y继续查看的日期\n'
'输入Q退出\n'
'请输入')
if next_chiose not in ('Y','Q'):
print('\033[31;;m请好好输入\033[0m')
elif next_chiose == 'Y':
history_count =1
elif next_chiose =='Q':
go_to_run()
return False
#购物
@login_deco
def shopping():
count = 0
chiose_count =0
print('\033[47;;m\t\t欢迎使用购物功能\t\t\033[0m')
global goods_count
while count == 0:
if not goods_count[0]:
df = load_goods()
print('\t\t\t商品目录')
print(f'\033[35;36;m{df}\033[0m')
else:
df = load_goods_pach()
print('\t\t\t商品目录')
print(f'\033[35;36;m{df}\033[0m')
goods = input('\033[32;;m请选择你的商品\n'
'输入Q退出\n'
'请选择')
goods_list = df.columns
if goods == 'Q':
go_to_run()
return False
elif goods not in goods_list:
print('\033[31;;m无此商品\033[0m')
continue
else:
chiose_count =0
while chiose_count == 0:
num = input('\033[32;;m请选择你的商品数量\n'
'输入Q退出\n'
'请选择')
goods_num = df[goods]['数量']
if goods == 'Q':
go_to_run()
return False
elif not num.isdigit():
print('\033[31;;m请输入数字\033[0m')
continue
elif int(goods_num)<int(num):
print('\033[31;;m库存不足\033[0m')
else:
df[goods]['数量'] -= int(num)
goods_num = int(num)
dump_goods_pach(df)
print(f'\033[42;;m你把{goods}{num}个加入购物车\033[0m')
while chiose_count == 0:
chiose =input('\033[32;;m是否继续购物\n'
'Y是继续,N是退出\n'
'请选择')
if chiose not in ['Y','N']:
print('\033[31;;m请好好输入\033[0m')
continue
if chiose == 'N':
goods_count[0] = 1
go_to_run()
return True
if chiose == 'Y':
chiose_count = 1
goods_count[0] = 1
pass
#购物车
@login_deco
def shopping_car():
print('\033[47;;m\t\t欢迎使用购物车功能\t\t\033[0m')
global goods_count
if not goods_count[0]:
print('\033[31;;m购物车无商品\033[0m')
else:
df_1 = load_goods()
df_2 = load_goods_pach()
df = df_1 - df_2
print('-'*50)
print('\t\t\t购物车目录')
print(f'\033[35;36;m{df}\033[0m')
df_mun = df.values[1,:]
df_pice = df_1.values[0,:]
df_add = df_mun*df_pice
money = sum(df_add)
print(f'合计{money}元')
print('-' * 50)
while True:
chiose = input('\033[32;;m输入Q退出程序\n'
'输入0清空购物车并退出程序\n'
'输入1结算\n'
'请选择')
if chiose not in ['Q','0','1']:
print('\033[31;;m请好好输入\033[0m')
continue
elif chiose == 'Q':
go_to_run()
return False
elif chiose == '0':
goods_count[0] = None
df_new = load_goods()
dump_goods_pach(df_new)
go_to_run()
return False
elif chiose == '1':
info = load_login_info(LOGIN_NAME[0])
if int(money) > info['balance']:
print('\033[31;;m余额不足\033[0m')
continue
else:
print('\033[41;;m支付成功\033[0m')
info['balance'] -= int(money)
dump_login_info(LOGIN_NAME[0],info)
df_new = load_goods_pach()
dump_goods(df_new)
df_mun = df[1:2]
df_pice = df_1[0:1]
df = df.columns
new_df = df_pice.append(df_mun)
print(new_df)
time =time_strftime()
dump_goods_history(LOGIN_NAME[0],time,new_df)
go_to_run()
return True
#运行模块
def run():
action_dict={
'0':register,
'1':login,
'2':top_up,
'3':balance,
'4':withdraw,
'5':transfer,
'6':user_history,
'7':shopping,
'8':shopping_car,
}
while True:
if LOGIN_NAME[0]:
print(f'你好{LOGIN_NAME[0]}')
for action_num,action in ACTION_INFO.items():
print(f'\033[35;;m\t\t输入{action_num}功能为{action}\033[0m')
action_chiose = input('\033[32;;m请输入你要选择的功能:')
if action_chiose == 'Q':
print('退出程序')
return
if action_chiose not in action_dict:
print('\033[31;;m输入错误\033[0m')
continue
action_dict[action_chiose]()
if __name__ == '__main__':
run()
3.common.py
from conf.setting import *
import time
def login_deco(func):
def wrapper(*args,**kwargs):
if not LOGIN_NAME[0]:
print('请先登入')
go_to_run()
return False
func()
return True
return wrapper
def go_to_run():
for a in range(20):
time.sleep(0.1)
txt = '\t\t返回主界面中'
txt += '.'*int(a%4)
print('\r',f'\033[32;;m{txt}\033[0m',end='')
print('')
def time_now():
return time.time()
def time_strftime():
return str(time.strftime('%Y-%m-%d-%H-%M-%S'))
4.setting.py
import os
import time
ACTION_INFO={
'0':'注册',
'1':'登入',
'2':'充值',
'3':'余额查询',
'4':'提现',
'5':'转账',
'6':'购物历史记录',
'7':'购物',
'8':'购物车',
'Q' :'退出'
}
LOGIN_NAME = [None]
ATM_PATH = os.path.dirname(os.path.dirname(__file__))
USER_PATH = os.path.join(ATM_PATH,'db')
GOODS_PATH = os.path.join(ATM_PATH,'db','goods_info.xlsx')
GOODS_PATCH = os.path.join(ATM_PATH,'db','goods_info_patch.xlsx')
5.shop.py
(商品信息存储文件与功能模块的交互)
import pandas as pd
from conf.setting import GOODS_PATH,GOODS_PATCH
import os
def dump_goods(df):
df.to_excel(GOODS_PATH)
def dump_goods_pach(df):
df.to_excel(GOODS_PATCH)
def load_goods():
df = pd.read_excel(GOODS_PATH,index_col=0,header=0)
return df
def load_goods_pach():
df = pd.read_excel(GOODS_PATCH,index_col=0,header=0)
return df
def dump_goods_history(name,time,df):
path_1 = os.path.join(r'E:\ATM\db', name)
path = os.path.join(r'E:\ATM\db',name,f'{time}.xlsx')
if not os.path.exists(path_1):
os.mkdir(path_1)
df.to_excel(path)
def load_goods_history(name,time):
path = os.path.join(r'E:\ATM\db', name, f'{time}.xlsx')
df = pd.read_excel(path,index_col=0,header=0)
return df
def show_history(name):
new_list =[]
path_1 = os.path.join(r'E:\ATM\db', name)
lis = os.listdir(path_1)
print('\033[46;;m提示:年-月-日-时-分-秒\033[0m')
for info in lis:
info = info[0:-5]
print(info)
new_list.append(info)
return new_list
def history(name):
path_1 = os.path.join(r'E:\ATM\db', name)
if os.path.exists(path_1):
return True
return False
if __name__ == '__main__':
df = load_goods_pach()
print(df)
6.user.py
(用户信息存储文件与功能模块的交互)
from conf.setting import *
import json
import hashlib
from lib.common import time_now
def save_info(user_name,pwd):
m = hashlib.md5()
m.update(pwd.encode('utf8'))
pwd =m.hexdigest()
user_path = os.path.join(USER_PATH, f'{user_name}.json')
with open(user_path,'w',encoding='utf8') as fw:
info_dict = {'name':user_name,'pwd':pwd,'freeze':time_now(),'balance':0,'freeze_count':0}
json.dump(info_dict,fw)
def load_info(user_name,pwd):
user_path = os.path.join(USER_PATH, f'{user_name}.json')
if not LOGIN_NAME[0]:
if not os.path.exists(user_path):
print('\033[31;;m用户不存在\033[0m')
return False
m = hashlib.md5()
m.update(pwd.encode('utf8'))
pwd =m.hexdigest()
with open(user_path, 'r', encoding='utf8') as fr:
info_dict = json.load(fr)
if info_dict.get('pwd') != pwd:
print('\033[31;;m密码错误\033[0m')
return False
with open(user_path, 'r', encoding='utf8') as fr:
info_dict = json.load(fr)
info_dict['freeze_count'] = 0
with open(user_path, 'w', encoding='utf8') as fw:
json.dump(info_dict,fw)
return info_dict
def decide_user_name(user_name):
user_path = os.path.join(USER_PATH, f'{user_name}.json')
if os.path.exists(user_path):
return True
return False
def freeze_user(user_name):
user_path = os.path.join(USER_PATH, f'{user_name}.json')
with open(user_path, 'r', encoding='utf8') as fr:
info_dict = json.load(fr)
info_dict['freeze_count'] += 1
info_dict['freeze'] = time_now() + 300*info_dict['freeze_count']
with open(user_path,'w',encoding='utf8') as fw:
json.dump(info_dict,fw)
def load_freeze_user(user_name):
user_path = os.path.join(USER_PATH, f'{user_name}.json')
with open(user_path, 'r', encoding='utf8') as fr:
dict = json.load(fr)
if time_now() >= dict['freeze'] :
print('\033[32;;m账号登入成功\033[0m')
return True
else:
min = int(divmod(-time_now() + dict['freeze'], 60)[0])
s = int(divmod(-time_now() + dict['freeze'], 60)[1])
print(f'\033[31;;m账号{user_name}已被冻结,还需要{min}分{s}秒\033[0m')
return False
def load_login_info(name):
login_user_path = os.path.join(USER_PATH, f'{name}.json')
with open(login_user_path, 'r', encoding='utf8') as fr:
info_dict = json.load(fr)
return info_dict
def dump_login_info(name,dict):
login_user_path = os.path.join(USER_PATH, f'{name}.json')
with open(login_user_path, 'w', encoding='utf8') as fw:
json.dump(dict,fw)
return True
if __name__ == '__main__':
save_info('杨文益','12312')
dict = load_info('杨文益','12312')
print(dict['name'])
7.goods_info.xlsx/goods_info_patch.xlsx
苹果 | 香蕉 | 西瓜 | 荔枝 | 梨 | |
---|---|---|---|---|---|
价格 | 2 | 3 | 10 | 5 | 5 |
数量 | 200 | 215 | 34 | 32 | 523 |
python面向过程编程小程序- 模拟超市收银系统的更多相关文章
- python面向过程编程小程序 -ATM(里面用了终端打印)
06.09自我总结 1.文件摆放 ├── xxxx │ ├── run.py │ └── fil_mode.py │ └── data_time.py │ └── loading.py │ └── d ...
- C++ 大作业 超市收银系统
#include<iostream> #include<fstream> #include<string> #include<iomanip> #inc ...
- JAVA个人小程序GUI篇-收银(标签、按钮、复选框、下拉标、文本域、表格······)
如果用eclipse需先装载windowsbuild //导入包 import java.awt.BorderLayout; import java.awt.EventQueue; import ja ...
- 【Python精华】100个Python练手小程序
100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...
- 整理了适合新手的20个Python练手小程序
100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. 本文附带基础视频教程:私信回复[基础]就可以获取的 [程序1] ...
- 利用python实现微信小程序游戏跳一跳详细教程
利用python实现微信小程序游戏跳一跳详细教程 1 先安装python 然后再安装pip <a href="http://newmiracle.cn/wp-content/uploa ...
- 微信小程序开发:python+sanic 实现小程序登录注册
开发微信小程序时,接入小程序的授权登录可以快速实现用户注册登录的步骤,是快速建立用户体系的重要一步.这篇文章将介绍 python + sanic + 微信小程序实现用户快速注册登录全栈方案. 微信小程 ...
- 基于C#的超市收银管理系统
基于C#的超市收银管理系统 前序 一直在忙学习Qt有关的知识,非常有幸这学期学习了C#.让我也感觉到了一丝欣慰,欣慰的是感觉好上手啊,学了几天顿时懂了.好多控件的使用方法好类似,尽管平时上课没有怎么认 ...
- 肯德基收银系统java
参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++或Java或Python结合设计模式(2种以上)至少实现系统的以下功能: 1.正常餐品结算和找零. 2.基本套餐结算和找零. 3.使用优惠 ...
随机推荐
- 用Delphi开发视频聊天软件
摘要:目前网上视频聊天软件.视频会议软件.可视IP电话软件随处可见,你是否想自己做一个玩玩?其实这类软件无非是视频加上网络而建成的.如果熟悉视频捕捉和网络传输技术,根本就难不倒你.微软为软件开发人员提 ...
- Window Features(包括Z-Order,Layered Windows, Message-Only Windows, Owned Windows, Window的状态等)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632599(v=vs.85).aspx#owned_windows https: ...
- c# winform快捷键实现
我们在软件中经常用到快捷键,这里整理备份一下. 首先我们要定义可以作为快捷键的按键,以下是整理的 一些,自己可以根据情况来修改 public static Dictionary<int, str ...
- iOS11中iOS处理GIF图片的方式
GIF 五部走如下 : 1 从相册中取出GIF图的Data 2 通过腾讯的IM发送Gif图 3 展示GIF图 4 GIF图URL缓存机制 5 将展示的GIF图存到相册中 一 从相册中 ...
- QTcpSocket 对连接服务器中断的不同情况进行判定
http://blog.csdn.net/goforwardtostep/article/details/52300335
- impala学习笔记
impala学习笔记 -- 建库 CREATE DATABASE IF NOT EXISTS database_name; -- 在HDFS文件系统中创建数据库,需要指定要创建数据库的位置. CREA ...
- java集合框架collection(4)HashMap和Hashtable的区别
HashMap和Hashtable的区别 HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们之间的分别.主要的区别有:线程安全性,同步(synchronizatio ...
- ElasticSearch2.3.1环境搭建哪些不为人知的坑
首先说明一点,大家最好不要用什么尝鲜版,用比稳定版就好了,要不麻烦不断,另外出了问题,最好去官网,或者google搜索,因为这样靠谱些,要不现在好多都是低版本的,1.4的什么的,结果按照安装,多少情况 ...
- Netty源码分析--Reactor模型(二)
这一节和我一起开始正式的去研究Netty源码.在研究之前,我想先介绍一下Reactor模型. 我先分享两篇文献,大家可以自行下载学习. 链接:https://pan.baidu.com/s/1Uty ...
- tomcat实现
转载地址:https://blog.csdn.net/u014795347/article/details/52328221?locationNum=2&fps=1 以下代码纯属本人复制,而且 ...