class Buyer: def __init__(self): self.data=GetData() self.opera_exl=OperationExcel() def check_salary_commodity_info(self,salary,index): if salary>=self.data.get_price(index): return self.opera_exl.get_row_values(index) else: return False def commodity_lists(self,salary,commodity_list): for i in range(1,self.opera_exl.get_rows()): check_salary_commodity_info=self.check_salary_commodity_info(salary,i) if check_salary_commodity_info: commodity_list.append(check_salary_commodity_info) return commodity_list def addin_bought_car(self,my_bought_car,commodity_list,salary): flag=None commodity_ID=input('请输入商品ID:') if commodity_ID.isdigit(): commodity_ID=int(commodity_ID) if commodity_ID<len(commodity_list) and commodity_ID>=0: my_bought_car.append(commodity_list[commodity_ID]) salary=salary-commodity_list[commodity_ID][1] print('将%s添加到购物车,剩余金额\033[31;1m%s\033[0m'%(commodity_list[commodity_ID],salary)) write_commodity=OperationExcel(sheetid=1) write_commodity.write_data(write_commodity.get_rows(),0,commodity_list[commodity_ID][0]) write_commodity.write_data(write_commodity.get_rows(),1,commodity_list[commodity_ID][1]) write_commodity.write_data(write_commodity.get_rows(),2,salary) flag=True elif commodity_ID=='q': self.quit_shopping(my_bought_car,salary) flag=False else: print('请输入正确商品ID号') flag=True return my_bought_car,salary,flag def quit_shopping(self,my_bought_car,salary): if my_bought_car: print('一共购买了',my_bought_car) print('余额还剩:\033[31;1m%s\033[0m'%salary) else: print('你未购买任何商品,再见!') def shopping(self): flag=True while flag: my_bought_car=[] salary=input('请输入工资:') if salary.isdigit(): salary=int(salary) while self.commodity_lists(salary,commodity_list=[]): commodity_list = [] commodity_list=self.commodity_lists(salary,commodity_list) if flag: for index,item in enumerate(commodity_list): print(index,item) my_bought_car,salary,flag=self.addin_bought_car(my_bought_car,self.commodity_lists(salary,commodity_list),salary) else: break else: self.quit_shopping(my_bought_car,salary) print('您的工资不足以继续买商品。。。') elif salary=='q': print('已退出') flag=False break else: print('请输入正确格式!') return flag
class Seller: def __init__(self): self.opera_exl=OperationExcel() def update_commodity_info(self,flag): for index,i in enumerate(range(1,self.opera_exl.get_rows())): print(index,self.opera_exl.get_row_values(i)) choose_id=input('请输入商品ID号:') if choose_id.isdigit(): choose_id=int(choose_id)+1 print(self.opera_exl.get_row_values(choose_id)) updata_price=input('请输入修改后的金额:') if updata_price.isdigit(): updata_price = int(updata_price) self.opera_exl.write_data(choose_id,1,updata_price) self.opera_exl=OperationExcel() print('修改成功',self.opera_exl.get_row_values(choose_id)) elif updata_price == 'q': print('已退出') flag = False else: print('输入错误,请重新输入:') elif choose_id=='q': print('已退出') flag=False else: print('输入错误,请重新输入:') return flag def add_commodity_info(self,flag): for index,i in enumerate(range(1,self.opera_exl.get_rows())): print(index,self.opera_exl.get_row_values(i)) add_commodity_name=input('请输入商品名称:') if add_commodity_name == 'q': print('已退出') flag = False add_commodity_price=input('请输入商品金额:') if add_commodity_price.isdigit(): add_commodity_price = int(add_commodity_price) self.opera_exl.write_data(self.opera_exl.get_rows(), 0, add_commodity_name) self.opera_exl.write_data(self.opera_exl.get_rows(), 1, add_commodity_price) self.opera_exl = OperationExcel() elif add_commodity_price == 'q': print('已退出') flag = False else: print('输入错误,请重新输入:') return flag def choose_do(self): flag=True while flag: choose_do=input('请输入要做的操作:(u/a)') if choose_do=='u': flag=self.update_commodity_info(flag) elif choose_do=='a': flag=self.add_commodity_info(flag) elif choose_do=='q': print('已退出') flag=False break else: print('输入错误,请重新输入:') return flag