正确答案,刚做的
package com.kay.bank;
import java.util.Scanner;
public class Bank {
Scanner input = new Scanner(System.in);
Data data = new Data();
User crUser; //当前登陆的用户
public void start()
{
System.out.println("--------------------");
System.out.println("1.开户");
System.out.println("2.插卡");
System.out.println("3.系统管理");
System.out.println("4.退出");
System.out.println("--------------------");
System.out.println("请选择菜单:");
int num = input.nextInt();
switch(num)
{
case 1:
//开户操作
openAccount();
break;
case 2:
//插卡操作
insertCard();
break;
case 3:
//x系统管理操作
systemManager();
break;
default:
//退出
}
}
/**
* 开户
*/
public void openAccount()
{
User user = new User();
System.out.println("****************");
System.out.println("请输入用户名:");
user.name = input.next();
//验证系统中用户名是否存在
User checkUser = data.checkUserByName(user.name);
if(checkUser == null)
{
//系统中用户名不存在
System.out.println("请输入密码:");
String pwd = input.next();
System.out.println("请确认密码:");
String repwd = input.next();
if(pwd.equals(repwd))
{
user.pwd = pwd;
System.out.println("请输入存款金额:");
double money = input.nextDouble();
if(money <= 0)
{
System.out.println("输入的金额不合法,请重新输入!");
openAccount();
}
else
{
user.money = money;
//向数据库中存放新创建的User
data.saveUser(user);
System.out.println("创建新用户成功!");
start();
}
}
else
{
System.out.println("密码不一致,请重新输入!");
openAccount();
}
}
else
{
System.out.println("用户名已存在");
start();
}
}
/**
* 插卡操作
*/
public void insertCard()
{
System.out.println("请输入用户名:");
String name = input.next();
System.out.println("请输入密码:");
String pwd = input.next();
User user = data.checkUser(name, pwd);
if(user != null)
{
if(user.isEnable == true)
{
System.out.println("登陆成功");
crUser = user;
showCustomMenu();
}
else
{
System.out.println("您的账户被冻结,请打110");
start();
}
}
else
{
System.out.println("用户名或密码错误");
insertCard();
}
}
/**
* 显示插卡登陆成功后二级菜单
*/
public void showCustomMenu()
{
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.退出");
System.out.println("---------------");
System.out.println("请选择:");
int num = input.nextInt();
switch(num)
{
case 1:
//查询余额
getMoney();
break;
case 2:
//取款
minusMoney();
break;
case 3:
//存款
addMoney();
break;
case 4:
//转账
transitionMoney();
break;
case 5:
//修改密码
updatePwd();
break;
case 6:
exitCustomMenu();
//退出
}
}
/**
* 获得当前用户的余额
*/
public void getMoney()
{
System.out.println("您的余额为:" + crUser.money);
showCustomMenu();
}
/**
* 取款
*/
public void minusMoney()
{
System.out.println("请输入取款额:");
double money = input.nextDouble();
if(money >= 0 && money < crUser.money)
{
//可以取款
crUser.money = crUser.money - money;
showCustomMenu();
}
else
{
System.out.println("金额有误!请重新输入!");
minusMoney();
}
}
public void exitCustomMenu()
{
start();
}
/**
* 存款
*/
public void addMoney()
{
System.out.println("请输入存款额:");
double money = input.nextDouble();
if(money >= 0)
{
crUser.money = crUser.money + money;
showCustomMenu();
}
else
{
System.out.println("金额不合法!");
addMoney();
}
}
/**
* 修改密码
*/
public void updatePwd()
{
System.out.println("请输入原始密码:");
String oldPwd = input.next();
if(oldPwd.equals(crUser.pwd))
{
System.out.println("请输入新密码:");
crUser.pwd = input.next();
showCustomMenu();
}
else
{
System.out.println("原始错误,请重新输入!");
updatePwd();
}
}
/**
* 转账
*/
public void transitionMoney()
{
System.out.println("请输入要转入的账号名称:");
String name = input.next();
User intoUser = data.checkUserByName(name);
if(intoUser != null)
{
//转入的账号存在
System.out.println("请输入转入的金额:");
double money = input.nextDouble();
if(money >= 0 && money < crUser.money)
{
//金额合法并且余额满足转账条件 可以进行转账
crUser.money = crUser.money - money;
intoUser.money = intoUser.money + money;
showCustomMenu();
}
else
{
System.out.println("金额不合法,请重新输入!");
transitionMoney();
}
}
else
{
System.out.println("转入的账号不存在,请重新输入!");
transitionMoney();
}
}
/**
* 系统管理
*/
public void systemManager()
{
System.out.println("请输入管理员密码:");
String pwd = input.next();
if(pwd.equals("admin"))
{
//管理员登陆成功
showManagerMenu();
}
else
{
System.out.println("密码错误!");
start();
}
}
/**
* 显示管理员登陆后的菜单
*/
public void showManagerMenu()
{
System.out.println("******************");
System.out.println("1.显示所有用户");
System.out.println("2.冻结账号");
System.out.println("3.恢复账号");
System.out.println("4.退出");
System.out.println("******************");
System.out.println("请选择菜单项:");
int num = input.nextInt();
switch(num)
{
case 1:
//显示所有用户
showAllUser();
break;
case 2:
//冻结账号
freezeAccount();
break;
case 3:
//恢复账户
renewAccount();
break;
case 4:
start();
//退出
}
}
/**
* 显示所有用户
*/
public void showAllUser()
{
System.out.println("系统中所有用户:");
System.out.println("--------------------------------");
System.out.println("用户名\t是否可用");
for (int i = 0; i < data.userArray.length; i++) {
User user = data.userArray[i];
if(user != null)
{
System.out.println(user.name + "\t" + user.isEnable);
}
}
System.out.println("--------------------------------");
showManagerMenu();
}
/**
* 冻结账户
*/
public void freezeAccount()
{
System.out.println("系统中所有可被冻结用户:");
System.out.println("--------------------------------");
System.out.println("用户名\t是否可用");
for (int i = 0; i < data.userArray.length; i++) {
User user = data.userArray[i];
if(user != null && user.isEnable == true)
{
System.out.println(user.name + "\t" + user.isEnable);
}
}
System.out.println("--------------------------------");
System.out.println("请输入要被冻结的账户:");
String name = input.next();
User user = data.checkUserByName(name);
if(user != null)
{
//要冻结的账户存在
user.isEnable = false;
System.out.println("冻结成功!");
}
else
{
System.out.println("用户不存在!");
}
showManagerMenu();
}
/**
* 恢复账户
*/
public void renewAccount()
{
System.out.println("系统中所有可被恢复用户:");
System.out.println("--------------------------------");
System.out.println("用户名\t是否可用");
for (int i = 0; i < data.userArray.length; i++) {
User user = data.userArray[i];
if(user != null && user.isEnable == false)
{
System.out.println(user.name + "\t" + user.isEnable);
}
}
System.out.println("--------------------------------");
System.out.println("请输入要被恢复的账户:");
String name = input.next();
User user = data.checkUserByName(name);
if(user != null)
{
//要恢复的账户存在
user.isEnable = true;
System.out.println("恢复成功!");
}
else
{
System.out.println("用户不存在!");
}
showManagerMenu();
}
}
package com.kay.bank;
public class Data {
User[] userArray = new User[100];
/**
* 存储新的用户
* @param user 新用户对象
*/
public void saveUser(User user)
{
for (int i = 0; i < userArray.length; i++) {
if(userArray[i] == null)
{
userArray[i] = user;
break;
}
}
}
/**
* 根据用户名和密码验证用户是否在数组中存在
* @param name 用户名
* @param pwd 密码
*/
public User checkUser(String name,String pwd)
{
for (int i = 0; i < userArray.length; i++) {
User user = userArray[i];
if(user != null
&& user.name.equals(name)
&& user.pwd.equals(pwd))
{
//代表用户存在
return user;
}
}
return null;
}
/**
* 根据用户名验证用户是否存在
* @param name
* @return
*/
public User checkUserByName(String name)
{
for (int i = 0; i < userArray.length; i++) {
User user = userArray[i];
if(user != null
&& user.name.equals(name))
{
//代表用户存在
return user;
}
}
return null;
}
}
package com.kay.bank;
public class Test {
public static void main(String[] args) {
Bank atm = new Bank();
atm.start();
}
}
package com.kay.bank;
public class User {
String name;
String pwd;
double money;
boolean isEnable = true;
}