ATM程序设计
package com.arthur.object;
import java.util.Scanner;
/*** * 简单的ATM存取款程序 1.登陆页面 2.登陆 3.菜单:1,查询,2,存款,3,取款,4,修改密码,5退出 初始密码:123 用户名:admin */public class ATM2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
// 1.输出登陆页面
String pwd = ""; String pass = "";// 密码 double monnery = 12000; String go = "y"; int end = 0; for (int i = 1; i < 5; i++) { // 判断密码输入次数
// 判断密码登陆 if (i > 3) { System.out.println("密码输入次数过多,请稍后再试......"); break; }
System.out.println("请输入密码");
pass = scanner.next(); if (!pass.equals("123")) { System.out.println("输入错误,请重新输入"); } else {
System.out.println("登陆成功!");// 判断密码登陆结束 System.out.println("\t**************\t\t\t\t\t****************"); System.out.println("******************************\tATM取款程序\t********************************"); System.out.println("\t**************\t\t\t\t\t****************"); break; } } do { System.out.println("业务如下:"); System.out.println("1.查询 2.存款 3.取款 4.修改密码 5.退出"); System.out.println("请输入相对应的业务代码:"); int select = scanner.nextInt(); switch (select) { case 1:// 查询余额 System.out.println("余额是:" + monnery + "元"); break; case 2:// 存款 System.out.println("请输入金额:"); double deposit = scanner.nextDouble();// 存款 if (deposit <= 0) { System.out.println("金额不能为负数"); continue; } if (deposit % 100 != 0) { System.out.println("存款金额必须为100倍数"); continue; } monnery += deposit; System.out.println("存款成功,余额为:" + monnery + "元"); break; case 3:// 取款 System.out.println("请输入金额:"); double withdraw_money = scanner.nextDouble(); if (withdraw_money < 0) { System.out.println("取款不能为负数!"); continue; } if (withdraw_money % 100 != 0) { System.out.println("取款必须为100倍数"); continue; } if (withdraw_money > 10000 ) { System.out.println("每次取款不能超过1000元"); continue; } if(withdraw_money > monnery){ System.out.println("余额不足!"); } monnery -= withdraw_money;// 取款 System.out.println("取款成功,剩余金额为:" + monnery + "元"); break; case 4:// 修改密码 System.out.println("请输入原密码:"); pass = scanner.next(); if (pass.equals("123")) { System.out.println("请输入新密码:"); String new_pass = scanner.next(); System.out.println("请再次输入新密码:"); String new2_pass = scanner.next(); if (new_pass.equals(new2_pass)) {// 比较两次输入新密码是否相同 pass = new_pass; System.out.println("密码修改成功!新密码为:" + new_pass); } else { System.out.println("密码修改失败!"); } } else { System.out.println("原密码不对..."); } break; case 5:
end = -1; break; default: System.out.println("输入操作代码不对,请重新输入");
break;
} if (end == -1) { System.out.println("退出成功!"); break; } if (end != -1) { System.out.println("是否继续?(y/n)"); go = scanner.next(); }
} while (go.equals("y"));
if (go.equals("n")) { System.out.println("退出成功!"); }
}
}