目的

掌握字典数据用记事本存取与函数,变量的调用

实现

代码

  from Admin import admin
  from atm import ATM
  import pickle
  import os
  import time
  
  
  def main():
      Admin = admin()
      Admin.printAdminView()
      if Admin.adminLoginAndOff():
          return -1
      else:
          print("成功登陆!")
          time.sleep(4)
  
      absPath = os.path.join(os.getcwd(), "allUsers.txt")
      if not os.path.isfile(absPath):
          f = open(absPath, "w")
          f.close()
      f = open(absPath, "rb")
      allUsers = pickle.load(f)
      atm = ATM(allUsers)
  
      def pinkleDump():
          f = open(absPath, "wb")
          pickle.dump(atm.allUsers, f)
          f.close()
  
      while True:
          Admin.printFunctionView()
          option = int(input("您想要(请输入序号):"))
  
          if option == 1:
              atm.getMoney()
              pinkleDump()
          elif option == 2:
              atm.saveMoney()
              pinkleDump()
          elif option == 3:
              atm.checkAccount()
          elif option == 4:
              atm.transferMoney()
              pinkleDump()
          elif option == 5:
              atm.rePassword()
              pinkleDump()
          elif option == 6:
              atm.delAccount()
              pinkleDump()
          elif option == 7:
              atm.lockAccount()
              pinkleDump()
          elif option == 8:
              atm.unlockAccount()
              pinkleDump()
          elif option == 9:
              atm.newAccount()
              pinkleDump()
          elif option == 0:
              atm.renewAccount()
              pinkleDump()
          elif option == 10:
              if not Admin.adminLoginAndOff():
                  print("成功退出!")
                  time.sleep(1)
                  pinkleDump()
                  return -1
          time.sleep(1)
  
  
  
  
  if __name__ == "__main__":
      main()
  from user import User
  from bankCard import BandCard
  import random
  
  
  class ATM(object):
      def __init__(self, allUsers):
          self.allUsers = allUsers
  
      def newAccount(self):
          userName = input("请输入开户人的名字:")
          idCard = input("请输入开户人的身份证号码:")
          phoneNum = input("请输入开户人的电话号码:")
          preStore = int(input("请输入预存款金额:"))
          if preStore <= 0:
              print("预存款金额错误!开户失败......")
              return -1
  
          firstPass = input("请设置账户密码:")
          if not self.checkPassword(firstPass):
              return -1
          bankCardId = self.randomCardId()
  
          Card = BandCard(bankCardId, firstPass, preStore)
          user = User(userName, idCard, phoneNum, Card)
          self.allUsers[bankCardId] = user
          print("卡号是(%s),请牢记: " % (bankCardId))
  
      def checkAccount(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,请重新输入卡号......")
              return -1
          if user.bankCardId.lockCard:
              print("卡号已冻结,查询失败......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              user.bankCardId.lockCard = True
              print("多次密码输入错误,帐号被冻结......")
              return -1
          print("帐号: %s    余额:  %s" % (user.bankCardId.bankCardId, user.bankCardId.bankCardMoney))
  
      def getMoney(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,请重新输入卡号......")
              return -1
          if user.bankCardId.lockCard:
              print("卡号已冻结,查询失败......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              user.bankCardId.lockCard = True
              print("多次密码输入错误,帐号被冻结......")
              return -1
          money = int(input("输入取款金额:"))
          if user.bankCardId.bankCardMoney >= money:
              user.bankCardId.bankCardMoney = user.bankCardId.bankCardMoney - money
              print("取款成功!您的卡%s余额为:%d  " % (account, user.bankCardId.bankCardMoney))
          else:
              print("余额不足!您的卡%s余额为:%d  " % (account, user.bankCardId.bankCardMoney))
  
      def saveMoney(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,请重新输入卡号......")
              return -1
          if user.bankCardId.lockCard:
              print("卡号已冻结,查询失败......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              user.bankCardId.lockCard = True
              print("多次密码输入错误,帐号被冻结......")
              return -1
          money = int(input("输入取款金额:"))
          user.bankCardId.bankCardMoney += money
          print("存款成功!您的卡%s余额为:%d  " % (account, user.bankCardId.bankCardMoney))
  
      def transferMoney(self):
          account = input("请输入你的卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,请重新输入卡号......")
              return -1
          if user.bankCardId.lockCard:
              print("卡号已冻结,查询失败......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              user.bankCardId.lockCard = True
              print("多次密码输入错误,帐号被冻结......")
              return -1
          print("您的卡%s余额为:%d  " % (account, user.bankCardId.bankCardMoney))
          account1 = input("请输入你要转入的卡号:")
          user1 = self.allUsers.get(account1)
          if not user1:
              print("卡号不存在,请重新输入卡号......")
              return -1
          if user1.bankCardId.lockCard:
              print("卡号已冻结,查询失败......")
              return -1
          print("您的卡%s余额为:%d  " % (account1, user1.bankCardId.bankCardMoney))
          money = int(input("输入转账金额:"))
          if user.bankCardId.bankCardMoney >= money:
              user.bankCardId.bankCardMoney -= money
          else:
              print("余额不足......")
          user1.bankCardId.bankCardMoney += money
          print("您的卡%s余额为:%d  " % (account, user.bankCardId.bankCardMoney))
          print("您的卡%s余额为:%d  " % (account1, user1.bankCardId.bankCardMoney))
  
      def lockAccount(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,锁定失败......")
              return -1
          if user.bankCardId.lockCard:
              print("卡号已冻结,无需锁定......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              print("密码输入错误,锁定失败......")
              return -1
          user.bankCardId.lockCard = True
          print("该卡已锁定......")
  
      def unlockAccount(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,解锁失败......")
              return -1
          if not user.bankCardId.lockCard:
              print("卡号没有冻结,无需解锁......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              print("密码输入错误,锁定失败......")
              return -1
          user.bankCardId.lockCard = False
          print("该卡已解锁......")
  
      def rePassword(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,解锁失败......")
              return -1
          if user.bankCardId.lockCard:
              print("卡号没有冻结,无需解锁......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              print("密码输入错误,锁定失败......")
              return -1
          while True:
              repass = input("请输入新密码:")
              repass1 = input("请再次输入新密码:")
              if repass == repass1:
                  user.bankCardId.bankCardPass = repass
                  print("你的新密码是" + repass + ",请妥善保存好!")
                  break
              else:
                  print("两次输入的密码不一样,请重新再输入")
  
      def renewAccount(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,解锁失败......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              print("密码输入错误,锁定失败......")
              return -1
          newAccount = self.randomCardId()
          self.allUsers[newAccount] = self.allUsers[account]
          (self.allUsers[newAccount]).bankCardId.bankCardId = newAccount
          del self.allUsers[account]
          print("旧卡(%s)删除,新卡号为(%s)" % (account, (self.allUsers[newAccount]).bankCardId.bankCardId))
  
      def delAccount(self):
          account = input("请输入卡号:")
          user = self.allUsers.get(account)
          if not user:
              print("卡号不存在,解锁失败......")
              return -1
          if not self.checkPassword(user.bankCardId.bankCardPass):
              print("密码输入错误,锁定失败......")
              return -1
          del self.allUsers[account]
          print("销户成功!")
  
      def checkPassword(self, realPassword):
          for i in range(3):
              tempPassword = input("请输入密码:")
              if tempPassword == realPassword:
                  return True
              else:
                  print("密码输入错误!开户失败......")
  
      def randomCardId(self):
          rCardNum = ""
          for i in range(6):
              ch = str(random.randrange(0, 9, 1))
              rCardNum += ch
          if not self.allUsers.get(rCardNum):
              return rCardNum
  class User(object):
      def __init__(self, userName, idCard, phoneNum, bankCardId):
          self.bankCardId = bankCardId
          self.idCard = idCard
          self.userName = userName
          self.phoneNum = phoneNum
  class BandCard(object):
      def __init__(self, bankCardId, bankCardPass, bankCardMoney):
          self.bankCardId = bankCardId
          self.bankCardPass = bankCardPass
          self.bankCardMoney = bankCardMoney
          self.lockCard = False
  import time
  class admin(object):
      adminUser = "8"
      adminPass = "8"
      def printAdminView(self):
          print("#################################################################")
          print("#                                                               #")
          print("#                                                               #")
          print("#                                                               #")
          print("#                      Davis ATM SYSTEM                         #")
          print("#                                                               #")
          print("#                                                               #")
          print("#                                                               #")
          print("#################################################################")
  
      def printFunctionView(self):
          print("#################################################################")
          print("#        1:取钱                            2:存钱               #")
          print("#        3:查询                            4:转账               #")
          print("#        5:改密                            6:销户               #")
          print("#        7:冻结                            8:解冻               #")
          print("#        9:开户                            0:补卡               #")
          print("#                        10:退出                               #")
          print("#################################################################")
      def adminLoginAndOff(self):
          adminUserInput = input("请输入管理员账号:")
          adminPassInput = input("请输入管理员密码:")
          if not adminUserInput == self.adminUser:
              print("无效的管理员账户!")
              return -1
          elif not adminPassInput == self.adminPass:
              print("无效的管理员密码!")
              return -1