溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python如何實(shí)現(xiàn)電子產(chǎn)品商店

發(fā)布時(shí)間:2021-04-07 11:41:01 來源:億速云 閱讀:159 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹python如何實(shí)現(xiàn)電子產(chǎn)品商店,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

利用python實(shí)現(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

價(jià)格:¥3299.00

庫存:100

請輸入購買數(shù)量:2

--------------------------------------

Apple Watch(¥3299) * 2 =¥6598.00

--------------------------------------

總金額:¥6598.00

請輸入商品Id(回車去結(jié)賬,0清空購物車):2

--------------------------------------

Id:2

名稱:AirPods

價(jià)格:¥1288.00

庫存:100

請輸入購買數(shù)量:2

--------------------------------------

Apple Watch(¥3299.00) * 2 =¥6598.00

AirPods(¥1288.00) * 2      =¥2576.00

--------------------------------------

總金額:¥9174.00

1.首先,先在ProcessOn上畫出一個(gè)基本的流程圖,使自己有一個(gè)清晰的邏輯,如何去寫這個(gè)項(xiàng)目,流程圖如下:

python如何實(shí)現(xiàn)電子產(chǎn)品商店

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

python如何實(shí)現(xiàn)電子產(chǎn)品商店

3.根據(jù)流程圖和UML模板編寫程序,代碼如下:

(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)打印對象時(shí),輸出的內(nèi)容
 def __str__(self):
 return 'id:%s\n' \
  '名稱:%s\n' \
  '價(jià)格:%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)定義一個(gè)類名為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)
 
 # 計(jì)算商品小計(jì)
 def amout(self):
 return self.goods.price * self.count
 
 
if __name__ == '__main__':
 goods = Goods('Apple pods',2999,100)
 # 創(chuàng)建購物車商品對象,需要傳入一個(gè)商品對象
 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):
 # 存儲所有商品
 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):
 """展示購物車商品,計(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):
 """添加商品到購物車"""
 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:
  # 計(jì)算商品索引
  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ù)量
  # 變量表示在購物車中是否有這個(gè)商品
  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))
 
  # 展示購物車商品,計(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()

以上是“python如何實(shí)現(xiàn)電子產(chǎn)品商店”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI