JavaSE模拟ATM机源代码

这一篇只有源代码,共三个类,废话不多说,上代码!想看设计思想以及具体步骤的→传送门

用户类(User实体类)

/**
 * 	模拟用户(用户的属性)
 * 	1.账号
 * 	2.密码
 * 	3.余额
 * 	4.登录失败的次数
 */

package com.mhq.homework;

public class User {
	public String username;
	public String password;
	public double money;
	public int loginNum = 0;

}

ATM机类(主要是各种功能的实现)

package com.mhq.homework;

public class ATM {
	//存储用户注册的信息
	static User[]users =new User[2];
	//用户注册的数量
	static int number = 0;
	
	//修改密码,只有登录后才可以调用,除了需要传密码参数,还要传需要修改密码的用户user
	public boolean updatePassword(String newPassword,String confirmPassword,User user) {
		/*	调用lengthUser方法,判断用户输入的新密码是否满足长度限制
		 * 	里面的参数,user.username就是修改密码里面传过来的需要修改的用户信息user的账号
		 * 	newPassword即为参数里的newPassword(用户的新密码)
		 */
		User newUser = lengthUser(user.username, newPassword);
		if(newUser == null) {
			System.out.println("修改失败");
			System.out.println("密码的长度在范围之外");
			return false;
		}
		//判断两次密码是否一致
		if(newPassword.equals(confirmPassword)) {
			user.password = newPassword;
			System.out.println("修改成功");
			return true;
		}else {
			System.out.println("修改失败");
			System.out.println("两次密码不一致");
			return false;
		}
	}
	
	//判断余额是否充足
	public static boolean compareMoney(User user,double money) {
		//如果余额不足,返回false
		if(money>user.money) {
			System.out.println("你的余额已不足,请充值");
			return false;
		}else {
			return true;
		}
	}
	
	//存钱
	public void saveMoney(User user,double money) {
		user.money+=money;
		System.out.println("你存了"+money+"元,你的余额为:"+user.money+"元");
	}
	
	//取钱
	public void getMoney(User user,double money) {
		//调用判断余额是否充足的方法compareMoney
		boolean tag = compareMoney(user, money);
		if(tag == true) {
			user.money-=money;
			System.out.println("你取了"+money+"元,你的余额为:"+user.money+"元");
		}
		
	}
	
	//查看余额
	public void findMoney(User user) {
		System.out.println("你的余额为:"+user.money+"元");
	}
	
	//转账,调用的参数,转账人transferUser,到账人getMoneyUser,钱数money
	public void transferMoney(User transferUser,User getMoneyUser,double money) {
		//调用判断余额是否充足的方法compareMoney
		boolean tag = compareMoney(transferUser, money);
		if(tag == true) {
			transferUser.money-=money;
			getMoneyUser.money+=money;
			System.out.println("你向用户"+getMoneyUser.username+"转了"+money+"元");
			System.out.println("你的余额为:"+transferUser.money+"元");
		}
	}
	
	//判断用户是否存在  返回值为user类型
	public User getUser(String username) {
		//遍历存储用户信息的数组,和用户输入的账号比较
		for(int i=0;i<number;i++) {
			if(username.equals(users[i].username)&&users[i]!=null) {
				return users[i];
			}
		}
		return null;
	}
	
	//储存扩容,若存储用户信息的数组已满,进行扩容
	public User[] capacityUsers() {
		User [] newUsers = new User[users.length+10];
		for(int i=0;i<users.length;i++) {
			newUsers[i] = users[i];
		}
		users = newUsers;
		return users;
	}
	
	//这里是注册
	public void regist(String username,String password,String confirmPassword) {
		 //调用判断用户是否存在的方法getUser
		User isUser =getUser(username);
		if(isUser!=null){
			System.out.println("注册失败");
			System.out.println("账号已存在");
			return;
		}
		//调用lengthUser方法,判断用户输入的账号,密码是否满足长度限制
		User userLength = lengthUser(username, password);
		if(userLength ==null) {
			System.out.println("注册失败");
			System.out.println("账号密码的长度在范围之外");
			return;
		}
		//判断两次密码是否一致(若出现以上两种情况,则不会执行此步骤)
		if(password.equals(confirmPassword)) {
			//初始储存信息(假装的储存信息)
			User registUser = new User();
			registUser.username = username;
			registUser.password = password;
			//判断是否需要扩容
			if(number>=users.length) {
				capacityUsers();
			}
			//真正的储存信息
			users[number] = registUser;
			//注册认识+1
			number++;
			System.out.println("注册成功!");
		}else {
			System.out.println("注册失败");
			System.out.println("输入的两次密码不一致");
		}
	}
	
	//这里是判断账号密码的长度
	public User lengthUser(String username,String password) {
		//账号和密码的长度都在6~10之间
		if((username.length()>=6&&username.length()<=10)&&(password.length()>=6&&password.length()<=10)) {
			User user = new User();
			user.username = username;
			user.password = password;
			return user;
		}else {
			return null;
		}
			
	}
	
	//这里是登录
	public User login(String username,String password) {
		/*	调用判断账号是否存在的方法getUser
		 * 	若账号存在,则返回该账号的信息
		 * 	判断该账号的密码是否与用户输入的密码一致
		 */
		User user = getUser(username);
		if(user==null) {
			System.out.println("登录失败!");
			System.out.println("账号不存在!");
			return null;
		}else {
			if(password.equals(user.password)&&user.loginNum<=3) {
				System.out.println("登陆成功!");
				//登录失败次数清零
				user.loginNum=0;
				return user;
			}else if(user.loginNum>3){
				System.out.println("你的账号已冻结");
				System.out.println("请到柜台办理相关手续");
				return null;
			}else {
				System.out.println("登录失败!");
				System.out.println("账号密码不匹配!");
				if(3-user.loginNum==0) {
					System.out.println("你的账号已冻结");
					System.out.println("请到柜台办理相关手续");
					return null;
				}
				
				System.out.println("您还有"+(3-user.loginNum)+"次机会");
				user.loginNum++;
				return null;
			}	
		}
	}
	
}

Bank类(环境类)

package com.mhq.homework;

import java.util.Scanner;

public class Bank {
	//main方法
	public static void main(String[] args) {
		//新建Scanner对象和ATM对象
		Scanner scanner = new Scanner(System.in);
		ATM atm = new ATM();
		//无限循环(模拟ATM机的机制,一个用户使用之后,下一个用户可以继续使用)
		while(true) {
			index(scanner,atm);
		}
	}
	
	//初始界面
	public static void index(Scanner scanner,ATM atm) {
		System.out.println("*********************************************");
		System.out.println("客官,里面请");
		System.out.println("请选择您的操作:\n1.注册\n2.登录");
		String tag = scanner.next();
		switch (tag) {
		case "1":
			System.out.println("欢迎来到注册!");
			System.out.println("账号密码应为6~10位");
			System.out.print("请输入账号:");
			String registUsername = scanner.next();
			System.out.print("请输入密码:");
			String registPassword = scanner.next();
			System.out.print("请确认密码:");
			String confirmPassword = scanner.next();
			atm.regist(registUsername,registPassword,confirmPassword);
			break;
		case "2":
			System.out.println("欢迎来到登录!");
			System.out.print("请输入账号:");
			String loginUsername = scanner.next();
			System.out.print("请输入密码:");
			String loginPassword = scanner.next();
			User user = atm.login(loginUsername,loginPassword);
			//登陆成功,跳转到用户界面
			if(user!=null) {
					userIndex(scanner, atm, user);
			}
			break;
		default:
			System.out.println("不要调皮,请重新选择");
			index(scanner,atm);
			break;
		}
	}
	
	//用户登录成功后的界面
	public static void userIndex(Scanner scanner,ATM atm,User user) {
		System.out.println("*********************************************");
		System.out.println("欢迎您:"+user.username);
		System.out.println("请选择您的操作:\n1.存钱\n2.取钱\n3.转账\n4.查询余额\n5.修改密码\n6.退出");
		int number=0;
		String tag = scanner.next();
		switch (tag) {
		case "1":
			System.out.println("这里是存钱");
			System.out.print("请输入你要存的钱数(元):");
			double saveMoney = scanner.nextDouble();
			atm.saveMoney(user, saveMoney);
			userIndex(scanner, atm, user);
			break;
		case "2":
			System.out.println("这里是取钱");
			System.out.print("请输入你要取的钱数(元):");
			double getMoney = scanner.nextDouble();
			atm.getMoney(user, getMoney);
			userIndex(scanner, atm, user);
			break;
		case "3":
			System.out.println("这里是转账");
			System.out.print("请输入你要转的账户:");
			String getMoneyUsername = scanner.next();
			//需要判断到账人的账号是否存在,存在才可以转账
			User getMoneyUser = atm.getUser(getMoneyUsername);
			if(getMoneyUser!=null && getMoneyUser.username!=user.username) {
				System.out.print("请输入你要转的钱数(元):");
				double transferMoney = scanner.nextDouble();
				atm.transferMoney(user,getMoneyUser,transferMoney);
			}else {
				System.out.println("你要转的账户不存在");
				System.out.println("转账错误");
			}
			userIndex(scanner, atm, user);
			break;
		case "4":
			System.out.println("这里是查询余额");
			atm.findMoney(user);
			userIndex(scanner, atm, user);
			break;
		case "5":
			System.out.println("这里是修改密码");
			System.out.println("新密码的长度应为6~10");
			System.out.print("请输入新密码:");
			String newPassword = scanner.next();
			System.out.print("请确认新密码:");
			String confirmPassword = scanner.next();
			boolean or = atm.updatePassword(newPassword, confirmPassword, user);
			//修改密码有三次机会,若三次都错误,返回初始界面
			if(or==false) {
				number++;
				if(number>=3) {
					System.out.println("你的机会已用完,即将退出登录");
					index(scanner, atm);
					break;
				}
				nextUpdatePassword(atm, scanner, user,number);
			}
			userIndex(scanner, atm, user);
			break;
		case "6":
			System.out.println("欢迎下次光临");
			index(scanner,atm);
			break;
		default:
			System.out.println("选择错误,请重新选择您的操作");
			userIndex(scanner, atm, user);
			break;
		}
	}
	
	//继续修改密码
	public static void nextUpdatePassword(ATM atm,Scanner scanner,User user,int number) {
		System.out.print("你是否继续修改密码(Y/N):");
		String tag = scanner.next();
		switch (tag) {
		case "Y":
		case "y":
			System.out.println("这里是修改密码");
			System.out.print("请输入新密码:");
			String newPassword = scanner.next();
			System.out.print("请确认新密码:");
			String confirmPassword = scanner.next();
			boolean or = atm.updatePassword(newPassword, confirmPassword, user);
			if(or ==false) {
				number++;
				if(number>=3) {
					System.out.println("你的机会已用完,即将退出登录");
					index(scanner, atm);
					break;
				}
				nextUpdatePassword(atm, scanner, user,number);
			}
			break;
		case "N":
		case "n":
			userIndex(scanner, atm, user);
			break;
		default:
			System.out.println("输入错误,请重新输入");
			nextUpdatePassword(atm,scanner,user,number);
			break;
		}
	}
	
}

你可能感兴趣的:(java,javase)