public class ATMSystem {
//银行账户,第一维:账户索引ID,第二维:账户详情,如:
//accounts[0][0] 表示第1个账户的登录名称
//accounts[0][1] 表示第1个账户的登录密码
//accounts[0][2] 表示第1个账户的账户余额
private static String[][] accounts = {{"leon","123456","1000"},{"satol","199610","900"}};
//功能操作数组
private static String[] menus = { "1:余额查询", "2:存款", "3:取款", "4:转账", "5:修改密码", "0:退出" };
private static Scanner sc =new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
// 初始化账户信息
initAccounts();
// 显示登陆界面
welcome();
// 系统登录,登录成功后返回账户索引ID
int accountIndex = login();
//登录成功
if (accountIndex != -1) {
//无限循环,保证系统始终处于运行状态
while (true) {
//输出系统菜单
printMenu();
//选择的系统菜单Id
int menuId = sc.nextInt();
switch (menuId) {
case 1: // 余额查询
queryBalance(accountIndex);
break;
case 2: // 存款
deposit(accountIndex);
break;
case 3: // 取款
withdraw(accountIndex);
break;
case 4: // 转账
transfer(accountIndex);
break;
case 5: // 修改密码
changePassword(accountIndex);
break;
case 0: // 退出系统
System.out.println("系统退出成功!");
System.exit(0);
default:
System.out.println("非法操作");
break;
}
}
}
}
`
//转账
private static int transfer(int accountIndex){
System.out.println("*********************************************");
System.out.println("请输入对方账户:");
String theSecondAcount = sc.next();
int index=-1;
for (int i=0;i<accounts.length;i++){
if (accounts[i][0].equals(theSecondAcount)){
index = i;
}
}
if (index == -1){
System.out.println("该用户不存在!请核对后再操作!");
}else {
while(true){
System.out.println("转账规则:每次转钱不能超过1000,金额必须是100的整数!确保您的余额是否满足!");
System.out.println("输入转账金额:");
int cashCount = sc.nextInt();
int i = Integer.parseInt(accounts[accountIndex][2]);
if (cashCount<1000&&cashCount%100==0&&cashCount > 0){
if(cashCount > i){
System.out.println("您的账户余额不足!");
}else {
i-=cashCount;
accounts[accountIndex][2]=String.valueOf(i);
System.out.println("转账成功!请对方账户确认!");
System.out.println("*********************************************");
int theI= Integer.parseInt(accounts[index][2]);
theI+=cashCount;
accounts[index][2]=String.valueOf(theI);
System.out.println("您的余额为:");
queryBalance(accountIndex);
System.out.println("*********************************************");
break;
}
} else{
System.out.println("您的操作不符合规范,请仔细阅读转账规则!");
continue;
}
}
}
return index;
}
private static void withdraw(int accountIndex) {
// TODO Auto-generated method stub
System.out.println("*********************************************");
while(true){
System.out.println("取钱规则:每次取钱不能超过1000,取款必须是100的整数!确保您的余额是否满足!");
System.out.println("输入取款金额:");
int cashCount = sc.nextInt();
int i = Integer.parseInt(accounts[accountIndex][2]);
// if (cashCount<1000&&cashCount%100==0&&cashCount < i){
// i-=cashCount;
// accounts[accountIndex][2]=String.valueOf(i);
// System.out.println("请取走钞票!请确认!");
// break;
// }
if (cashCount<1000&&cashCount%100==0&&cashCount > 0){
if(cashCount > i){
System.out.println("您的账户余额不足!");
}else {
i-=cashCount;
accounts[accountIndex][2]=String.valueOf(i);
System.out.println("请取走钞票!请确认!");
System.out.println("*********************************************");
break;
}
} else{
System.out.println("您的操作不符合规范,请仔细阅读取钱规则!");
continue;
}
}
}
private static void deposit(int accountIndex) {
// TODO Auto-generated method stub
System.out.println("*********************************************");
while(true){
System.out.println("存钱规则:每次存钱不能超过1000,存款必须是100的整数!");
System.out.println("请整理好您的钞票,放入出钞口!(输入存款金额):");
int cashCount = sc.nextInt();
if (cashCount<1000&&cashCount%100==0){
int i = Integer.parseInt(accounts[accountIndex][2]);
i+=cashCount;
accounts[accountIndex][2]=String.valueOf(i);
System.out.println("存款成功!请确认!");
System.out.println("*********************************************");
break;
}else{
System.out.println("您的操作不符合规范,请仔细阅读存钱规则!");
System.out.println("*********************************************");
continue;
}
}
}
private static void queryBalance(int accountIndex) {
// TODO Auto-generated method stub
System.out.println(accounts[accountIndex][0]+",您的余额为:");
System.out.println(accounts[accountIndex][2]);
System.out.println("*********************************************");
}
private static void printMenu() {
// TODO Auto-generated method stub
for (int i=0;i<menus.length;i++){
System.out.println(menus[i]);
}
}
private static int login() {
// TODO Auto-generated method stub
//int loginAcountsCount = 0;
int loginPasswdCount = 0;
int valueOfReturn=-1;
int loopNumber = 1;
int index = 0;
System.out.println("请输入账户名:");
String acountsName = sc.next();
for (int i=0;i<accounts.length;i++){
if (accounts[i][0].equals(acountsName)){
index = i;
}
}
while(loopNumber==1){
System.out.println("请输入密码:");
if (accounts[index][1].equals(sc.next())){
System.out.println("登录成功!");
System.out.println("当前用户:"+acountsName);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(System.currentTimeMillis()));
System.out.println("*********************************************");
valueOfReturn = index;
loopNumber=-1;
}else{
loginPasswdCount++;
if (loginPasswdCount >= 5){
System.out.println("对不起,您已经尝试了5次密码输入." +
"您的账户已经被冻结了。" +
"不能操作了," +
"请到柜台办理密码更改业务!");
loopNumber=-1;
valueOfReturn=-1;
}else{
System.out.println("密码错误!!" +
"您还有"+
(5-loginPasswdCount)+
"次机会输入," +
"请确保自己的输入!");
continue;
}
}
}
return valueOfReturn;
}
private static void welcome() {
// TODO Auto-generated method stub
System.out.println("-----------------------------------------");
System.out.println("*******欢迎来到深圳银行********");
System.out.println("*******请按照菜单进行操作*******");
System.out.println("-----------------------------------------");
}
private static void initAccounts() {
// TODO Auto-generated method stub
System.out.println("开始初始化,请稍等......");
}
private static void changePassword(int accountIndex) {
// TODO Auto-generated method stub
System.out.println("请输入您需要设置的密码:");
String passwd1 = sc.next();
System.out.println("请再输入一次:");
String passwd2 = sc.next();
if(passwd1.equals(passwd2)){
System.out.println("密码修改成功!!");
accounts[accountIndex][1]=passwd1;
}else {
System.out.println("您两次输入的密码不一致!请重新操作!");
changePassword(accountIndex);
}
}
}
`