Try   HackMD

高慶霖

Problem2:

程式說明(程式架構,測試方式…):

  • Product(廠商可自行輸入)、ShoppingCart、Shop三個Class建立基本商店
  • 區分消費者與廠商(1、0)的功能
    • 廠商:加入產品
    • 消費者:將商品加入購物車、移除購物車中的產品、顯示購物車中的內容、結帳並顯示總金額

程式執行截圖:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

程式碼:

""" 電商系統,系統中有「生活用品」,「書籍」,和「電子產品」,「購物車」 請使用物件導向設計此電商系統所需之類別,並測試系統的運作,請儘量使用所學之物件導向觀念。 """ class Product: def __init__(self, product_id, name, price): self.product_id = product_id self.name = name self.price = price class ShoppingCart: def __init__(self): self.items = {} def add_item(self, product, quantity): if product.product_id in self.items: self.items[product.product_id]['quantity'] += quantity else: self.items[product.product_id] = { 'product': product, 'quantity': quantity } def remove_item(self, product, quantity): if product.product_id in self.items: if self.items[product.product_id]['quantity'] <= quantity: del self.items[product.product_id] else: self.items[product.product_id]['quantity'] -= quantity def get_total_price(self): total_price = 0 for item in self.items.values(): total_price += item['product'].price * item['quantity'] return total_price class Shop: def __init__(self): self.products = {} def add_product(self, product): self.products[product.product_id] = product def remove_product(self, product): if product.product_id in self.products: del self.products[product.product_id] def display_products(self): #print("Product List:") for product in self.products.values(): print(f"{product.product_id}. {product.name} - ${product.price:.2f}") def checkout(self, cart): total_price = cart.get_total_price() print(f"Total Price: ${total_price:.2f}") return total_price def main(): # 初始化購物系統 shop = Shop() product_num = [] product_m = [] #系統開始 while True: while True: print("請輸入身分別: ") print("廠商:0") print("消費者:1") print("退出程式:#") try: id = str(input()) if(id!="0" and id!="1" and id!="#"): raise else: break except: print("輸入錯誤,請重新輸入") # 加入產品 if(id=="0"): i = 1 while True: print("加入新商品格式:商品名稱/價錢") print("完成後,請輸入exit/0") try: new_product, new_product_price = map(str,input().split("/")) new_product = new_product.strip() if(new_product == "exit"): i = 0 break else: product = Product(i, new_product, float(new_product_price)) print("=加入成功=") product_num.append(new_product) product_m.append(float(new_product_price)) i += 1 shop.add_product(product) except: print("格式錯誤,請重新輸入") elif(id=="1"): # 顯示產品列表 #shop.display_products() # 初始化購物車 cart = ShoppingCart() # 加入購物車 # 移除購物車中的產品 # 顯示購物車中的內容 # 結帳並顯示總金額 while True: print("若要開始購物,請輸入0") print("若要移除購物車商品,請輸入1") print("若要顯示購物車中的內容,請輸入2") print("若要結帳,請輸入3") print("若要結束購物,請輸入exit") try: q = str(input()) if(q!="0" and q!="1" and q!="2" and q!="3" and q!="exit"): raise if(q=="0"): # 加入購物車 shop.display_products() buy, num_b = map(str,input("購物格式:商品名稱/購買數量 ").split("/")) buy = buy.strip() cart.add_item(Product(product_num.index(buy)+1, buy, product_m[product_num.index(buy)]),int(num_b)) print("=加入成功=") elif(q=="1"): # 移除購物車中的產品 print("Shopping Cart Contents: ") for item in cart.items.values(): print(f"{item['product'].name} x {item['quantity']}") d, num_d = map(str,input("購物格式:商品名稱/移除數量 ").split("/")) d = d.strip() cart.remove_item(Product(product_num.index(d)+1, d, product_m[product_num.index(d)]),int(num_d)) print("=移除成功=") elif(q=="2"): # 顯示購物車中的內容 print("Shopping Cart Contents: ") for item in cart.items.values(): print(f"{item['product'].name} x {item['quantity']}") elif(q=="3"): # 結帳並顯示總金額 shop.checkout(cart) break elif(q=="exit"): break except: print("輸入錯誤,請重新輸入") elif(id=="#"): break if __name__ == "__main__": main() """ Groceries 10.00 Books 20.00 Electronics 15.00 exit 0 """ """ Groceries 10 Books 20 Electronics 15 """