bank.py
__author__ = 'zhaobin022' import json import tools import getpass import datetime class DatabaseHelper: def __init__(self): self.database = 'db.json' ''' self.accountDic = {'zhangsan': {'total':0, 'password':tools.getMd5Password('123'), 'status':True }, 'lisi': {'total':0, 'password':tools.getMd5Password('456'), 'status':True } } ''' self.accountDic = self.readJson() # self.__writeJson() def readJson(self): with open(self.database) as f: return json.load(f) def writeJson(self): with open(self.database,'w') as f: json.dump(self.accountDic,f) class Bank: def __init__(self): self.dh = DatabaseHelper() def __login(f): def inner(self,*args, **kwargs): username = raw_input("username : ") username = username.strip() accountDic = self.dh.accountDic if accountDic.get(username): self.username = username count = 0 if not accountDic[username]['status']: return False,'You account has been locked !' while True: password = raw_input("passowrd : ") if accountDic[username]['password'] == tools.getMd5Password(password): return f(self,*args,**kwargs) else: print "Please input the right password :" count += 1 if count == 3: accountDic[username]['status'] = False self.dh.writeJson() return False,'Your account have been locked ! You have inputed wrong password three times ' else: return False , '''Don't have this account''' return inner def logout(self,username,password): pass @__login def getMoney(self, number): #number = kwargs['number'] if number <= 0: return False,"Please input the right number" accountDic = self.dh.accountDic.get(self.username) if accountDic['total'] >= number: accountDic['total']-=float('%0.2f'%number) accountDic['total']=float('%0.2f'%accountDic['total']) self.dh.writeJson() return True,'get %0.2f money , your balance is %0.2f ' % (number,accountDic['total']) else: return False, """You don't have enough money !""" @__login def queryMoney(self): if self.dh.accountDic.get(self.username): return True,self.dh.accountDic.get(self.username).get('total') @__login def returnMoney(self,number): number = float('%s'% number) if number <= 0: return False,"Please input the right number" accountDic = self.dh.accountDic.get(self.username) accountDic['total']+=float('%0.2f'%number) self.dh.writeJson() return True,'You change %0.2f , and your balance is %0.2f ' % (number,accountDic['total']) @__login def forwardMoney(self): destinationnAccount = raw_input("destinationnAccount") destinationnAccount = self.dh.accountDic.get(destinationnAccount.strip()) account = self.dh.accountDic.get(self.username) if destinationnAccount: number = raw_input("number :") number = float('%s'% number.strip()) if account['total'] >= number: account['total']-=number account['total']=float('%0.2f'%account['total']) destinationnAccount['total']+=number self.dh.writeJson() return True,'your balance is %0.2f ' % (account['total']) else: return False,"You don't have money to transfer accounts !" else: return False,'The destinationn account not exist !' @__login def flushCard(self,number): if number <= 0: return False,"Please input the right number" account = self.dh.accountDic.get(self.username) if account['total'] >= number: account['total']-=float('%0.2f'%number) account['total']=float('%0.2f'%account['total']) self.dh.writeJson() return True,'flush %0.2f money , your balance is %0.2f ' % (number,account['total']) else: return False, """You don't have enough money !"""
db.json
{"lisi": {"status": true, "password": "250cf8b51c773f3f8dc8b4be867a9a02", "total": 2950.0}, "zhangsan": {"status": true, "password": "202cb962ac59075b964b07152d234b70", "total": 11000.0}}
goods.json
{"Python": {"count": 2, "price": 50}, "TV": {"count": 10, "price": 1500}, "Bike": {"count": 3, "price": 500}}
main.py
__author__ = 'zhaobin022' from bank import Bank from shop import Shop from prettytable import PrettyTable if '__main__' == __name__: while True: print ''' 1 . Shoping 2 . ATM 3 . Quit ''' choice = raw_input("input your choice") choice = int(choice) if choice == 1: s = Shop() while True: goodDic = s.listGoods() print 'go to make deal input (commit) exit input exit' good_choice = raw_input("input your choice") if good_choice in goodDic.keys(): count = raw_input("please input the count") print goodDic[good_choice] print s.addCart(goodDic[good_choice],count) print 'Cart List' s.listCart() elif good_choice.strip() == 'commit': print s.makeDeal('lisi') elif good_choice.strip() == 'exit': break elif choice == 2: b = Bank() while True: print ''' 1 . getMoney 2 . queryMoney 3 . returnMoney 4 . forwardMoney 5 . quit ''' choice = raw_input("input your choice :") if choice.strip() == '1': #username = raw_input("input your username :") number = raw_input("input your number :") print b.getMoney(number=int(number)) elif choice.strip() == '2': # username = raw_input("input your username :") print b.queryMoney() elif choice.strip() == '3': number = raw_input("input your number :") print b.returnMoney(number=number) elif choice.strip() == '4': print b.forwardMoney() elif choice.strip() == '5': break elif choice == 3: break # b = Bank() # s = Shop() # print 'goods list' # s.listGoods() # print 'cart list' # s.listCart() # print s.addCart('Bike',2) # s.listCart() # s.makeDeal(username='lisi')# # status , obj = b.queryMoney(username='lisi') #print b.getMoney(username='zhangsan',number=5.23) # print b.returnMoney(username='zhangsan',number=0.77) # print b.forwardMoney(username='zhangsan',number=0.77,destinationnAccount='lisi')
shop.py
__author__ = 'zhaobin022' import bank import json import copy from prettytable import PrettyTable class DatabaseHelper: def __init__(self): self.database = 'goods.json' ''' self.goods = { 'Bike':{'price':500,'count':4}, 'Python':{'price':50,'count':2}, 'TV':{'price':1500,'count':3}, } ''' self.goods = self.readJson() # self.__writeJson() def readJson(self): # f = open(self.database) with open(self.database) as f: self.goods = json.load(f) return self.goods def writeJson(self): # f = open(self.database,'w') with open(self.database,'w') as f: json.dump(self.goods,f) class Shop: def __init__(self): self.dh = DatabaseHelper() self.shopCart = {} def addCart(self,name,count): if self.dh.goods.has_key(name): v = copy.deepcopy(self.dh.goods.get(name)) if v.get('count') >= int(count): v['count'] = int(count) self.shopCart[name]=v return True,'''add cart successfull''' else: return False,'''don't have enough goods ''' def delFromCart(self,name,count): if self.shopCart.get(name): v = self.shopCart.get(name) if count <= v['count']: v['count'] -= count self.shopCart[name]=v return True , '''del Cart successfull''' else: return False,'''cart don't have enough good ''' def listCart(self): x = PrettyTable(["Name", "Count", "UnitPrice"]) sum = 0 for k,v in self.shopCart.items(): x.add_row([k,v['count'], v['price']]) sum += v['price']*v['count'] print x print 'Total : %s' % sum def listGoods(self): tempDic = {} print "Goods List" x = PrettyTable(["Id", "Name", "Count"]) x.padding_width = 1 for k,v in enumerate(self.dh.goods.items()): x.add_row([k,v[0], v[1]['count']]) # print k,'----------',v[0],'---------',v[1]['count'] tempDic[str(k)]=v[0] print x return tempDic def makeDeal(self,username): sum = 0 for k,v in self.shopCart.items(): sum += v.get('price') * v.get('count') b = bank.Bank() status,obj = b.flushCard(number=sum) if status: for k,v in self.shopCart.items(): self.dh.goods[k]['count'] -= v['count'] self.dh.writeJson() return status,obj else: return False,obj
tools.py
__author__ = 'zhaobin022' import hashlib def getMd5Password(password): m = hashlib.md5() m.update(password) return m.hexdigest()