使用python3简单完成购物过程

功能:
要求用户输入购物卡余额,打印购物清单
用户可以连续的购买商品,直至钱不够为止
退出时,打印用户已经购买的商品信息和剩余金额

本次技术要求:使用list, dict, function,class。不能使用数据库,下一个版本会使用Django完成类似功能

代码如下:

  1 import sys
  2 import copy
  3 
  4 
  5 class Product:
  6     def __init__(self, pid, name, price, count):
  7         self.pid = pid
  8         self.name = name
  9         self.price = price
 10         self.count = count
 11 
 12     def __str__(self):
 13         return "id>>" + str(self.pid) + "  \tname>>" + self.name+"  \tprice>>" + str(self.price) + " \tcount>>" + str(self.count)
 14 
 15 
 16 class ProductList:
 17     # 输入列表,抓成以id为key的字典,便于以后查找
 18     def __init__(self, products=[]):
 19         self.prod_list = dict()
 20         for product in products:
 21             self.prod_list[product.pid] = product
 22 
 23     def minus(self, productId, num):
 24         if productId in self.prod_list:
 25             if self.prod_list[productId].count > num:
 26                 self.prod_list[productId].count -= num
 27                 return True
 28             elif self.prod_list[productId].count == num:
 29                 self.prod_list.pop(productId)
 30                 return True
 31             else:
 32                 print("库存不够,请重新选择数量")
 33                 return False
 34         else:
 35             print("本仓库没有此件商品,请重新选择")
 36 
 37     def add(self, product, num=1):
 38         if product.pid in self.prod_list:
 39             self.prod_list[product.pid].count += num
 40         else:
 41             new_product = copy.deepcopy(product)
 42             new_product.count = num
 43             self.prod_list[new_product.pid] = new_product
 44 
 45     def print_all_product(self):
 46         for key in self.prod_list:
 47             print(self.prod_list[key])
 48 
 49 
 50 class Shopping:
 51     @classmethod
 52     def get_salary(cls):
 53         salary = input("请输入您购买卡的余额:")
 54         if not salary.isdecimal():
 55             print("您输入的卡的余额不是数值型,请重新输入 ")
 56             return Shopping.get_salary()
 57         else:
 58             return int(salary)
 59 
 60     def __init__(self, sell_product=[]):
 61         self.salary = Shopping.get_salary()
 62         self.sell_product = ProductList(sell_product)
 63         self.buy_product = ProductList()
 64 
 65     def buy_some_product(self):
 66         self.sell_product.print_all_product()
 67         select_product = input("请输入您的产品编码: ")
 68         product_num = input("请输入商品的数量: ")
 69         if product_num.isdigit():
 70             product_num = int(product_num)
 71         else:
 72             print("数量必须是数字!")
 73             self.buy_some_product()
 74         # 如果够支付,则工资减少,同时buy_product增加一个商品
 75         if self.is_afford(select_product, product_num):
 76             self.buy_product.add(self.sell_product.prod_list[select_product], product_num)
 77             self.salary -= self.sell_product.prod_list[select_product].price*product_num
 78             self.sell_product.minus(select_product, product_num)
 79             print("您当前购买商品如下:")
 80             self.buy_product.print_all_product()
 81             print("您的余额是 %s 元" % self.salary)
 82         else:
 83             print("您的余额不足,请重新选择")
 84 
 85     # 判断当前的工资余额是否够支付产品
 86     def is_afford(self, procudtId, product_num):
 87         if type(procudtId) == int:
 88             procudtId = str(procudtId)
 89         if self.salary >= self.sell_product.prod_list[procudtId].price * product_num:
 90             return True
 91         else:
 92             return False
 93 
 94     def select_process(self):
 95         while True:
 96             selector = input("""
 97                 请输入您的选择?
 98                     购买(b)
 99                     退出(q)
100                 """)
101             if selector == 'b':
102                 self.buy_some_product()
103             else:
104                 print("您购买了如下商品")
105                 self.buy_product.print_all_product()
106                 print("您的余额是 %s 元" % self.salary)
107                 print("欢迎您下次光临")
108                 sys.exit(0)
109 
110 
111 
112 if __name__ == '__main__':
113     # 测试初始化产品列表的信息的功能
114     init_product=[Product('1', 'bike', 100, 10), Product('2', 'ipad', 3000, 3), Product('3', 'paper', 500, 30)]
115     shop = Shopping(init_product)
116     shop.select_process()

类product:产品的基本信息

ProductList:以产品pid为key来存储许多产品,同时提供增加、减少产品数量的功能

Shopping:主要完成购物功能。初始化销售产品列表和购物卡余额。循环购物的过程。

注意:

ProductList.add(self, product, num=1)一定不能使用输入的product的信息,要保证各个列表中的product的信息是独立(产品中的数量是独立的)。

说明:这个小功能仅仅是练习。距离产品还有很大的差距。

原文地址:https://www.cnblogs.com/yangshl/p/6291335.html