Python小程序之-ATM

需求文档
# card类 存储卡
卡号   密码     余额    锁定状态
cardid password  money   islock

# person类  用户类
用户名 身份证号 电话  卡
name    userid  phone card

# view类 视图类
登录  打印欢迎的界面 打印功能操作界面

# operation类 操作类
具体完成10个功能

# main 类,来调用相应的功能,进行统一调用
开户(1) : register
查询(2) : query         
存钱(3) : save_money    
取钱(4) : get_money             
转账(5) : trans_money  
改密(6) : change_pwd         
锁卡(7) : lock  
解卡(8) : unlock         
补卡(9) : new_card  
退出(0) : save


user.txt    卡号:用户对象
userid.txt  身份证:卡号

在这里插入图片描述

main.py

from package.view import View
from package.operation import Operation


class Main():
	@staticmethod
	def run():
		if View.login():
			obj = Operation()
			while True:
				choice = input("请选择办理业务:")
				if choice == "1":
					obj.register()
				if choice == "2":
					obj.query()
				if choice == "3":
					obj.save_money()
				if choice == "4":
					obj.get_money()
				if choice == "5":
					obj.trans_money()
				if choice == "6":
					obj.change_pwd()
				if choice == "7":
					obj.lock()
				if choice == "8":
					obj.unlock()
				if choice == "9":
					obj.new_card()
				elif choice == "0":
					obj.save()
					break

if __name__ == "__main__":
	Main.run()

card.py

# card类  存储卡
class Card():
	def __init__(self, cardid, password, money):
		self.cardid = cardid
		self.password = password
		self.money = money
		self.islock = False

person.py

# person类 用户类
class Person():
	def __init__(self, name, userid, phone, card):
		self.name = name
		self.userid = userid
		self.phone = phone
		self.card = card

view.py

# view类 视图类
import time
class View():
	def login():
		name = input("请输入管理员账户:")
		pwd = input("请输入管理员密码:")
		if name == "admin" and pwd == "111":
			View.welcome_view()
			time.sleep(1)
			View.operation_view()
			return True
		else:
			print("管理员账号或密码不正确")



	def welcome_view():
		print("""\
****************************************
*                                      *
*              welcome                 *
*                                      *
****************************************""")
	def operation_view():
		print("""\
****************************************
*		   开户(1)  查询(2)             *
*		   存钱(3)  取钱(4)             *
*		   转账(5)  改密(6)             *
*		   锁卡(7)  解卡(8)             *
*		   补卡(9)  退出(0)             *	
****************************************""")

if __name__ == "__main__":
	View.login()

operation.py

import os
import pickle
import random
from .card import Card
from .person import Person
import re

class Operation():
	def __init__(self):
		self.load_user()
		self.load_userid()

	def load_user(self):
		if os.path.exists("user.txt"):
			with open("user.txt", mode="rb") as fp:
				self.user_dict = pickle.load(fp)
		else:
			self.user_dict = {
    
    }
		print(self.user_dict)

	def load_userid(self):
		if os.path.exists("userid.txt"):
			with open("userid.txt", mode="rb") as fp:
				self.user_id_dict = pickle.load(fp)
		else:
			self.user_id_dict = {
    
    }
		print(self.user_id_dict)

	def save(self):
		with open("user.txt", mode="wb") as fp:
			pickle.dump(self.user_dict, fp)

		with open("userid.txt", mode="wb") as fp:
			pickle.dump(self.user_id_dict, fp)

	def register(self):
		name = input("请输入姓名:")
		if name == "":
			print("姓名不能为空")
		else:
			userid = input("请输入身份证号:")
			if userid not in self.user_id_dict:
				res = re.search(
					r"^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$",
					userid)
				if not res:
					print("身份证不正确")
				else:
					phone = input("请输入手机号:")
					res = re.search(r"^1[3-9]\d{9}$", phone)
					if not res:
						print("手机号不正确")
					else:
						password = self.get_pwd("请输入您的密码(6位)", "请确认您的密码(6位)")
						cardid = self.get_cardid()
						money = 10
						card = Card(cardid, password, money)
						user = Person(name, userid, phone, card)
						self.user_dict[cardid] = user
						self.user_id_dict[userid] = cardid
						print("恭喜{}开卡成功,您的卡号为:{},卡内余额为{}元".format(name, cardid, money))
						print(self.user_dict)
						print(self.user_id_dict)
			else:
				print("身份证号不能重复")



	def get_pwd(self, name1, name2):

		while True:
			pwd1 = input(name1)
			res = re.search(r"^\w{6}$", pwd1)
			if not res:
				print("密码不正确")
			else:
				pwd2 = input(name2)
				res = re.search(r"^\w{6}$", pwd2)
				if not res:
					print("密码不正确")
				else:
					if pwd1 == pwd2:
						return pwd1
					else:
						print("两次密码不一致,请重新输入")

	def get_cardid(self):
		while True:
			cardid = str(random.randrange(100000, 1000000))
			if cardid not in self.user_dict:
				return cardid

	def query(self):
		card = self.get_card_info()
		if not card:
			print("抱歉,这张卡不存在")
		else:
			if card.islock:
				print("抱歉,您的卡被锁了")
			else:
				if self.check_pwd(card):
					print("您的卡内余额为{}元".format(card.money))

	def get_card_info(self):
		cardid = input("请输入您的卡号:")
		if cardid not in self.user_dict:
			return False
		else:
			user = self.user_dict[cardid]
			return user.card

	def check_pwd(self, card):
		times = 1
		while times < 4:
			pwd = input("请输入您的密码:")
			if pwd == card.password:
				return True
			else:
				print("密码错误,你还剩{}次机会".format(3-times))
				if times == 3:
					card.islock = True
					print("抱歉,此卡已锁")
			times += 1

	def save_money(self):
		card = self.get_card_info()
		if not card:
			print("抱歉,这个卡不存在")
		else:
			user = self.user_dict[card.cardid]
			print("你这张卡用户名为:{}".format(user.name))
			key_sure = input("确认存款按1,按任意键返回上一层!")
			if key_sure == "1":
				str_money = input("请输入您的存款金额:")
				if str_money.isdecimal():
					money = int(str_money)
					card.money += money
					print("成功存入{}元人民币".format(money))
				else:
					print("您输入的金额有误")

	def unlock(self):
		card = self.get_card_info()
		if not card:
			print("抱歉,这个卡不存在")
		else:
			userid = input("请输入身份证号码:")
			user = self.user_dict[card.cardid]
			if user.userid == userid:
				card.islock = False
				print("***解卡成功***")
			else:
				print("***解卡失败***")

	def lock(self):
		card = self.get_card_info()
		if not card:
			print("抱歉,这个卡不存在")
		else:
			num = input("1:使用密码冻结  2:使用身份证号冻结")
			if num == "1":
				pwd = input("请输入您的密码:")
				if pwd == card.password:
					card.islock = True
					print("***锁卡成功***")
				else:
					print("***锁卡失败***")
			elif num == "2":
				userid = input("请输入身份证号码:")
				user = self.user_dict[card.cardid]
				if user.userid == userid:
					card.islock = True
					print("***锁卡成功***")
				else:
					print("***锁卡失败***")

	def get_money(self):
		card = self.get_card_info()
		if not card:
			print("抱歉,这个卡不存在")
		elif card.islock:
			print("抱歉,您的卡被锁了")
		else:
			user = self.user_dict[card.cardid]
			print("你这张卡用户名为:{}".format(user.name))
			key_sure = input("确认取款按1,按任意键返回上一层!")
			if key_sure == "1":
				str_money = input("请输入您的取款金额:")
				if str_money.isdecimal():
					money = int(str_money)
					if money <= card.money:
						card.money -= money
						print("成功取出{}元人民币".format(money))
					else:
						print("取款金额超限")
				else:
					print("您输入的金额有误")

	def change_pwd(self):
		card = self.get_card_info()
		if not card:
			print("抱歉,这个卡不存在")
		else:
			num = input("1.原密码改密  2.身份证改密")
			if num == "1":
				pwd = input("请输入您的旧密码:")
				if pwd == card.password:
					newpwd = input("请输入您的新密码")
					if newpwd == pwd:
						print("密码不能与原密码一样")
					else:
						res = re.search(r"^\w{6}$", newpwd)
						if not res:
							print("密码不正确")
						else:
							newpwd2 = input("请确认您的新密码")
							res = re.search(r"^\w{6}$", newpwd)
							if not res:
								print("密码不正确")
							else:
								if newpwd == newpwd2:
									card.password = newpwd
									print("改密成功")
								else:
									print("密码不一致,修改失败")
				else:
					print("密码有误")
			elif num == "2":
				userid = input("请输入身份证号码:")
				user = self.user_dict[card.cardid]
				if user.userid == userid:
					newpwd = input("请输入您的新密码")
					res = re.search(r"^\w{6}$", newpwd)
					if not res:
						print("密码不正确")
					else:
						newpwd2 = input("请确认您的新密码")
						res = re.search(r"^\w{6}$", newpwd2)
						if not res:
							print("密码不正确")
						else:
							if newpwd == newpwd2:
								card.password = newpwd
								print("改密成功")
							else:
								print("密码不一致,修改失败")
				else:
					print("密码有误")

	def trans_money(self):
		card1 = self.get_card_info()
		if not card1:
			print("抱歉,这个卡不存在")
		elif card1.islock:
			print("抱歉,您的卡被锁了")
		else:
			card2 = self.get_card_info()
			if not card2:
				print("抱歉,这个卡不存在")
			else:
				if card1 == card2:
					print("抱歉,不能给自己转账")
				else:
					str_money = input("请输入您的转账金额:")
					if str_money.isdecimal():
						money = int(str_money)
						if money <= card1.money:
							card1.money -= money
							card2.money += money
							print("成功转账{}元人民币".format(money))
					else:
						print("您输入的金额有误")

	def new_card(self):
		userid = input("请输入身份证号码:")
		id1 = self.user_id_dict.get(userid)
		if not id1:
			print("此身份证号不存在")
		else:
			card_old = self.user_id_dict[userid]
			user = self.user_dict[card_old]
			newcard = self.get_cardid()
			if newcard not in self.user_dict:
				user.card.cardid = newcard
				self.user_dict[newcard] = user
				self.user_dict.pop(card_old)
				self.user_id_dict[userid] = newcard
			print("补卡办理成功,您的新卡号为{}".format(newcard))

猜你喜欢

转载自blog.csdn.net/qq_45957580/article/details/108184819