源代碼下載鏈接: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