python:实现简单购物车增加商品、删除商品的功能
作业:实现购物车可以添加手机、电脑等功能
手机类:
# -*- coding: utf-8 -*- class IphoneInfo: def __init__(self): self._title = '' self._price = 0 self._color = '' self._version = '' self._weight = '' def set_title(self, title): self._title = title def get_title(self): return self._title def set_price(self, price): self._price = price def get_price(self): return self._price def set_color(self, color): self._color = color def get_color(self): return self._color def set_version(self, version): self._version = version def get_version(self): return self._version def set_weight(self, weight): self._weight = weight def get_weight(self): return self._weight
电脑类:
# -*- coding: utf-8 -*- class ComputerInfo: def __init__(self): self._title = '' self._price = 0 self._color = '' self._version = '' self._weight = '' self._cpu = '' self._rom = '' self._ssd = '' self._hhd = '' def set_title(self, title): self._title = title def get_title(self): return self._title def set_price(self, price): self._price = price def get_price(self): return self._price def set_color(self, color): self._color = color def get_color(self): return self._color def set_version(self, version): self._version = version def get_version(self): return self._version def set_weight(self, weight): self._weight = weight def get_weight(self): return self._weight def set_cpu(self, cpu): self._cpu = cpu def get_cpu(self): return self._cpu def set_rom(self, rom): self._rom = rom def get_rom(self): return self._rom def set_ssd(self, ssd): self._ssd = ssd def get_ssd(self): return self._ssd def set_hhd(self, hhd): self._hhd = hhd def get_hhd(self): return self._hhd if __name__ == '__main__': mac1 = ComputerInfo() mac1.set_title('big mac 13\'') mac1.set_price(3999) mac1.set_color('white') print(mac1.get_title()) print(mac1.get_price()) print(mac1.get_color())
购物车类:
# -*- coding: utf-8 -*- from IphoneInfo import IphoneInfo from ComputerInfo import ComputerInfo class Cart: def __init__(self): self._deal_list = [] self._deal_num = {} # add deal to cart def add_deal(self, deal_list): self._deal_list.append(deal_list) def get_add_deal(self): return self._deal_list # delete deal from cart def del_deal(self, del_name): self._deal_list.remove(del_name) def get_del_deal(self): return self._deal_list # 设置所有商品的默认个数 def set_default_deal_num(self, deal_num=1): for i in range(len(self.get_add_deal())): # 把list的商品作为字典的key if self.get_add_deal()[i] in self._deal_num: self._deal_num[self.get_add_deal()[i].get_version()] += deal_num else: self._deal_num[self.get_add_deal()[i].get_version()] = deal_num # 设置单个商品个数 def change_onedeal_num(self, version='iphone6', deal_num=1): # version = input('input the version which you want to change numbers:') # deal_num = input('input the numbers(eg:1/-1)') if self._deal_num.get(version) + deal_num > 0: self._deal_num[version] += deal_num else: print('\033[1;31;0m change faild!\033[0m') # 返回商品+商品数据 def get_alldeal_num(self): for key,value in self._deal_num.items(): print(key, ':', value) return self._deal_num #展示购物车的商品 def show_cart(self): print('the deals which in the cart :') sum_price = 0 sum_dealnum = 0 for deal_x in self.get_add_deal(): # print(type(deal)) # print(type(deal_x)) print('the title :', deal_x.get_title()) print('the price :', deal_x.get_price()) print('the color :', deal_x.get_color()) print('the version :', deal_x.get_version()) #获取deal对应商品数量 numbers = self._deal_num.get(deal_x.get_version()) print('numbers:',numbers) print('-----------------') sum_price += deal_x.get_price() * numbers sum_dealnum += 1 print('your cart totle deal:', sum_dealnum, ', ', sum_price, 'yuan') print('-----------------------------') print('--------next cart------------') def del_deal_from_cart(self): not_like = input('delete the deal version which you do not like:') flag=0 for deal_x in self.get_add_deal(): if deal_x.get_version() == not_like: self.del_deal(deal_x) print('\033[1;32;0m delete success!\033[0m') print('\033[1;32;0m',deal_x.get_version(), 'is deleted\033[0m') print('--------------') flag = 1 if flag == 0: print('\033[1;31;0m delete failed!\033[0m') print('\033[1;31;0m',not_like, 'is not in cart!\033[0m') def commit_order(): flag = input('Do you want to settle your deals (Y/N):') if flag == 'Y' or flag == 'y': print('deal commit,it is ready going to cashier!') else: print('deal not commit!') #mock 购物车的数据 def mock_deal(): iphone6 = IphoneInfo() iphone6.set_title('iphone6\'s title') iphone6.set_price(3999) iphone6.set_version('iphone6') iphone6.set_color('white') iphone6.set_weight('168g') iphone7 = IphoneInfo() iphone7.set_title('iphone7\'s title') iphone7.set_price(4999) iphone7.set_version('iphone7') iphone7.set_color('red') iphone7.set_weight('200g') iphone8 = IphoneInfo() iphone8.set_title('iphone8\'s title') iphone8.set_price(7999) iphone8.set_version('iphone8') iphone8.set_color('silvery') iphone8.set_weight('268g') mac13 = ComputerInfo() mac13.set_title('mac 13\'s title') mac13.set_price(6888) mac13.set_version('mac13') mac13.set_color('green') mac13.set_weight('1.3kg') mac13.set_cpu('i5') mac13.set_rom('4G') mac13.set_ssd('128G') mac13.set_hhd('1T') mac14 = ComputerInfo() mac14.set_title('mac 14\'s title') mac14.set_price(8888) mac14.set_version('mac14') mac14.set_color('red') mac14.set_weight('1.4kg') mac14.set_cpu('i5') mac14.set_rom('8G') mac14.set_ssd('256G') mac14.set_hhd('1T') mac15 = ComputerInfo() mac15.set_title('mac 15\'s title') mac15.set_price(10888) mac15.set_version('mac15') mac15.set_color('red') mac15.set_weight('1.5kg') mac15.set_cpu('i7') mac15.set_rom('16G') mac15.set_ssd('512G') mac15.set_hhd('2T') new_cart = Cart() new_cart.add_deal(iphone6) new_cart.add_deal(iphone7) new_cart.add_deal(iphone8) new_cart.add_deal(mac13) new_cart.add_deal(mac14) new_cart.add_deal(mac15) return new_cart if __name__ == '__main__': deals = mock_deal() #设置购物车内所有商品的默认数量,default=1 deals.set_default_deal_num(2) #展示购物车 deals.show_cart() #设置商品个数/可拓展成用户输入version numbers deals.change_onedeal_num('mac13',-1) deals.show_cart() #用户从购物车中删除商品 deals.del_deal_from_cart() #展示购物车 deals.show_cart() #确认提交订单 commit_order() print('------over-----')
实现结果:可以实现购物车增加商品、删除某个商品、修改某个商品的个数等功能
学好python自动化,工作效率顶呱呱