分别编写三个类:用户类User、银行类Atm和测试类TestAtm()。
package com.sanxiau.bms;
/*
用户信息类
*/
public class User {
String bid;
String name;
String password;
long money = 0;
public User(String bid, String name, String password) {
this.bid = bid;
this.name = name;
this.password = password;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"账号:'" + bid + '\'' +
", 姓名:'" + name + '\'' +
", 密码:'" + password + '\'' +
", 余额:" + money +
'}';
}
}
package com.sanxiau.bms;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Atm {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Scanner scanner = new Scanner(System.in);
User user = new User();
//1.注册账户
public boolean postUser(){
System.out.println("----------注册账户---------");
System.out.println("1.请输入账户:");
user.bid= scanner.nextLine();
System.out.println("2.请输入姓名:");
user.name = scanner.nextLine();
System.out.println("3.请输入密码:");
user.password = scanner.nextLine();
if (user.name != null && user.bid != null && user.password != null) {
return true;
}
else {
return false;
}
}
//2.登录账户
public boolean recordUser(){
System.out.println("----------登录账户,输入-1取消---------");
while(true){
System.out.println("请输入账户:");
String id = scanner.nextLine();
System.out.println("请输入密码:");
String pw = scanner.nextLine();
//判断密码和账户是否相等
if (id.equals(user.bid) && pw.equals(user.password)) {
return true; //相等
} else if (!id.equals(user.bid) && !pw.equals(user.password)) {
System.out.println("账户和密码错误,请重新输入.....");
} else if (!id.equals(user.bid)) {
System.out.println("账号错误,请重新输入.....");
} else if (!pw.equals(user.password)) {
System.out.println("密码错误,请重新输入.......");
}
if (pw.equals("-1") || id.equals("-1")) {
return false;
}
}
}
//3.存钱
public void saveMoney(){
System.out.println("----------存钱,输入-1取消----------");
System.out.println("请输入存入的金额:");
int saveMoney = scanner.nextInt();
if (saveMoney == -1) {
return;
}
Date date = new Date();
user.money += saveMoney;
System.out.println(format.format(date)+"存进:"+saveMoney);
}
//4.取钱
public void drawMoney(){
System.out.println("----------取钱,输入-1取消----------");
System.out.println("请输入取出的金额:");
int drawMoney = scanner.nextInt();
if (drawMoney == -1) {
return;
}
if(user.money - drawMoney < 0){
System.out.println("余额不足,不能取出....");
return;
}
Date date = new Date();
user.money -= drawMoney;
System.out.println(format.format(date)+"取出:"+drawMoney);
}
//5.查询
public void selectMoney(){
System.out.println("----------查询----------");
System.out.println(user.toString());
}
//6.转账
public void carryMoney(){
System.out.println("----------转账,输入-1取消----------");
System.out.println("请输入转账的账户:");
String zid = scanner.next();
if (zid.equals("-1")) {
return ;
}
System.out.println("请输入转账金额:");
int carryMoney = scanner.nextInt();
if (carryMoney == -1) {
return ;
}
if(user.money - carryMoney < 0){
System.out.println("余额不足,不能转账....");
return ;
}
user.money -= carryMoney;
System.out.println(System.currentTimeMillis()+"转账到"+zid+"账户,金额为:"+carryMoney);
}
//7.注销
public boolean logoutUser(){
System.out.println("----------注销----------");
System.out.println("是否注销账户:1确认 2.取消");
int flags = scanner.nextInt();
if(flags == 1){
if (user.money > 0) {
System.out.println("当前用户存有金额,不能注销账户....");
return false;
}else{
System.out.println("您的账户已注销.....");
user.bid = null;
user.bid = null;
user.password = null;
return true;
}
} else {
return false;
}
}
}
package com.sanxiau.bms;
import java.util.Scanner;
//测试类
public class TestAtm {
public static void main(String[] args) {
Atm atm = new Atm();//创建一个money类对象
Scanner scanner = new Scanner(System.in);
System.out.println("------------欢迎来到银行系统----------");
System.out.println("请选择:1注册账户;2登录账户");
int flags1 = scanner.nextInt();// 判断是注册还是登录
boolean flags2 =false; //判断是否登录成功
if (flags1 == 1) { //注册账户
if(atm.postUser()){ //判断是否注成功
System.out.println("注册成功,进行第一次登录....");
if(atm.recordUser()){ //判断是否登录成功
System.out.println("登录成功,进入主页面.....");
flags2 = true;
}
else{
System.out.println("注册失败,退出..........");
}
}
} else if (flags1 == 2) { //登录账户
if(atm.recordUser()) { //判断是否登录成功
System.out.println("登录成功,进入主页面.....");
flags2 = true;
}
else{
System.out.println("登录失败,退出系统........");
}
}else{ //除了注册和登录外的异常
System.out.println("选择失败,退出..........");
}
//登录成功,进入首页
if (flags2) { //首页菜单
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:退出----------");
while (true){ //使用循环,在系统中选择服务
System.out.println("请选择服务:");
int i = scanner.nextInt(); //接收选择选项...
boolean flags3 = false; //判断当前账户是否注销
switch (i) { //对选择选项进行匹配操作
case 1:atm.saveMoney();
break; //存钱
case 2:atm.drawMoney();
break; //取钱
case 3:atm.selectMoney();
break; //查询
case 4:atm.carryMoney();
break; //转账
case 5:flags3 = atm.logoutUser();
break; //注销
case 6:
break; //退出
default:
System.out.println("请重新选择........");
}
if (i == 6 || (flags3 == true)){
//当账户注销或者退出时,退出系统
System.out.println("您已退出系统...........");
break;
}
}
}
}
}