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

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

3.根據(jù)流程圖和UML模板編寫(xiě)程序,代碼如下:
(1)定義一個(gè)類名為Goods的類
# 商品類
class Goods(object):
def __init__(self,name,price,stock):
self.id = 0
self.name = name
self.price = price
self.stock = stock
# 當(dāng)打印對(duì)象時(shí),輸出的內(nèi)容
def __str__(self):
return 'id:%s\n' \
'名稱:%s\n' \
'價(jià)格:%s\n' \
'庫(kù)存:%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)定義一個(gè)類名為Cartitem的類
from goods import Goods
class CartItem(object):
# 購(gòu)物車商品
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)
# 計(jì)算商品小計(jì)
def amout(self):
return self.goods.price * self.count
if __name__ == '__main__':
goods = Goods('Apple pods',2999,100)
# 創(chuàng)建購(gòu)物車商品對(duì)象,需要傳入一個(gè)商品對(duì)象
item = CartItem(goods,2)
money = item.amout()
print(money)
(3)最后把前兩個(gè)類整合一下,實(shí)現(xiàn)具體的功能:
from goods import Goods
from cart import CartItem
class Shop(object):
"""商店"""
def __init__(self):
# 存儲(chǔ)所有商品
self.shops = []
# 存儲(chǔ)購(gòu)物車商品
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('請(qǐng)選擇商品:')
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):
"""展示購(gòu)物車商品,計(jì)算總價(jià)"""
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):
"""添加商品到購(gòu)物車"""
print('\n')
g_id = input('請(qǐng)輸入商品Id(回車去結(jié)賬,0清空購(gòu)物車):')
if len(g_id) == 0:
# 結(jié)賬
total = 0.0
for item in self.cart:
total += item.amout()
self.print_line()
print('請(qǐng)支付:¥%.2f' % total)
# 清空購(gòu)物車
self.cart.clear()
print('支付成功!')
elif g_id == '0':
self.cart.clear()
print('購(gòu)物車已清空!')
else:
# 計(jì)算商品索引
idx = int(g_id) - 1
# 取出商品
goods = self.shops[idx]
self.print_line()
print(goods)
count = int(input('請(qǐng)輸入購(gòu)買數(shù)量:'))
# 判斷數(shù)量是否大于庫(kù)存量
while count > goods.stock:
count = int(input('沒(méi)有這么多商品,請(qǐng)重新輸入:'))
# 如果商品已經(jīng)在購(gòu)物車中,修改商品數(shù)量
# 變量表示在購(gòu)物車中是否有這個(gè)商品
is_exsts = False
for item in self.cart:
if item.goods == goods:
# 說(shuō)明在購(gòu)物車中有該商品
is_exsts = True
item.count += count
# 減少庫(kù)存
goods.stock -= count
# 如果執(zhí)行到這,is_exsts的值還是False,說(shuō)明購(gòu)物車中沒(méi)有該商品
if is_exsts == False:
# 把商品添加到購(gòu)物車
goods.stock -= count
self.cart.append(CartItem(goods, count))
# 展示購(gòu)物車商品,計(jì)算總價(jià)
self.list_cart()
def run(self):
"""運(yùn)行應(yīng)用程序"""
print('智游電子產(chǎn)品商店')
print('v1.0')
print('\n')
self.list()
while True:
self.add_to_cart()
shop = Shop()
shop.run()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Python的Django框架結(jié)合jQuery實(shí)現(xiàn)AJAX購(gòu)物車頁(yè)面
- python實(shí)現(xiàn)簡(jiǎn)單購(gòu)物商城
- Python實(shí)現(xiàn)信用卡系統(tǒng)(支持購(gòu)物、轉(zhuǎn)賬、存取錢)
- Python 模擬購(gòu)物車的實(shí)例講解
- Python 實(shí)現(xiàn)購(gòu)物商城,含有用戶入口和商家入口的示例
- Python實(shí)現(xiàn)的購(gòu)物車功能示例
- python簡(jiǎn)單商城購(gòu)物車實(shí)例代碼
- Python實(shí)現(xiàn)購(gòu)物系統(tǒng)(示例講解)
- Python初學(xué)時(shí)購(gòu)物車程序練習(xí)實(shí)例(推薦)
- Python實(shí)現(xiàn)購(gòu)物車程序
相關(guān)文章
將imagenet2012數(shù)據(jù)為tensorflow的tfrecords格式并跑驗(yàn)證的詳細(xì)過(guò)程
這篇文章主要介紹了將imagenet2012數(shù)據(jù)為tensorflow的tfrecords格式并跑驗(yàn)證,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Python數(shù)據(jù)庫(kù)編程之pymysql詳解
本文主要介紹了Python數(shù)據(jù)庫(kù)編程中pymysql,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
關(guān)于tf.matmul() 和tf.multiply() 的區(qū)別說(shuō)明
這篇文章主要介紹了關(guān)于tf.matmul() 和tf.multiply() 的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python實(shí)現(xiàn)LR1文法的完整實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)LR1文法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python編寫(xiě)一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼
這篇文章主要介紹了Python編寫(xiě)一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序,本文給大家附上小編精心整理的源碼,需要的朋友可以參考下2019-12-12

