這篇文章主要介紹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(回車去結賬,0清空購物車):1
--------------------------------------
Id:1
名稱:Apple Watch
價格:¥3299.00
庫存:100
請輸入購買數(shù)量:2
--------------------------------------
Apple Watch(¥3299) * 2 =¥6598.00
--------------------------------------
總金額:¥6598.00
請輸入商品Id(回車去結賬,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):
"""
設置新商品的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(回車去結賬,0清空購物車):')
if len(g_id) == 0:
# 結賬
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):
"""運行應用程序"""
print('智游電子產(chǎn)品商店')
print('v1.0')
print('\n')
self.list()
while True:
self.add_to_cart()
shop = Shop()
shop.run()以上是“python如何實現(xiàn)電子產(chǎn)品商店”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站名稱:python如何實現(xiàn)電子產(chǎn)品商店-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.chinadenli.net/article24/deheje.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、服務器托管、搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設、品牌網(wǎng)站設計、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)