KFC簡易模擬系統(Python)
問題重述:
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()
限於本人技術有限,只是提供一個基本思路,還有很多改進地方