def atmOperation(self, userID): '''ATM操作,总入口函数''' print('''请选择: 1、存款 2、取款 3、转账 4、查询余额 5、修改密码''') while True: choise = input().strip() if '1' == choise: # 存款 print('请输入存款金额:') money = self.inputMoney(userID) self.deposit(userID, money) print('存入%s元成功!' % (money)) self.turnBack(userID) break elif '2' == choise: # 取款 print('请输入取款金额!') money = self.inputMoney(userID) if self.withdrawals(userID, money): print('已取出%s元!请收好!' % (money)) self.turnBack(userID) break elif '3' == choise: # 转账 username = input('请输入要转账的账户名:') checkUser = u.Users().checkUser(username) if checkUser: print('请输入要转账金额:') money = self.inputMoney(userID) userID2 = u.Users().getUserID(username) if self.transfer(userID, userID2, money): print('已成功向%s转账%s元!' % (username, money)) else: print('账户不存在!') self.turnBack(userID) break elif '4' == choise: # 查询余额 self.queryBalance(userID) self.turnBack(userID) break elif '5' == choise: # 修改密码 u.Users().changePasswd(userID) self.turnBack(userID) break else: print('输入有误!')
def atmOperation(self, userID): '''atm operation, main foo''' print(''' Please select: 1. Deposit 2. Withdrawal 3. Transfer 4. Query 5. Change password ''') while True: choice = input().strip() if choice == '1': # Deposit print('Please input deposit amount: ') money = self.inputMoney(userID) self.deposit(userID, money) print('Deposit %s yuan successfully!' % money) self.turnBack(userID) break elif choice == '2': # withdrawal print('Please input withdrawal amount: ') money = self.inputMoney(userID) if self.withdrawal(userID, money): print('Take out %s yuan!' % money) self.turnBack(userID) break elif choice == '3': # transfer username = input('Please input transfer account: ') checkUser = u.Users().checkUser(username) if checkUser: print('Please input transfer account: ') money = self.inputMoney(userID) userID2 = u.Users().getUserID(username) if self.transfer(userID, userID2, money): print('Transfer %s yuan to %s!' % (money, username)) else: print('Account does not exist!') self.turnBack(userID) break elif choice == '4': # query balance self.queryBalance(userID) self.turnBack(userID) break elif choice == '5': # change password u.Users().changePassword(userID) self.turnBack(userID) break else: print('Invalid input!')
def deposit(self, userID, money): '''deposit''' # call Users and obtain user info users = u.Users().getUser() try: users[userID]['money'] = int(users[userID]['money']) + money except: # first deposit users[userID]['money'] = money # call Users and rewrite user info u.Users().setUser(users) # print('Account %s has saved %s yuan!' % (users[userID]['username'],money)) # print('The total balance is: %s yuan.' % users[userID]['money']) return True
def deposit(self, userID, money): '''存款''' # 调用用户类的获取用户信息方法 users = u.Users().getUser() try: users[userID]['money'] = int(users[userID]['money']) + money except: # 第一次存钱 users[userID]['money'] = money # 调用用户类,重新写入用户信息 u.Users().setUser(users) #print('账户%s已存入%s元!' % (users[userID]['username'],money) ) #print('总余额为: %s 元' % users[userID]['money']) return True
def auth(self): while True: username = input("请输入账户名>>>:").strip() password = input('请输入用户密码>>>:').strip() if len(username) == 0: print('\033[31;1m用户名不得为空\033[0m') else: auth = 'auth %s %s' % (username, password) self.client.send(auth.encode()) self.user_obj = users.Users(username) back_res = self.client.recv(1024).decode() if back_res == 'ok': print("认证成功") user = self.user_obj.get_user() self.current_user = username self.password = password self.home_path = user['home'] self.pwd_path = os.path.join(setting.USER_HOME, self.home_path, 'user_home') # 定义个默认路径 return True elif back_res == "300": print("密码错误") elif back_res == '301': print("用户不得同步登录") else: print("用户不存在")
def run(): atm = ATM() print('欢迎登录瑞士银行!!') # 登录验证,通过后返回userID值 username, userID = u.Users().userLoggin() if userID: atm.atmOperation(userID)
def withdrawal(self, userID, money): '''withdrwnal''' users = u.Users().getUser() try: int(users[userID]['money']) if money > users[userID]['money']: print('Balance is insufficient!') return False else: users[userID]['money'] -= money except: print('Balance is insufficient!') return False u.Users().setUser(users) # print('Account %s has withdrawaled %s yuan!' % (users[userID]['username'],money)) return True
def add_user(self,name,password): #添加用户 try: print('当前权限'+self.user_obj.role) if self.user_obj.role == 'administrator': user_file_path = conf.users_db_filepath + '\\' + name if not os.path.exists(user_file_path): os.mkdir(user_file_path) user_file = user_file_path + '\\' + name self.user_db = shelve.open(user_file,flag = "c", writeback = True) password_encrypt = hash.hash(password) user_obj = users.Users(name,password_encrypt) #添加hash模块加密密码 self.user_db[name]=user_obj user_home_path = conf.FTP_BASE + '\\' + name #print(user_home_path) if not os.path.exists(user_home_path): os.mkdir(user_home_path) if os.path.exists(user_file + '.dat') and os.path.exists(user_home_path): print('添加用户%s成功' % name) self.user_db.close() else: print('该用户已存在,无法添加') except Exception as e: print(e)
def run(): atm = ATM() print('Welcome log in Switzerland bank!') # log in verification, return userID after pass username, userID = u.Users().userLoggin() if userID: atm.atmOperation(userID)
def run(): s = ShoppingCart() print('Welcome to log in xxx website shopping mall!') # log in verification, return userID after pass username,userID = u.Users().userLoggin() if userID: s.shopping(username,userID)
def run(): s = ShoppingCar() print('欢迎登录XXX网上购物商城!') # 登录验证,通过后返回userID值 username,userID = u.Users().userLoggin() if userID: s.shopping(username,userID)
def init_admin_user(self,filepath): #初始化添加默认用户 try: self.admin_db = shelve.open(filepath,flag = "c", writeback = True) admin_obj = users.Users('admin',hash.hash('123456')) self.admin_db['admin']=admin_obj except Exception as e: print(e)
def withdrawals(self, userID, money): '''取款''' users = u.Users().getUser() try: int(users[userID]['money']) if money > users[userID]['money']: print('余额不足!!') return False else: users[userID]['money'] -= money except: print('余额不足!') return False u.Users().setUser(users) #print('账户%s已取出%s元!' % (users[userID]['username'],money) ) #print('总余额为: %s 元' % users[userID]['money']) return True
def queryBalance(self, userID): '''查询余额''' users = u.Users().getUser() try: print('账户%s的余额为:\n%s元' % (users[userID]['username'], users[userID]['money'])) except: print('账户%s的余额为:\n0元' % (users[userID]['username']))
def queryBalance(self, userID): '''query balance''' users = u.Users().getUser() try: print('Account %s balance is: \n%s yuan' % (users[userID]['username'], users[userID]['money'])) except: print('Account %s balance is: \n0 yuan' % users[userID]['username'])
def __init__(self): self.client = setting.socket.socket() self.client.connect(setting.IP_PORT) self.user_obj = users.Users() self.help_info = { "get":"用于上传文件,例如:get readme.txt 即 get 文件名", "put":"用于下载文件,例如:put readme.txt 即 put 文件名", 'dir':"用于显示当前目录下文件或文件详细信息 格式 ls " } if self.auth(): self.start()
def wrapper(*args, **kwargs): users = u.Users().getUser() f = open('logFile', 'a', encoding='utf-8') if func.__doc__ != 'check balance' and func.__doc__ != 'change password': f.write('%s\t%s%s%s yuan\n' % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), users[args[1]]['ussername'], func.__doc__, args[-1])) else: f.write('%s\t%s%s\n' % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), users[args[1]]['ussername'], func.__doc__)) f.close() return func(*args, **kwargs)
def wrapper(*args, **kwargs): users = u.Users().getUser() f = open(logFile, "a", encoding="utf-8") if func.__doc__ != '查询余额' and func.__doc__ != '修改密码': f.write('%s\t%s%s%s元\n' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), users[args[1]]['username'], func.__doc__, args[-1])) else: f.write('%s\t%s%s\n' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), users[args[1]]['username'], func.__doc__)) f.close() return func(*args, **kwargs)
def auth(self, *args): cmd = args[0] self.user_obj = users.Users(cmd[1]) auth_user = self.user_obj.get_user() print('in the auth_user:'******'status'] == 0: self.request.send(b"ok") #self.user_obj.update_status() self.user_home = auth_user["home"] else: self.request.send(b'301') print("用户不得同步登录") else: self.request.send(b"300") print('300:密码错误') else: self.request.send(b"302") print("用户不存在")
def shopping(self,username,userID): '''购物''' goods = g.Goods().printGoods() print(goods) while True: print('请选择,停止购物按q') choice = input().strip() if choice != 'q': if choice.isdigit(): choice = int(choice) self.buyList(username,choice) else: print('请输入有效数字!') else: # 获取购物车总价 sum = self.printBuyList(username) print('开始结算!') n = 2 while n >=0: password = input('请输入银行卡密码:') if u.Users().checkPassword(userID,password): # 调用银行卡接口结算 if a.ATM().withdrawals(userID,sum): print('结算成功!') return True else: print('结算失败!') else: if n !=0: print('密码错误!你还有%s次尝试机会!' % n) else: print('尝试次数已用完,结算失败!') n -=1 # 清空本次购物信息 #buyList = ['结算失败!'] #self.setBuyList(username, buyList) os.remove(ss.ShoppingCarFile(username)) return False
def shopping(self,username,userID): '''shopping''' goods = g.Goods().printGoods() print(goods) while True: print("Please select, press 'q' for stopping shopping") choice = input().strip() if choice != 'q': if choice.isdigit(): choice = int(choice) self.buyList(username,choice) else: print('Please input valid number!') else: # obtain total price in shopping cart sum = self.printBuyList(username) print('Start computing!') n = 2 while n >= 0: password = input('Please input credit card password: '******'Compute successfully!') return True else: print('Compute fail!') else: if n != 0: print('Password is incorrect! You have %s times opportunities!' % n) else: print('Opportunity has been used totally, compute fail!') n -= 1 # clear shopping info # self.setBuyList(username,buyList) os.remove(ss.ShoppingCartFile(username)) return False
def __init__(self): self.server = setting.socket.socket() self.server.bind(setting.IP_PORT) self.server.listen(5) self.user_obj = users.Users() #导入users文件并实例化Users类 self.start()