源代码下载链接:https://download.csdn.net/download/xust_kevin/10743109
问题重述:
1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠劵购买餐品结算和找零。
4.可在一定时间段参与店内活动(自行设计或参考官网信息)。
5.模拟打印小票的功能(写到文件中)。
代码实现所用到的工具和包:
1.pycharm
2.mysql
3.pymysql(python包)
4.datetime(python包)
基本思路:
思考一下去快餐店就餐的情景:一个顾客走进快餐店,他只需要直接去找服务员点餐,服务员给了他一份菜单,在顾客看过菜单后选择出了他想要的食物,服务员计算好顾客需要支付的金额后顾客支付所需要的人名币,服务员打印出小票,并结束服务。这就是一个完整的快餐店点餐的过程。
在这个过程中涉及到的人员有:顾客、服务员
在这个过程中涉及到的设备有:点餐机、小票打印机
他们之间的关系为:服务员使用点餐机为顾客点餐,顾客根据服务员提供的菜单进行点餐,点餐后服务员用小票打印机打印小票。从中看出服务员与顾客课设备相关联,顾客与服务员相关联,因此我设计了一个顾客类、服务员类、食物类,因为设计到使用数据库,因此又增加了一个数据库类。服务员类中包含食物类和数据库类的成员变量,顾客类中包含服务员类的成员变量。
实现步骤:
1.数据库类
数据库主要负责连接在本地客户端的MySQL应用服务程序,执行相应的查询、添加、删除和更新操作。
在次我的设计是将食物餐点和需要保存的销售量和营业额存储在MySQL数据库中,因此在数据库中至少需要三张表(食物表、销售量表、营业额统计表)来存放信息,分别要执行的操作为查询食物信息,添加营业额统计信息和添加或更新销售量信息
1.查询食物信息:
因为需要在控制台打印出菜单信息,所以只需要执行一个select操作就可以将数据库中的信息得到
2.统计销售量:
因为在统计销售量是可能存在有部分食物已经在数据库中,因此只需要执行更新数量操作,而如果数据库中没有该项食物的记录,则需要执行插入操作
3.统计营业额:因为营业额是以天来计算的,而且是在结束营业后才进行统计,所以只需要执行插入操作
代码如下:
import pymysql
import datetime
class Connector:
host = '你的端口号'
user = '你的用户名'
passwd = '你的密码'
db = '你的数据库'
# 连接数据库并返回数据库连接对象
def connect_db(self):
try:
db = pymysql.Connect(self.host, self.user, self.passwd, self.db)
return db
except pymysql.err.OperationalError:
print('连接数据库失败')
exit()
# 查询单品食物信息
def select_singalmeal(self):
select_sql = 'select number,name,price from singalmeal'
db = self.connect_db()
cursor = db.cursor()
try:
cursor.execute(select_sql)
except pymysql.err.IntegrityError:
print('查询失败')
db.commit()
db.close()
return cursor.fetchall()
# 查询套餐食物信息
def select_setmeal(self):
select_sql = 'select number,name,price from setmeal'
db = self.connect_db()
cursor = db.cursor()
try:
cursor.execute(select_sql)
except pymysql.err.IntegrityError:
print('查询失败')
db.commit()
db.close()
return cursor.fetchall()
# 统计销售物品信息并写入数据库
def insert_sell(self, prodect):
select_sql = 'select name from sell'
db = self.connect_db()
cursor = db.cursor()
try:
cursor.execute(select_sql)
except pymysql.err.IntegrityError:
print('查询失败')
db.commit()
result1 = cursor.fetchall()
existence_list = []
for each in result1:
existence_list.append(each[0])
for each in prodect:
try:
if each in existence_list:
update_sql = "update sell set amount=amount+1 where name = '%s'" % each
cursor.execute(update_sql)
else:
insert_sql = "insert into sell (name,amount) values ('%s','%d')" % (each, 1)
cursor.execute(insert_sql)
except pymysql.err.IntegrityError:
print('更新或插入数据库失败')
db.commit()
db.close()
# 统计营业额
def insert_turnover(self, turnover):
time = datetime.datetime.now()
curent_time = datetime.datetime.strftime(time, '%Y-%m-%d %H:%M:%S')
insert_sql = "insert into turnover (date,money) values ('%s','%f')" % (curent_time, turnover)
db = self.connect_db()
cursor = db.cursor()
try:
cursor.execute(insert_sql)
except pymysql.err.IntegrityError:
print('插入数据库失败')
db.commit()
db.close()
2.食物类
因为我将食物分成了单品和套餐,所以我定义了一个食物的基本类,定义一个方法来显示食物的信息,用单品类和套餐类继承食物基类
代码如下:
from KFC import SQLConnector
class Meal(object):
connection = SQLConnector.Connector()
def show_meal(self):
pass
class Singalmeal(Meal):
# 显示单品食物信息
def show_meal(self):
list_number = []
list_name = []
list_price = []
result = self.connection.select_singalmeal()
count = 1
print('餐号 名称 价格')
for each in result:
list_number.append(each[0])
list_name.append(each[1])
list_price.append(each[2])
length = 10 - len(each[1])
space = ' ' * length
print('%3d %s %s %.1f元' % (each[0], each[1].ljust(10), space, each[2]))
# print(-*length, str(each[2]).ljust(10))
count = count + 1
dic_meal = list(zip(list_number, list_name, list_price))
return dic_meal
class Setmeal(Meal):
# 显示套餐食物信息
def show_meal(self):
list_number = []
list_name = []
list_price = []
result = self.connection.select_setmeal()
count = 1
print('餐号 名称 价格')
for each in result:
list_number.append(each[0])
list_name.append(each[1])
list_price.append(each[2])
length = 10 - len(each[1])
space = ' ' * length
print('%3d %s %s %.1f元' % (each[0], each[1].ljust(10), space, each[2]))
# print(-*length, str(each[2]).ljust(10))
count = count + 1
dic_meal = list(zip(list_number, list_name, list_price))
return dic_meal
class Allmeal(Meal):
# 显示所有食物信息
def show_meal(self):
list_number = []
list_name = []
list_price = []
count = 1
result_sin = self.connection.select_singalmeal()
result_set = self.connection.select_setmeal()
print('餐号 名称 价格')
for each in result_sin:
list_number.append(count)
list_name.append(each[1])
list_price.append(each[2])
length = 10 - len(each[1])
space = ' ' * length
print('%3d %s %s %.1f元' % (count, each[1].ljust(10), space, each[2]))
count = count + 1
for each in result_set:
list_number.append(count)
list_name.append(each[1])
list_price.append(each[2])
length = 10 - len(each[1])
space = ' ' * length
print('%3d %s %s %.1f元' % (count, each[1].ljust(10), space, each[2]))
count = count + 1
dic_meal = list(zip(list_number, list_name, list_price))
return dic_meal
3.服务员类
服务员需要接待客人、打印小票、统计销售额等,因此定义一个方法用来接待客人(用于显示和接收控制台信息),定义一个方法用来专门打印小票(将订单信息写入文件),定义一个方法来统计当天的销售额,我添加了一个优惠的方法在里面
代码如下:
from KFC import Meal
from KFC import SQLConnector
import datetime
class Clerk(object):
turnover = 0
temp_money = 0
conn = SQLConnector.Connector()
# 打印小票(写入文件)
def print_smalltick(self, text):
with open('D:\\实验-程序方法学\\作业包4-KFC\\result.txt', 'a') as f:
f.write(text)
# 周五9折
def friday_discount(self):
time = datetime.datetime.now()
curent_time = datetime.datetime.strftime(time, '%Y-%m-%d %H:%M:%S')
day = time.weekday()
# 0-星期一。。。。6-星期天
if day == 4:
return 0.9, curent_time
else:
return 1, curent_time
# 售货员服务客人
def server_client(self):
print('请选择点个餐类型 :')
print('1.单点 2.套餐 3.单点+套餐')
select_kind = int(input('请选择:'))
if select_kind == 1:
meal = Meal.Singalmeal()
elif select_kind == 2:
meal = Meal.Setmeal()
else:
meal = Meal.Allmeal()
# 显示点餐信息
dic_money = meal.show_meal()
select_food = list(map(int, input('请选择你的餐号并用空格隔开:').split()))
self.temp_money = 0
smalltick = ''
sellproduct_list = []
for count in range(len(select_food)):
for each in dic_money:
if each[0] == select_food[count]:
sellproduct_list.append(each[1])
self.temp_money = self.temp_money + each[2]
length = 0 - len(each[1])
space = ' ' * length
# print('%3d%s%s%.1f元' % (count + 1, ' ' + each[1].ljust(10), space, each[2]))
smalltick = smalltick + str(count + 1) + ' ' + each[1].ljust(10) + space + str(
each[2]) + '元\n'
discount, date = self.friday_discount()
self.temp_money = self.temp_money * discount
self.temp_money = round(self.temp_money, 2)
space = ' ' * 15
# print('折扣:%.1f%s总计:%.1f元' % (discount, space, self.temp_money))
smalltick = smalltick + '折扣:' + str(discount) + space + ' 总计:¥' + str(self.temp_money) + '\n'
self.turnover = self.turnover + self.temp_money
print(smalltick)
print('1.使用优惠卷 2.不使用优惠卷')
select_co = int(input('请选择:'))
if select_co == 1:
coupon = int(input('请输入优惠券金额:'))
else:
coupon = 0
print('您的订单总共需要支付%.1f元' % round(float(self.temp_money) - coupon, 2))
pay_money = int(input('请输入付款金额:'))
if pay_money < self.temp_money - coupon:
print('金额不足以支付')
else:
rest_money = round(pay_money - self.temp_money + coupon, 2)
smalltick = smalltick + '应付款:¥' + str(self.temp_money) + ' 优惠金额:¥' + str(coupon) + '\n'
smalltick = smalltick + '实付款:¥' + str(pay_money) + ' 找零:¥' + str(rest_money) + '\n'
smalltick = smalltick + date +'\n\n'
print(smalltick)
self.print_smalltick(smalltick)
self.conn.insert_sell(sellproduct_list)
# 停止营业,显示当天营业额
def close(self):
print('营业结束,当天营业额为%.1f元' % self.turnover)
self.conn.insert_turnover(self.turnover)
4.顾客类
顾客只需要跟服务员沟通,顾客只用联系服务员点餐,所以在顾客类定义一个点餐方法
代码如下:
from KFC import Clerk
class Client(object):
waiter = Clerk.Clerk()
def oder_meal(self):
order = True
while order:
print('1.点餐 2.退出')
select = int(input('请选择:'))
if select == 1:
self.waiter.server_client()
else:
order = False
self.waiter.close()
client = Client()
client.oder_meal()
限于本人技术有限,只是提供一个基本思路,还有很多改进地方
源代码下载链接:https://download.csdn.net/download/xust_kevin/10743109