python實現(xiàn)電子產(chǎn)品商店
利用python實現(xiàn)以下功能:基于python下的電子產(chǎn)品商店
電子產(chǎn)品商店
v0.1
請選擇商品:
=============================
1 Apple Watch ¥3299.00
--------------------------------------
2 AirPods ¥1288.00
--------------------------------------
3 Home Pod ¥1299.00
--------------------------------------
請輸入商品Id(回車去結(jié)賬,0清空購物車):1
--------------------------------------
Id:1
名稱:Apple Watch
價格:¥3299.00
庫存:100
請輸入購買數(shù)量:2
--------------------------------------
Apple Watch(¥3299) * 2 =¥6598.00
--------------------------------------
總金額:¥6598.00
請輸入商品Id(回車去結(jié)賬,0清空購物車):2
--------------------------------------
Id:2
名稱:AirPods
價格:¥1288.00
庫存:100
請輸入購買數(shù)量:2
--------------------------------------
Apple Watch(¥3299.00) * 2 =¥6598.00
AirPods(¥1288.00) * 2 =¥2576.00
--------------------------------------
總金額:¥9174.00
1.首先,先在ProcessOn上畫出一個基本的流程圖,使自己有一個清晰的邏輯,如何去寫這個項目,流程圖如下:

2.其次,再列舉出來這個項目中需要用到的類都有哪些,各自包含的屬性是什么以及定義的都有哪些函數(shù)。然后在ProcessOn中 創(chuàng)建一個UML模板(從上往下依次是類名,屬性,函數(shù)名),模板如下:

3.根據(jù)流程圖和UML模板編寫程序,代碼如下:
(1)定義一個類名為Goods的類
# 商品類
class Goods(object):
def __init__(self,name,price,stock):
self.id = 0
self.name = name
self.price = price
self.stock = stock
# 當打印對象時,輸出的內(nèi)容
def __str__(self):
return 'id:%s\n' \
'名稱:%s\n' \
'價格:%s\n' \
'庫存:%s\n' % (self.id,self.name,self.price,
self.stock)
if __name__ == '__main__':
goods = Goods('Apple pods',2999,100)
print(goods)
goods2 = Goods('Apple Watch',3666,100)
print(goods2)
(2)定義一個類名為Cartitem的類
from goods import Goods
class CartItem(object):
# 購物車商品
def __init__(self,goods,count):
self.goods = goods
self.count = count
def __str__(self):
# %f是小數(shù)類型的占位符
return '%s(¥%.2f)*%s' % (self.goods.name,
self.goods.price,self.count)
# 計算商品小計
def amout(self):
return self.goods.price * self.count
if __name__ == '__main__':
goods = Goods('Apple pods',2999,100)
# 創(chuàng)建購物車商品對象,需要傳入一個商品對象
item = CartItem(goods,2)
money = item.amout()
print(money)
(3)最后把前兩個類整合一下,實現(xiàn)具體的功能:
from goods import Goods
from cart import CartItem
class Shop(object):
"""商店"""
def __init__(self):
# 存儲所有商品
self.shops = []
# 存儲購物車商品
self.cart = []
# 加載商品
self.load()
def load(self):
"""加載商品"""
self.add(Goods('Apple Watch', 3299, 100))
self.add(Goods('AirPods', 1288, 100))
self.add(Goods('Home Pod', 1299, 100))
self.add(Goods('iPhone X', 6288, 100))
def add(self, good):
"""
設(shè)置新商品的id,添加到列表中
:param good: 新商品
:return: None
"""
good.id = len(self.shops) + 1
self.shops.append(good)
def print_line(self):
print('-'*50)
def print_double_line(self):
print('='*50)
def list(self):
"""列出所有商品"""
print('請選擇商品:')
self.print_double_line()
# 遍歷商品列表
for g in self.shops:
print('%s %s %s' % (g.id, g.name, g.price))
self.print_line()
def list_cart(self):
"""展示購物車商品,計算總價"""
self.print_line()
total = 0.0
for item in self.cart:
print('%s =¥%s' % (item, item.amout()))
total += item.amout()
self.print_line()
print('總金額:¥%.2f' % total)
def add_to_cart(self):
"""添加商品到購物車"""
print('\n')
g_id = input('請輸入商品Id(回車去結(jié)賬,0清空購物車):')
if len(g_id) == 0:
# 結(jié)賬
total = 0.0
for item in self.cart:
total += item.amout()
self.print_line()
print('請支付:¥%.2f' % total)
# 清空購物車
self.cart.clear()
print('支付成功!')
elif g_id == '0':
self.cart.clear()
print('購物車已清空!')
else:
# 計算商品索引
idx = int(g_id) - 1
# 取出商品
goods = self.shops[idx]
self.print_line()
print(goods)
count = int(input('請輸入購買數(shù)量:'))
# 判斷數(shù)量是否大于庫存量
while count > goods.stock:
count = int(input('沒有這么多商品,請重新輸入:'))
# 如果商品已經(jīng)在購物車中,修改商品數(shù)量
# 變量表示在購物車中是否有這個商品
is_exsts = False
for item in self.cart:
if item.goods == goods:
# 說明在購物車中有該商品
is_exsts = True
item.count += count
# 減少庫存
goods.stock -= count
# 如果執(zhí)行到這,is_exsts的值還是False,說明購物車中沒有該商品
if is_exsts == False:
# 把商品添加到購物車
goods.stock -= count
self.cart.append(CartItem(goods, count))
# 展示購物車商品,計算總價
self.list_cart()
def run(self):
"""運行應(yīng)用程序"""
print('智游電子產(chǎn)品商店')
print('v1.0')
print('\n')
self.list()
while True:
self.add_to_cart()
shop = Shop()
shop.run()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
將imagenet2012數(shù)據(jù)為tensorflow的tfrecords格式并跑驗證的詳細過程
這篇文章主要介紹了將imagenet2012數(shù)據(jù)為tensorflow的tfrecords格式并跑驗證,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
Python數(shù)據(jù)庫編程之pymysql詳解
本文主要介紹了Python數(shù)據(jù)庫編程中pymysql,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
關(guān)于tf.matmul() 和tf.multiply() 的區(qū)別說明
這篇文章主要介紹了關(guān)于tf.matmul() 和tf.multiply() 的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python編寫一個驗證碼圖片數(shù)據(jù)標注GUI程序附源碼
這篇文章主要介紹了Python編寫一個驗證碼圖片數(shù)據(jù)標注GUI程序,本文給大家附上小編精心整理的源碼,需要的朋友可以參考下2019-12-12

