ATM系统的java实现

ATM机Java实现

ATM系统的主要功能:

  1. 注册:用户名,手机号,身份证号(18位),卡号(自动生成16位)密码(两次确认,长度6位)
  2. 查询:账号必须存在,密码(三次机会,不对就锁卡)
  3. 取款:账号必须存在,密码(三次机会,不对就锁卡),取款金额不能大于存款
  4. 存款:账号必须存在,存款金额不能低于0
  5. 转帐:你的账户,转款账户都必须存在,密码(三次机会,不对就锁卡),转账金额不能超过余额
  6. 锁卡:账号必须存在,可以使用密码冻结,还可以使用身份证号冻结
  7. 解卡:账号必须存在,只能使用身份证号进行解锁
  8. 补卡:使用身份证进行补卡,每个身份证只能有一张卡,之前的卡作废
  9. 改密:愿密码进行改密,也可以使用身份证进行改密
  10. 退出: 保存数据

程序入口:

public class ATM {
	static Person[] person;
	public static void main(String[] args) {
		Person[] person = new Person[100];
		Card card1 = new Card("6216633000001263541","123456",1000,false);
		person[0] = new Person("张三","123456789012345678","12345678900",card1);
		Card card2 = new Card("6216633000001263542","123456",2000,false);
		person[1] = new Person("李四","223456789012345678","12345678901",card2);
		ATM.person = person;		
		Views.welcome(ATM.person);

	}

}

银行卡对象:Card 存储银行卡信息
1.卡号. cardid
2.密码. password
3.余额. money
4. 是否锁卡.islock

public class Card {
	private String cardid;
	private String password;
	private double money;
	private boolean islock;
	
	
	
	//全参构造函数
	public Card(String cardid, String password, double money, boolean islock) {
		super();
		this.cardid = cardid;
		this.password = password;
		this.money = money;
		this.islock = islock;
	}

	//无参构造函数
		public Card() {
			
		}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Card other = (Card) obj;
		if (cardid == null) {
			if (other.cardid != null)
				return false;
		} else if (!cardid.equals(other.cardid))
			return false;
		if (islock != other.islock)
			return false;
		if (Double.doubleToLongBits(money) != Double.doubleToLongBits(other.money))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		return true;
	}
	
	public String getCardid() {
		return cardid;
	}
	
	public void setCardid(String cardid) {
		this.cardid = cardid;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public double getMoney() {
		return money;
	}
	
	public void setMoney(double money) {
		this.money = money;
	}
	
	public boolean isIslock() {
		return islock;
	}
	
	public void setIslock(boolean islock) {
		this.islock = islock;
	}
	

}

用户对象:Person 存储用户信息
1.用户名. name
2.身份证号.userid
3.手机号. phone
4.卡. card. 银行卡对象

public class Person {
	
	private String name;
	private String userid;
	private String phone;
	Card card;
	
	//全参构造函数
	public Person(String name, String userid, String phone, Card card) {
		super();
		this.name = name;
		this.userid = userid;
		this.phone = phone;
		this.card = card;
	}
	//无参构造函数
	public Person() {
		
	}
	
	
	public String getName() {
		return name;
	}
	
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getUserid() {
		return userid;
	}
	
	public void setUserid(String userid) {
		this.userid = userid;
	}
	
	public String getPhone() {
		return phone;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public Card getCard() {
		return card;
	}
	
	public void setCard(Card card) {
		this.card = card;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (card == null) {
			if (other.card != null)
				return false;
		} else if (!card.equals(other.card))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		if (userid == null) {
			if (other.userid != null)
				return false;
		} else if (!userid.equals(other.userid))
			return false;
		return true;
	}
	
	

}

视图对象: Views。
显示操作界面的
1.欢迎界面
2.主操作界面
3.各个选项的操作页面

import java.util.Scanner;

public class Views {
	
	static void welcome(Person[] person) {
		Scanner input = new Scanner(System.in);
		System.out.println("***********************************************");		 
		System.out.println("                 欢迎使用中国XX银行ATM机                                     ");		 
		System.out.println("***********************************************");		 
		System.out.println("请选择所需业务(输入序号):");		 
		System.out.println("1.注册");
		System.out.println("2.登录");	
		System.out.println("3.锁卡"); 
		System.out.println("4.解卡");		 
		System.out.println("5.补卡");		 
		System.out.println("6.改密");		
		
		
		int n = 0;
		try {
		  n = Integer.parseInt(input.nextLine());
		}catch(NumberFormatException e) {
			System.out.println("您的输入有误,请重新输入!");
			Views.welcome(person);
		}
		switch(n) {
		case 1:
			Controller.register(person);
			break;
		case 2:
			Controller.login(person);
			break;
		case 3:
			Controller.lock(person);
			break;
		case 4:
			Controller.unlock(person);
			break;
		case 5:
			Controller.newCard(person);
			break;
		case 6:
			Controller.changePwd(person);
			break;
		default:
			System.out.println("您的操作有误!");
			welcome(person);
		}
	}
	@SuppressWarnings("static-access")
	static void mainIndex(Person[] person) {
		Scanner input = new Scanner(System.in);
		System.out.println("请选择所需业务(输入序号):");		 
		System.out.println("1.查询");		 
		System.out.println("2.取款");		 
		System.out.println("3.存款");		 
		System.out.println("4.转账"); 
		System.out.println("5.退出");
		int n = 0;
		try {
			  n = Integer.parseInt(input.nextLine());
			}catch(NumberFormatException e) {
				System.out.println("您的输入有误,请重新输入!");
				Views.welcome(person);
			}
		switch(n) {
		case 1:
			Controller.query(person);
			break;
		case 2:
			Controller.getMoney(person);
			break;
		case 3:
			Controller.addMoney(person);
			break;
		case 4:
			Controller.saveMoney(person);
			break;
		case 5:
			Controller.save(person);
			break;
		default:
			System.out.println("您的操作有误!");
			mainIndex(person);
		}
	}

}

功能对象: Controller 具体的操作功能类
功能对象中主要就是去实现ATM这个系统中的操作
在上面定义的那十个方法

  1. 注册 :regiser
  2. 查询:query
  3. 取款:getMoney
  4. 存款:addMoney
  5. 转帐:saveMoney
  6. 锁卡:lock
  7. 解卡:unlock
  8. 补卡:newCard
  9. 改密:changePwd
  10. 退出: save
import java.util.Random;
import java.util.Scanner;

public class Controller {
	static int n = 0;//记录密码输入错误次数
	static int index = -1;//记录登录用户所在Person数组位置下标
	
 	
	//注册:用户名,手机号,身份证号(18位),卡号(自动生成16位)密码(两次确认,长度6位)
	static void register(Person[] person) {
		int indexx = -1;
		
		for(int i=0;i<person.length;i++) {
			if(person[i] == null) {
				indexx = i;
				break;
			}
		}
		Scanner input = new Scanner(System.in);
		System.out.println("请输入身份证号(18位):");
		String userid = input.nextLine();
		boolean flag = true;
		for(int i=0;i<person.length;i++) {
			if(person[i] != null && userid.equals(person[i].getUserid())) {
				flag = false;
				System.out.println("该用户已注册,可直接登录!");
				Views.welcome(person);
			}
		}
		if(flag) {
			Person p = new Person();
			Card c = new Card();
			p.setUserid(userid);
			System.out.println("请输入用户名:");
			p.setName(input.nextLine());
			System.out.println("请输入手机号:");
			p.setPhone(input.nextLine());
			String password;
			String password1;
			do {
				System.out.println("请输入密码(6位数字):");
				password = input.nextLine();
				System.out.println("请再次确认密码:");
				password1 = input.nextLine();
				if(!password.equals(password1)) {
					System.out.println("两次输入的密码不一致,请重新输入!");
				}
			}while(!password.equals(password1));
			if(password.equals(password1)){
				System.out.println("注册成功!");
				String id = randomID();
				for(int i=0;i<person.length;i++) {
			        while(person[i] != null && person[i].card.getCardid().equals(id)) {
			        	id = randomID();
			        }
				}
				System.out.println("卡号为:"+id);
				c.setCardid(id);
				c.setPassword(password);
				c.setIslock(false);
				c.setMoney(0);
				p.setCard(c);
				person[indexx] = p;
				Views.welcome(person);	
			}
		}
	}
	
	//自动生成卡号
	static String randomID() {
		Random random = new Random();
		String id = "";
		for(int i=0;i<16;i++) {
			int n = random.nextInt(10);
			String s = ""+n;
			id = id+s;
		}
		return id;
	}
	
	//登录:用卡号和密码登录
	static void login(Person[] person) {
		
		Scanner input = new Scanner(System.in);
		System.out.println("请输入卡号:");
		String id = input.nextLine();
		boolean flag = false;
		int i;
		for(i=0;i<person.length;i++) {
			if(person[i] != null && person[i].card.getCardid().equals(id)) {
				flag = true;
				index = i;
				break;
			}
		}
		if(!person[i].card.isIslock()) {
			if(flag) {
				System.out.println("请输入密码:");
				String p = input.nextLine();
				if(person[i].card.getPassword().equals(p)) {
					System.out.println("登录成功!");
					Views.mainIndex(person);
				}else {
					n++;
					if(n<3) {
						int n1 = 3 - n;
						System.out.println("密码错误,您还有"+n1+"次机会");
					}
					if(n==3) {
						System.out.println("密码错误三次,卡已锁定");
						n = 0;
						person[index].card.setIslock(true);
						Views.welcome(person);	
					}
					System.out.println("密码错误,请重新登录!");
					Views.welcome(person);	
					
				}
			}else {
				System.out.println("卡号不存在,请重新输入!");
				Views.welcome(person);	
			}
		}else {
			System.out.println("此卡已被锁,请先解锁,再进行登录!");
			index = -1;
			Views.welcome(person);	
		}
		
	}
	
	//查询:账号必须存在,密码(三次机会,不对就锁卡)
	static void query(Person[] person) {
		System.out.println("您的余额为:"+person[index].card.getMoney());
		Views.mainIndex(person);
	}
	
	//取款:账号必须存在,密码(三次机会,不对就锁卡),取款金额不能大于存款
	static void getMoney(Person[] person) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入取款金额:");
		int money = Integer.parseInt(input.nextLine());
		if(money<=person[index].card.getMoney()) {
			person[index].card.setMoney(person[index].card.getMoney()-money);
			System.out.println("取款成功!您的余额为:"+person[index].card.getMoney());
			Views.mainIndex(person);
	    }else {
	    	System.out.println("余额不足,请重新输入!");
	    	Views.mainIndex(person);
	    }
	}
	
	//存款:账号必须存在,存款金额不能低于0
	static void addMoney(Person[] person) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入存款金额:");
		int money = Integer.parseInt(input.nextLine());
		if(money>0) {
			person[index].card.setMoney(person[index].card.getMoney()+money);
			System.out.println("存款成功!您的余额为:"+person[index].card.getMoney());
			Views.mainIndex(person);
		}else {
			System.out.println("您的输入有误,请重新输入!");
			Views.mainIndex(person);
		}
	}
	
	//转帐:你的账户,转款账户都必须存在,密码(三次机会,不对就锁卡),转账金额不能超过余额
	static void saveMoney(Person[] person) {
		int indexx = -1;
		boolean flag = false;
		Scanner input = new Scanner(System.in);
		System.out.println("请输入转账卡号:");
		String cardid = input.nextLine();
		for(int i=0;i<person.length;i++) {
			if(person[i] != null && person[i].card.getCardid().equals(cardid)) {
				indexx = i;
				flag = true;
				break;
			}
		}
		if(flag) {
			System.out.println("请输入转账金额:");
			int money = Integer.parseInt(input.nextLine());
			if(money<=person[index].card.getMoney()) {
				person[index].card.setMoney(person[index].card.getMoney()- money);
				person[indexx].card.setMoney(person[indexx].card.getMoney()+ money);
				System.out.println("转账成功!您的余额为:"+person[index].card.getMoney());
				Views.mainIndex(person);
			}else {
				System.out.println("您的余额不足,请重新输入!");
				Views.mainIndex(person);
			}
		}else {
			System.out.println("您输入的账号不存在,请重新输入!");
			Views.mainIndex(person);
		}
	}
	
	// 锁卡:账号必须存在,可以使用密码冻结,还可以使用身份证号冻结
	static void lock(Person[] person) {
		int indexx = -1;
		boolean flag = false;
		Scanner input = new Scanner(System.in);
		System.out.println("请输入卡号:");
		String cardid = input.nextLine();
		for(int i=0;i<person.length;i++) {
			if(person[i] != null && person[i].card.getCardid().equals(cardid)) {
				indexx = i;
				flag = true;
				break;
			}
		}
		if(flag) {
			System.out.println("请输入密码或身份证号:");
			String s = input.nextLine();
			if(person[indexx].card.getPassword().equals(s) || person[indexx].getUserid().equals(s) ) {
				person[indexx].card.setIslock(true);
				System.out.println("锁卡成功!");
				Views.welcome(person);
			}else {
				System.out.println("您的密码或身份证号不正确,请重新输入!");
				Views.welcome(person);
			}
		}else {
			System.out.println("您输入的账号不存在,请重新输入!");
			Views.welcome(person);
		}
	}
	
	// 解卡:账号必须存在,只能使用身份证号进行解锁
	static void unlock(Person[] person) {
		int indexx = -1;
		boolean flag = false;
		Scanner input = new Scanner(System.in);
		System.out.println("请输入卡号:");
		String cardid = input.nextLine();
		for(int i=0;i<person.length;i++) {
			if(person[i] != null && person[i].card.getCardid().equals(cardid)) {
				indexx = i;
				flag = true;
				break;
			}
		}
		if(flag) {
			System.out.println("请输入身份证号:");
			String s = input.nextLine();
			if(person[indexx].getUserid().equals(s) ) {
				person[indexx].card.setIslock(false);
				System.out.println("解卡成功!");
				Views.welcome(person);
			}else {
				System.out.println("您的身份证号不正确,请重新输入!");
				Views.welcome(person);
			}
		}else {
			System.out.println("您输入的账号不存在,请重新输入!");
			Views.welcome(person);
		}
	}
	
	//补卡:使用身份证进行补卡,每个身份证只能有一张卡,之前的卡作废
	static void newCard(Person[] person) {
		int indexx = -1;
		
		Scanner input = new Scanner(System.in);
		System.out.println("请输入身份证号(18位):");
		String userid = input.nextLine();
		boolean flag = false;
		for(int i=0;i<person.length;i++) {
			if(person[i] != null && person[i].getUserid().equals(userid)) {
				indexx = i;
				flag = true;
				break;
			}
		}
		if(flag) {
			Random random = new Random();
			String id = "";
			for(int i=0;i<16;i++) {
				int n = random.nextInt(10);
				String s = ""+n;
				id = id+s;
			}
			System.out.println("补卡成功!新卡号为:"+id);
			person[indexx].card.setCardid(id);
			Views.welcome(person);
		}else {
			System.out.println("身份证号错误,请重新输入!");
			Views.welcome(person);
		}
		
	}
	
	// 改密:愿密码进行改密,也可以使用身份证进行改密
	static void changePwd(Person[] person) {
		int indexx = -1;
		String p1;
		String p2;
		boolean flag = false;
		Scanner input = new Scanner(System.in);
		System.out.println("请输入卡号:");
		String cardid = input.nextLine();
		for(int i=0;i<person.length;i++) {
			if(person[i] != null && person[i].card.getCardid().equals(cardid)) {
				indexx = i;
				flag = true;
				break;
			}
		}
		if(flag) {
			System.out.println("请输入密码或身份证号:");
			String s = input.nextLine();
			if(person[indexx].card.getPassword().equals(s) || person[indexx].getUserid().equals(s) ) {
				do {
					System.out.println("请输入新的密码(6位):");
					p1 = input.nextLine();
					System.out.println("请再次确认密码:");
					p2 = input.nextLine();
					if(!p1.equals(p2)) {
						System.out.println("两次输入的密码不一致,请重新输入!");
					}
				}while(!p1.equals(p2));
				if(p1.equals(p2)) {
					System.out.println("密码更改成功!");
					person[indexx].card.setPassword(p1);
					Views.welcome(person);
				}else {
					
				}
			}else {
				System.out.println("您的密码或身份证号不正确,请重新输入!");
				Views.welcome(person);
				
			}
		}else {
			System.out.println("您输入的账号不存在,请重新输入!");
			Views.welcome(person);
		}

    }
	
	//退出: 保存数据
	static void save(Person[] person) {
		System.out.println("您已退出,请拿好您的银行卡!");
		index = -1;
		Views.welcome(person);
	}
}

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值