package atm;
//------ATM模拟系统------
import java.util.Scanner;
public class ATM {
private String AccountNum = "1367111222";// 账号
private String password = "123456";// 密码
private long balance = 10000;// 初始余额
Scanner sc = new Scanner(System.in);
// 构造函数
public ATM() {
}
public ATM(String temp, String temp2) {
this.AccountNum = temp;
this.password = temp2;
}
// -------修改密码模块------
public void changePassword(String oldPass, String password) {
if (!oldPass.equals(this.password)) {// 判断初始密码
System.err.println("Wrong initial password.");
return;
}
if (password.length() < 6) {// 判断新密码长度
System.err.println("Password too short.");
return;
}
if (this.password.equals(password)) { // 不能与原密码相同
System.err.println("Password cannot be the same.");
return;
}
this.password = password;
System.out.println("newpassword:" + this.password);
}
// -------查询余额模块------
public long balanceInquery() {
return this.balance;
}
// ------存款模块------
public void deposit() {
int amount;
System.out.println("请输入存款金额:");
amount = sc.nextInt();
if (amount < 0) { // 避免出现负存款
System.err.println("Cannot deposit negative amount");
return;
}
this.balance += amount;
System.out.println("balance=" + this.balance);
}
// -------取款模块------
public void withdraw() {
int amount;
System.out.println("请输入取款金额:");
amount = sc.nextInt();
if (amount > 5000 || amount < 0) { // 每次取款不能超过5000
System.err.println("Withdraw limit:¥0-¥5000");
System.exit(0);
}
if ((amount % 100) != 0) { // 取款为100倍数
System.err.println("The amount has to be a product of100");
System.exit(0);
}
long newBalance = this.balance - amount;
if (newBalance < 0) { // 取款后余额不能为负
System.err.println("Not enough money in the account");
}
this.balance = newBalance;
System.out.println("balance=" + this.balance);
}
// -------主界面显示模块------
public void menu() {
int select;
// ATM a=new ATM();
try {
// String AccountNum="1367111222";
// String password="123456";
// Scanner sc=new Scanner(System.in);
System.out
.println("-----------------------------------欢迎使用ATM模拟系统------------------------");
System.out.print("\t请输入账号:");
AccountNum = sc.next();
System.out.print("\t请输入密码:");
password = sc.next();
if (!AccountNum.equals(this.AccountNum)) {
System.err.println("账号错误"); // 验证登陆账号
System.exit(0);
} else if (!password.equals(this.password)) {
System.err.println("密码错误"); // 验证登陆密码
System.exit(0);
} else {
System.out.println("登陆成功,进入主菜单!");
do {
System.out
.println("\n ************1查询************");
System.out
.println("\n ************2取款************");
System.out
.println("\n ************3存款************");
System.out
.println("\n ************4修改密码**********");
System.out
.println("\n ************0退出************");
System.out.println("\n请输入选择:");
System.out
.println("----------------------------------------------------------------");
select = sc.nextInt();
switch (select) {
case 1:
System.out.println("Balance=" + this.balanceInquery());// 余额查询
break;
case 2:
this.withdraw(); // 取款
break;
case 3:
this.deposit();// 存款
break;
case 4:
System.out.println("Oldpassword:");
String temp = sc.next();
System.out.println("Newpassword:");
String temp2 = sc.next();
this.changePassword(temp, temp2);// 改密
break;
case 0:
System.exit(0);// 退出
break;
default:
System.out.println("请输入数字1-5");
}// end switch
} while (true);// 实现循环
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ATM a = new ATM();
a.menu();
}
}