1. 程式人生 > >python練習之銀行模擬系統

python練習之銀行模擬系統

建立 atm.py
from card import Card
from user import User
import random


class Atm(object):
    def __init__(self):
        self.user_dict = {}


    def print_ui(self):
        print("".center(50, "*"))
        print("*".ljust(16, " "), "開戶(1)  查詢(2)", " ".rjust(13, " "), "*")
        print("*".ljust(16
, " "), "取款(3) 存款(4)", " ".rjust(13, " "), "*") print("*".ljust(16, " "), "轉賬(5) 改密(6)", " ".rjust(13, " "), "*") print("*".ljust(16, " "), "鎖定(7) 解鎖(8)", " ".rjust(13, " "), "*") print("*".ljust(16, " "), "銷戶(9) 退出(10)", " ".rjust(12, " "), "*") print("".center(50, "
*")) def openAcount(self): name=input("請輸入姓名:") phonenum=int(input("請輸入電話號碼:")) identycard=input("請輸入身份證號碼:") passwd=input("請輸入密碼:") passwd1=input("請再輸入密碼:") if passwd!=passwd1: print("兩次密碼不一致,開戶失敗") return money=float
(input("請輸入存款金額:")) if money<10: print("存款金額少於10元,開戶失敗") return cardid=random.randrange(1000000,9999999) print("您的卡號為:",cardid) card=Card(cardid,passwd,money) user=User(name,identycard,phonenum,card) print("開戶成功") self.user_dict[cardid]=user def Search(self): cardid=int(input("請輸入卡號:")) user=self.user_dict.get(cardid) if user==None: print("卡號不存在") return if user.card.islock==True: print('卡被鎖定,請先解鎖') return for i in range(3): passwd=input("請輸入密碼:") if user.card.passwd!=passwd: print("密碼錯誤") else: break else: print("機會已用完,卡被鎖了") user.card.islock = True return print("使用者名稱:%s,身份證號%s:,電話號%d:,卡號:%s,餘額:%.2f" %(user.name,user.identycard,user.phonenum,user.card.cardid,user.card.money )) def Savemoney(self): cardid=int(input("請輸入卡號:")) user=self.user_dict.get(cardid) if user==None: print("卡號不存在") return if user.card.islock==True: print('卡被鎖定,請先解鎖') return for i in range(3): passwd=input("請輸入密碼:") if user.card.passwd!=passwd: print("密碼錯誤") else: break else: print("機會已用完,卡被鎖了") user.card.islock = True return money=float(input("請輸入存款金額:")) user.card.money+=money print("存款成功,當前餘額為:",user.card.money) def Takemoney(self): cardid=int(input("請輸入卡號:")) user=self.user_dict.get(cardid) if user==None: print("卡號不存在") return if user.card.islock==True: print('卡被鎖定,請先解鎖') return for i in range(3): passwd=input("請輸入密碼:") if user.card.passwd!=passwd: print("密碼錯誤") else: break else: print("機會已用完,卡被鎖了") user.card.islock = True return money=float(input("請輸入取款金額:")) if money>user.card.money: print("輸入金額超出卡內餘額,請重新輸入:") return money user.card.money-=money print("取款成功,當前餘額為:",user.card.money) def Changemoney(self): cardid=int(input("請輸入卡號:")) user=self.user_dict.get(cardid) if user==None: print("卡號不存在") return if user.card.islock==True: print('卡被鎖定,請先解鎖') return for i in range(3): passwd=input("請輸入密碼:") if user.card.passwd!=passwd: print("密碼錯誤") else: break else: user.card.islock = True print("機會已用完,卡被鎖了") return othercard=int(input("請輸入對方卡號:")) otheruser = self.user_dict.get(othercard) if otheruser == None: print("卡號不存在") return money=float(input("請輸入轉賬金額:")) if money>user.card.money: print("輸入金額超出卡內餘額,請重新輸入:") return money user.card.money-=money print("轉賬成功,當前餘額為:",user.card.money) def Changepasswd(self): cardid = int(input("請輸入卡號:")) user = self.user_dict.get(cardid) if user == None: print("卡號不存在") return if user.card.islock == True: print('卡被鎖定,請先解鎖') return for i in range(3): passwd = input("請輸入密碼:") if user.card.passwd != passwd: print("密碼錯誤") else: break else: user.card.islock = True print("機會已用完,卡被鎖了") return if user == None: print("卡號不存在") return identycard=input("請輸入身份證號碼:") if user.identycard!=identycard: print("身份資訊驗證錯誤,請重新輸入") return passwd = input("請輸入新的密碼:") user.card.passwd=passwd print("密碼更改成功") print("使用者名稱:%s,身份證號%s:,電話號%d:,卡號:%s,餘額:%.2f" % (user.name, user.identycard, user.phonenum, user.card.cardid, user.card.money)) # 鎖卡 def Lockcard(self): cardid = int(input("請輸入卡號:")) user = self.user_dict.get(cardid) if user == None: print("卡號不存在") return if user.card.islock == True: print('卡已被鎖定') return print("是否確認鎖卡,如果鎖卡請輸入1,退出請輸入0:") num=int(input()) if num==1: for i in range(3): passwd = input("請輸入密碼:") if user.card.passwd != passwd: print("密碼錯誤") else: print("鎖卡成功") user.card.islock =True break else: print("鎖卡失敗") return else: exit() # 解鎖 def solveLock(self): cardid = int(input("請輸入卡號:")) user = self.user_dict.get(cardid) if user == None: print("卡號不存在") return if user.card.islock == False: print('卡未被鎖定') return print("是否確認解卡,如果解卡請輸入1,退出請輸入0:") num = int(input()) if num == 1: for i in range(3): identycard = input("請輸入身份證號:") if user.identycard != identycard: print("卡和姓名不匹配,解卡失敗") else: print("解卡成功") user.card.islock = False break else: print("解卡失敗") return else: exit() # 銷戶(刪除使用者) def Popuser(self): cardid = int(input("請輸入卡號:")) user = self.user_dict.get(cardid) if user == None: print("卡號不存在") return if user.card.islock == True: print('卡被鎖定,請先解鎖') return print("是否確認銷戶,如果銷戶請輸入1,退出請輸入0:") num = int(input()) if num == 1: for i in range(3): passwd = input("請輸入密碼:") if user.card.passwd != passwd: print("密碼錯誤,銷戶失敗") else: print("銷戶成功") self.user_dict.pop(cardid) break else: exit()
建立user.py
class User:
    def __init__(self,name,identycard,phonenum,card):
        self.name=name
        self.identycard=identycard
        self.phonenum=int(phonenum)
        self.card = card
card.py
class Card(object):
    def __init__(self,cardid,passwd,money):
        self.cardid=cardid
        self.passwd=passwd
        self.money=money
        self.islock=False
        #未鎖定
        #狀態值一般用布林表示
建立 Admin.py

class Admin:
def __init__(self,name,passwd):
self.name=name
self.passwd = passwd

def login(self,):
name=input("請輸入管理員賬戶:")
passwd=input("請輸入管理員密碼:")
if self.name==name and self.passwd==passwd:
print("登入成功")
return True
else:
print("登入失敗")
return False

建立 main.py
"""
銀行模擬

卡類:
    屬性:
        賬號,密碼,餘額
使用者類:
    屬性:
        姓名,身份證,手機號,銀行卡
ATM類:
    屬性:列表或者字典保村使用者資訊
    行為:列印介面,開戶,查詢,轉賬,取款,存款,鎖卡,解鎖,改密,銷戶,退出


管理員類:
    屬性:管理員的賬號和密碼
    行為:登入
"""
#先建立管理員類

from admin import Admin
from atm import Atm
def main ():
    ad=Admin("zhangsan","123")
    #呼叫管理員的登入
    for i in range(3):
        isok=ad.login()
        if isok==True:
            #建立Atm物件
            atm=Atm()

            while True:
                # 建立列印介面
                atm.print_ui()
                bh=int(input("請輸入功能編號:"))
                if bh==1:
                    atm.openAcount()
                if bh==2:
                   atm.Search()
                if bh==3:
                    atm.Takemoney()
                if bh==4:
                    atm.Savemoney()
                if bh==5:
                    atm.Changemoney()
                if bh==6:
                    atm.Changepasswd()
                if bh==7:
                    atm.Lockcard()
                if bh==8:
                   atm.solveLock()
                if bh==9:
                   atm.Popuser()
                if bh==10:
                    exit()

        break

if __name__=="__main__":

    main()

基本功能已經實現,需要的可以自己研究