Python implements the actual banking system

The examples in this article share the specific code of python to implement the bank actual combat system for your reference. The specific content is as follows

Attach the source code first:

│ admin.py administrator interface
│ alluser.txt save user information
│ How to operate each part of atm.py bank (deposit and withdraw money, etc.)
│ card.py defines the card class
│ main.py main program while True
│ user.py user class

Source code of main.py

"""
people
Class name: User
Attribute: Name ID card number phone number card
behavior:

card
Class name: Card
Property: Card number password balance
behavior:

ATM
Class name: ATM
Attribute: user dictionary
Behavior: Open an account, check withdrawals, deposit transfers, change passwords, lock, decrypt, reissue cards, cancel accounts, and exit

administrator
Class name: Admin
Attributes:
Behavior: Administrator interface administrator verification system function interface

"""
import os
import pickle

import admin
from atm import ATM

def main():
 # Administrator object
 admin1 = admin.Admin()

 # Administrator boot
 admin1.printAdminView()if admin1.adminOption():return-1

 # ATM object
 filepath = os.path.join(os.getcwd(),"alluser.txt")
 f =open(filepath,"rb")
 allUsers = pickle.load(f)
 f.close()
 atm =ATM(allUsers)while True:
 admin1.printFunctionView()
 # Waiting for user action
 option =input("Please enter your operation:")if option =="1" or option =="Open an account":
 atm.creatUser()
 elif option =="2" or option =="Inquire":
 atm.searchUserInfo()
 elif option =="3" or option =="Withdrawal":
 atm.withdrawals()
 elif option =="4" or option =="deposit":
 atm.saveMoney()
 elif option =="5" or option =="Transfer":
 atm.transferMoney()
 elif option =="6" or option =="Change":
 atm.changePasswd()
 elif option =="7" or option =="locking":
 atm.lockUser()
 elif option =="8" or option =="Unlock":
 atm.unlockUser()
 elif option =="9" or option =="Replace card":
 atm.newCard()
 elif option =="0" or option =="Account cancellation":
 atm.killUser()
 elif option =="t" or option =="drop out":if not admin1.adminOption():
 # Save the user information in the current system to a file
 f =open(filepath,"wb")
 pickle.dump(atm.allUsers, f,2)
 f.close()return-1else:print("The instruction is wrong, please re-enter! !")

 admin.timeFlush()if __name__ =='__main__':main()

Source code of admin.py

import time

def timeFlush():
 sum =2 #Set countdown time
 timeflush =0.25 #Set the screen refresh interval
 for i inrange(0,int(sum / timeflush)):
 list =["\", "|", "/", "—"]
 index = i %4print("\r operation is successful! Please wait{} ".format(list[index]), end="")
 time.sleep(timeflush)classAdmin(object):
 admin ="1"
 passwd ="1"

 def printAdminView(self):print("*****************************************************************")print("* *")print("* *")print("*Welcome to login csdn bank*")print("* *")print("* *")print("*****************************************************************")

 def printFunctionView(self):print("\r*****************************************************************")print("*Open an account(1)Inquire(2) *")print("*Withdrawal(3)deposit(4) *")print("*Transfer(5)Change(6) *")print("*locking(7)Unlock(8) *")print("*Replace card(9)Account cancellation(0) *")print("*drop out(t) *")print("*******************************************************************")

 def adminOption(self):
 inputAdmin =input("Please enter the administrator account:")if self.admin != inputAdmin:print("The account number is entered incorrectly! !")return-1
 inputPasswd =input("Please enter the administrator password:")if self.passwd != inputPasswd:print("The password is incorrect! !")return-1

 # It means that the account password is correct! !
 timeFlush()return0

source code of user.py

classUser(object):
 def __init__(self, name, idCard, phone, card):
 self.name = name
 self.idCard = idCard
 self.phone = phone
 self.card = card

The source code of card.py

classCard(object):
 def __init__(self, cardId, cardPasswd, cardMoney):
 self.cardId = cardId
 self.cardPasswd = cardPasswd
 self.cardMoney = cardMoney
 self.cardLock = False

Source code of atm.py

import random
from card import Card
from user import User
classATM(object):
def __init__(self, allUsers):
self.allUsers = allUsers
# Open an account
def creatUser(self):
# Add a key-value pair to the user dictionary(card number--user)
name =input("please enter your name:")
idCard =input("please enter your identity card number:")
phone =input("Please enter your phone number:")
prestoreMoney =int(input("Please enter the pre-stored amount:"))if prestoreMoney <0:print("The pre-stored amount is wrong! ! Account opening failed")return-1
onePasswd =input("Please set your password:")
# verify password
if not self.checkPasswd(onePasswd):print("Incorrect password! ! Account opening failed")return-1
# Generate a random card number
cardId = self.randomCardId()
# Generate card information
card =Card(cardId, onePasswd, prestoreMoney)
# Generate user information
user =User(name, idCard, phone, card)
# Save in dictionary
self.allUsers[cardId]= user
print("Your card number is%s,Please remember the card number!!"% cardId)
# Inquire
def searchUserInfo(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! Query failed")return-1if user.card.cardLock:print("The card has been locked! ! Please perform other operations after unlocking!")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! The card has been locked! ! Please perform other operations after unlocking!")
user.card.cardLock = True
return-1print("account number:%s balance:%d"%(user.card.cardId, user.card.cardMoney))
# Withdrawal
def withdrawals(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! Query failed")return-1if user.card.cardLock:print("The card has been locked! ! Please perform other operations after unlocking!")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! The card has been locked! ! Please perform other operations after unlocking!")
user.card.cardLock = True
return-1
# At this step, the card number information is correct, and the withdrawal operation
theMoney =int(input("Please enter the amount you need to withdraw:"))if theMoney   user.card.cardMoney:print("Insufficient balance! !")return-1
elif theMoney <0:print("The amount you entered is incorrect!!")else:
user.card.cardMoney -= theMoney
print("Successful withdrawal! ! The balance is:%d"% user.card.cardMoney)
# deposit
def saveMoney(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! Query failed")return-1if user.card.cardLock:print("The card has been locked! ! Please perform other operations after unlocking!")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! The card has been locked! ! Please perform other operations after unlocking!")
user.card.cardLock = True
return-1
# At this step, the card number information is correct, and the deposit operation
theMoney =int(input("Please enter the amount you need to deposit:"))if theMoney <0:print("The amount you entered is incorrect!!")else:
user.card.cardMoney += theMoney
print("Successful deposit! ! The balance is:%d"% user.card.cardMoney)
# Transfer
def transferMoney(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! Query failed")return-1if user.card.cardLock:print("The card has been locked! ! Please perform other operations after unlocking!")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! The card has been locked! ! Please perform other operations after unlocking!")
user.card.cardLock = True
return-1
# Go here to explain that the card number information is correct, and perform the transfer operation
theOtherCardId =input("Please enter the card number you need to transfer:")
# Verify that the card number exists
otheruser = self.allUsers.get(theOtherCardId)if not otheruser:print("The card number does not exist! ! Transfer failed")return-1if otheruser.card.cardLock:print("The card has been locked! !")return-1
theOtherCardName =input("Please enter the name of the person you need to transfer:")
# Verify that the transfer person’s name is correct
if otheruser.name != theOtherCardName:print("The name of the transferer is entered incorrectly")return-1print("Your account is%s Your balance is%d"%(user.card.cardId, user.card.cardMoney))
# Start transfer
theMoney =int(input("Please enter the amount you need to transfer:"))if theMoney <0:print("The amount you entered is incorrect!!")else:
user.card.cardMoney -= theMoney
otheruser.card.cardMoney += theMoney
print("The transfer was successful! ! Your balance is%d"% user.card.cardMoney)return-1
# Change
def changePasswd(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! Query failed")return-1if user.card.cardLock:print("The card has been locked! ! Please perform other operations after unlocking!")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! The card has been locked! ! Please perform other operations after unlocking!")
user.card.cardLock = True
return-1
# Let&#39;s change the password
newPasswd =input("Please enter a new password:")if newPasswd == user.card.cardPasswd:print("The old and new passwords cannot be the same! ! operation failed")return-1
# verify password
if not self.checkPasswd(newPasswd):print("Incorrect password! !")return-1
user.card.cardPasswd = newPasswd
print("Password reset complete! ! Please remember your password")
# locking
def lockUser(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! input error")return-1if user.card.cardLock:print("The card has been locked! ! Please unlock before using other functions")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! Lock failed")return-1
tempIdCard =input("please enter your identity card number")if tempIdCard != user.idCard:print("ID input error! ! Lock failed")return-1
# Proceeding to this step shows that the information input is successful and the lock starts
user.card.cardLock = True
print("Locked successfully")
# Unlock
def unlockUser(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! input error")return-1if not user.card.cardLock:print("The card is not locked! ! No need to unlock")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! Lock failed")return-1
tempIdCard =input("please enter your identity card number")if tempIdCard != user.idCard:print("ID input error! ! Lock failed")return-1
# Proceeding to this step indicates that the information has been entered successfully and unlocking begins
user.card.cardLock = False
print("Successfully unlocked")
# Replace card
def newCard(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! Query failed")return-1if user.card.cardLock:print("The card has been locked! ! Please perform other operations after unlocking!")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! The card has been locked! ! Please perform other operations after unlocking!")
user.card.cardLock = True
return-1
CardName =input("please enter your name:")
# Verify that the name is correct
if user.name != CardName:print("The name is entered incorrectly! !")return-1
useridCard =input("please enter your identity card number:")
# Verify that the ID is correct
if user.idCard != useridCard:print("The ID number is entered incorrectly! !")return-1
# Proceeding to this step shows that the information is correct, the following is the card replacement operation, only the card number is changed, other information is not changed
newIdCard= self.randomCardId()
self.allUsers[newIdCard]= self.allUsers.pop(user.card.cardId)
user.card.cardId = newIdCard
print("Your new card number is:%s Please remember! !"% user.card.cardId)
# Account cancellation
def killUser(self):
cardNum =input("Please enter your card number:")
# Verify that the card number exists
user = self.allUsers.get(cardNum)if not user:print("The card number does not exist! ! Query failed")return-1if user.card.cardLock:print("The card has been locked! ! Please perform other operations after unlocking!")return-1
# verify password
if not self.checkPasswd(user.card.cardPasswd):print("Incorrect password! ! The card has been locked! ! Please perform other operations after unlocking!")
user.card.cardLock = True
return-1
CardName =input("please enter your name:")
# Verify that the name is correct
if user.name != CardName:print("The name is entered incorrectly! !")return-1
useridCard =input("please enter your identity card number:")
# Verify that the ID is correct
if user.idCard != useridCard:print("The ID number is entered incorrectly! !")return-1
answer =input("Are you sure you want to cancel your account? determine(1)cancel(2)")if answer =="1" or answer =="determine":
del self.allUsers[cardNum]print("Account cancelled")return-1
elif answer =="2" or answer =="cancel":print("cancel成功!!")return-1else:print("input error! !")return-1
# verify password
def checkPasswd(self, realPasswd):for i inrange(3):
tempPasswd =input("Please enter the password again:")if tempPasswd == realPasswd:return True
return False
def randomCardId(self):while True:
str =""for i inrange(6):
# Randomly generate a number
ch =chr(random.randrange(ord("0"),ord("9")+1))
str += ch
# Determine whether to repeat
if not self.allUsers.get(str):return str

alluser.txt source code

�}q X 123456qcuser
User
q)�q}q(X nameqX 1qX idCardqhX phoneqhX cardq ccard
Card
q
) �q }q(X cardIdq
hX
cardPasswdqhX cardMoneyqKX cardLockq�ububs.

Because the pickle library is used to store user information (dictionaries) persistently, it will be garbled when open for reading and writing. 123456 in the first row is the card number, and all other information is 1. Don't try to modify this allUsers in pycharm. txt file, otherwise it will cause a bug that the program cannot be started. The author does not know how to improve the garbled phenomenon. I hope readers can optimize

If you encounter a situation where alluser.txt cannot run: please read down:

Because of the pickle library, we were going to read this file at the beginning. The program was read smoothly, but the program was not cool. Therefore, we must have the source code that can be recognized by the pickle library in alluser.txt. If you copy and paste in pycharm because of the alluser.txt code, pycharm will automatically convert to utf-8 or other

We need to delete alluser.txt first, so that the pickle library will not read it first, and create an empty dictionary. We will open an account first, and then exit the program. A new alluser.txt file will be created automatically, and the user information we just created All are saved in a txt file, so that we can restore again to achieve the purpose of persistent preservation

filepath = os.path.join(os.getcwd(),"alluser.txt")
# Comment out the previous ones and prevent the program from reading
# f =open(filepath,"rb")
# allUsers = pickle.load(f)
# f.close()
# Create a new empty dictionary
allUsers ={}
atm =ATM(allUsers)

Then we open an account, and finally exit, a brand new alluser.txt file will be created automatically

filepath = os.path.join(os.getcwd(),"alluser.txt")
f =open(filepath,"rb")
allUsers = pickle.load(f)
f.close()
# Then we restore it
# allUsers ={}
atm =ATM(allUsers)

ok, done

operation result:

For more learning materials, please pay attention to the topic "Management System Development".

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements the actual banking system
Python implements parking management system
Python implements car management system
Python implements the brick-and-mortar game
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements the steepest descent method
How Python implements the timer function
Python implements the aircraft war project
Python implements the sum of fractional sequences
Python basic actual combat-guess the age game
python implements the gradient method python the fastest descent method
python3 simply implements the combined design pattern
2.1 The Python Interpreter (python interpreter)
Python realizes the development of student management system
Python implements the shuffling of the cards in Doudizhu
Python implements the source code of the snake game
Python implements a simple business card management system
Python implements Super Mario
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements image stitching
Python implements scanning tools
Consolidate the Python foundation (2)
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Consolidate the foundation of Python(7)
Python implements udp chat window
python guess the word game
Consolidate the foundation of Python(6)
Python implements a guessing game
Ubuntu install the latest Python 3.
Python implements digital bomb game
Python implements TCP file transfer
Python realizes the guessing game
The difference between Python extensions
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements password strength verification
Python realizes face sign-in system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Consolidate the foundation of Python (3)
Python implements image stitching function