这一篇只有源代码,共三个类,废话不多说,上代码!想看设计思想以及具体步骤的→传送门
用户类(User实体类)
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;
public boolean updatePassword(String newPassword,String confirmPassword,User user) {
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) {
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) {
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+"元");
}
public void transferMoney(User transferUser,User getMoneyUser,double money) {
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+"元");
}
}
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) {
User isUser =getUser(username);
if(isUser!=null){
System.out.println("注册失败");
System.out.println("账号已存在");
return;
}
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;
number++;
System.out.println("注册成功!");
}else {
System.out.println("注册失败");
System.out.println("输入的两次密码不一致");
}
}
public User lengthUser(String username,String password) {
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) {
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 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ATM atm = new 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;
}
}
}