Fork me on GitHub

银行 ATM 系统项目

取钱:卡是否存在,是否冻结,取钱金额是否正确
转账:把一个卡里的钱转到其他卡内 (卡是否存在,是否冻结,对方账户是否存在,转账的金额是否正确)
改密:(1)原密码改密  (2)身份证改密
解卡:判断卡号和身份证是否正确在选择解卡
补卡:将旧用户的所有信息和新卡绑定(包括名字,余额等所有卡信息和用户信息,数据重新绑定)

# 类型要判断
	姓名不能为空,
	密码6位包含字母数字大小写,
	身份证号用正则匹配且不能为空,一个身份证号只能注册一次不能重复.
	不能给自己转账

  

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

class Main():
	@staticmethod
	def run():
		if View.login():
			obj= Operation()
			while True:
				name=input("请输入您要办理的序号")
				if name=="1":
					obj.register()
				elif name=="2":
					obj.query()
				elif name=="3":
					obj.save_money()
				elif name=="4":
					obj.Withd_money()
				elif name=="5":
					obj.Transfer_counts()
				elif name=="6":
					obj.Modify_density()
				elif name=="7":
					obj.lock()
				elif name=="8":
					obj.unlock()
				elif name=="9":
					obj.new_card()
				elif name=="0":
					obj.save()
					break
if __name__ == '__main__':
	Main.run()

  

# ###建立卡类
# card卡类:
# 卡号,密码,余额,锁定状态
# cardid , password , money , islock

class Card():
	def __init__(self,cardid,password,money):
		self.cardid=cardid
		self.password =password
		self.money=money
		self.islock=False

  

###建立用户类person
# person 用户类:
# 用户名 身份证号 手机号 卡
# name userid phone card

class Person():
	def __init__(self,name,userid,phone,card):
		self.name=name
		self.userid=userid
		self.phone=phone
		self.card=card

  

### view 视图类
# 登录,  打印欢迎界面 , 打印操作界面

import time

class View():
	def login():
		user=input('请输入登录账号:')
		pwd=input("请输入用名密码")
		if user=="admin" and pwd =="111":
			# 打印欢迎页面
			View.longin_view()
			#设置延迟一秒
			time.sleep(1)
			# 打印操作页面
			View.operation_view()
			return True
		else:
			print("抱歉,你的账户或密码不正确")

	def longin_view():
		print("%s" % ('=' * 50))
		print()
		print('%25s' % ('欢迎来到界面'))
		print()
		print("%s" % ("=" * 50))

	def operation_view():
		print("%s" % ('=' * 50))
		print("%20s		%-40s" % ('开户:(1)','查询:(2)'))
		print("%20s		%-40s" % ( '存钱:(3)','取钱:(4)'))
		print("%20s		%-40s" % ( '转账:(5)','改密:(6)'))
		print("%20s		%-40s" % ( '锁卡:(7)','解卡:(8)'))
		print("%20s		%-40s" % ('补卡:(9)','退出:(0)'))
		print("%s" % ('=' * 50))

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

  

### operation 操作类
# 把十个功能都在operation里面完成实现
import pickle
import os
import random
from .person import Person
from.card import Card
import re

class Operation():
	# 当进入到系统的时,优先把数据库中的数据拿出来
	def __init__(self):
		self.load_user()
		self.load_userid()
		# {卡号:用户}
		print(self.user_dict)
		# (身份证号:卡号)
		print(self.user_id_dict)

	# 加载{卡号:用户} 字典数据
	def load_user(self):
		if os.path.exists("user.txt"):
			with open("user.txt","rb") as fp:
				self.user_dict = pickle.load(fp)
		else:
			self.user_dict = {}

	# 加载{身份证号:卡号} 字典数据
	def load_userid(self):
		if os.path.exists("userid.txt"):
			with open("userid.txt","rb") as fp:
				self.user_id_dict = pickle.load(fp)
		else:
			self.user_id_dict = {}

	# 当点击退出时,把数据重新更新到数据库
	def save(self):
		with open("user.txt", "wb") as fp:
			pickle.dump(self.user_dict,fp)

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

	# 开户注册
	def register(self):
		while True:
			name = input("请输入您的姓名:")
			if name=='':
				print("账号不能为空请从新输入")
			else:
				break
		while True:
			phone = input("请输入您的身份证号码(18位):")
			obj = re.search("[1-9][\d+]{16}.", phone)
			if obj == None:
				print("输入身份证号码有误():")
			else:
				userid = obj.group()
				break

		while True:
			phone = input("请输入您的手机号(11位):")
			obj = re.search("^1[\d+]{10}", phone)
			if obj == None:
				print("请输入以1开头11位手机号")
			else:
				phone=obj.group()
				break

		# userid = input("请输入您的身份证号:")
		# phone = input("请输入您的手机号码:")
		if name == userid == phone == "":
			print('您输入的信息有误请从新输入')
		else:
			# 获取密码
			password =self.get_pwd()
			# 获取卡号
			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("恭喜%s开卡成功,您的卡号为:%s,卡内余额%s元"%(name,cardid,money))
	# 获取密码
	def get_pwd(self):
		while True:
			pwd1 = input("请输入密码:").strip()
			pwd2 = input("请再次输入密码:").strip()
			if pwd1 ==pwd2 and pwd1!="":
				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('您卡内余额是%s元'%(card.money))

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

			return card
	#检测密码(输入三次后锁卡)
	def check_pwd(self,card):
		count =1
		while count<4:
			pwd = input("请输入您的密码:").strip()
			if pwd == card.password:
				return True
			else:
				# 剩余次数 =总次数 - 已经有的次数
				print('密码错误,还剩余%s次机会'%(3-count))
				if count==3:
					card.islock=True
					print('抱歉,您输入密码次数已超过三次,卡已锁住')
			count+=1

	# 存钱业务
	def save_money(self):
		card=self.get_card_info()
		if not card:
			print('抱歉,您的卡不存在')
		else:
			if card.islock:
				print('抱歉,卡已锁住,请先解锁')
			else:
				user = self.user_dict[card.cardid]
				print("您这张卡的用名名是:%s"%(user.name))
				key_sure=input("确认存款请按 1 ,任意键返回上一层")
				if key_sure =="1":
					str_money=input("请输入您要存款金额").strip()
					if str_money.isdecimal():
						money=int(str_money)
						card.money+=money
						print("成功存入金额%s"%(money))
					else:
						print('您输入的金额有误')
	# 取款业务
	def Withd_money(self):
		card=self.get_card_info()
		if not card:
			print('抱歉,您的卡不存在')
		else:
			if card.islock:
				print("抱歉,卡已锁住,请先解锁")
			else:
				pwd=self.check_pwd(card)
				wd_money=input("请输入取款金额:")
				if wd_money.isdecimal():
					money=int(wd_money)
					if  money>card.money:
						print('您的余额不足,请从新输入:')
					else:
						card.money-=money
						print("您已成功取款%s元,卡内余额%s元"%(money,card.money))

	# 转账业务
	def Transfer_counts(self):
		card=self.get_card_info()
		if not card:
			print('抱歉,您的卡不存在')
		else:
			if card.islock:
				print('抱歉,卡已锁住,请先解锁')
			else:
				self.check_pwd(card)
				Other_party=input('请输入对方卡号:')
				if Other_party == card.cardid:
					print("抱歉,账号不能为空或给自己转账")
					return False
				if Other_party not in self.user_dict:
					print("抱歉,账号不能为空或给自己转账")
					return False
				else:
					user = self.user_dict[Other_party]
					Other_party= user.card

					count=input("您将给%s进行转账,确认请按 1 返回按任意键"%(user.name))
					if count=="1":
						Other_count = input("请输入转账金额:")
						Other_count=int(Other_count)
						if card.money<Other_count:
							print("抱歉,您的余额不足")
						else:
						# 原账号减掉的剩余的钱
							card.money-=Other_count
							# 对方账号
							Other_party.money+=Other_count
							print("您已经成功向%s转账%s元"%(user.name,Other_count))
					else:
						return False
	# 改密
	def Modify_density(self):
		card=self.get_card_info()
		if not card:
			print('抱歉,你的卡不存在')
		else:
			if card.islock:
				print('抱歉,卡已锁住,请先解锁后才能更改密码')
			else:
				Modify_count=input("请选择改密方式: (1) 原密修改 (2) 身份证信息验证")
				if Modify_count=="1":
					self.check_pwd(card)
					card.password=self.get_pwd2()
					print("=======改密成功=======")
				elif Modify_count =="2":
					while True:
						Modify_userid=input("请输入您的身份证号码:")
						# user=self.userid_dict[card.cardid]
						user=self.user_dict[card.cardid]
						if Modify_userid ==user.userid:
							self.check_pwd(card)
							card.password = self.get_pwd2()
							print("=======改密成功=======")
							break
						else:
							print("您输入身份证号码有误请从新输入")

	# 设置更改新密码 self.userid_dict[userid]
	def get_pwd2(self):
		while True:
			pwd1 = input("请输入新密码:").strip()
			pwd2 = input("请再次输新密码:").strip()
			if pwd1 ==pwd2 and pwd1!='':
				return pwd1
			else:
				print('您输入两次密码不一致,请从新输入:')

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

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

	# 补卡
	def new_card(self):
		card =self.get_card_info()
		if not card:
			print('抱歉,卡不存在')
		else:
			if card.islock:
				print('抱歉,卡已锁住,请先解锁后才能更补卡')
			else:
				userid=input("请输入身份证号:")
				user=self.user_dict[card.cardid]
				if userid==user.userid and userid!="":
					# print(card.cardid)
					del self.user_dict[card.cardid]
					card.cardid=self.get_cardid()
					self.user_dict[card.cardid] = user
					self.user_id_dict[userid] = card.cardid
					print('补卡成功,补卡号为:%s,当前余额为%s元'%(card.cardid,card.money))

  

posted @ 2019-05-29 19:25  MR_黄Python之路  阅读(559)  评论(0编辑  收藏  举报