ATM机(对象)
import java.util.Scanner;
//ATM机
public class ATMMachine {
private UserInfo theUser;// ATM中的用户信息
private int cash;// 现金
public final int MAX_CASH = 200000;// 最大现金额
// 构造方法
public ATMMachine() {
this.theUser = new UserInfo("王五", "123456", 500);
this.cash = 100000;
}
// 运行--控制ATM机的运行流程
public void run() {
this.welcome();
if (this.login()) {
System.out.println("欢迎您,登录成功!");
while (true) {
switch (this.choiceMenu()) {
case 1:
this.query();
break;
case 2:
this.getMoney();
break;
case 3:
this.storeMoney();
break;
case 4:
this.changePwd();
break;
case 5:
this.exit();
break;
default:
System.out.println("没有该选项");
}
}
} else {
System.out.println("三次登录失败,您的卡已经被没收!");
System.out.println("请在工作日带上有效证件,到柜台处理!");
}
}
// 欢迎
private void welcome() {
System.out.println("***************************");
System.out.println("**********欢**迎************");
System.out.println("**********使**用************");
System.out.println("**********ICBC*************");
System.out.println("***************************");
}
// 登录
private boolean login() {
for (int i = 0; i < 3; i++) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入用户名:");
String inputName = scan.next();
System.out.println("请输入密 码:");
String inputPwd = scan.next();
if (inputName.equals(this.theUser.getUsername()) && inputPwd.equals(this.theUser.getPassword())) {
return true;
} else {
System.out.println("用户名密码有误,您还有" + (2 - i) + "次机会!");
}
}
return false;
}
// 选择菜单
private int choiceMenu() {
System.out.println("请选择您要执行的操作:");
System.out.println("1、查询;2、取款;3、存款;4、修改密码;5、退出");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
// 查询
private void query() {
System.out.println("你的余额为:" + this.theUser.getAccount());
}
// 取款
private void getMoney() {
float sum;
System.out.println("请输入你的取款金额!");
Scanner scan = new Scanner(System.in);
int getmoney = scan.nextInt();
if (getmoney < 0) {
System.out.println("你输入的金额不能小于零,请重新输入!");
} else if (getmoney % 100 != 0) {
System.out.println("你输入的金额必须是100的倍数!");
} else if (getmoney >= this.theUser.getAccount()) {
System.out.println("你输入的金额超出了你的余额!");
} else {
this.cash -= getmoney;
this.theUser.setAccount(this.theUser.getAccount() - getmoney);
System.out.println("取款金额为:" + getmoney);
System.out.println("你当前余额为:" + this.theUser.getAccount());
}
}
// 存款
private void storeMoney() {
System.out.println("请放入你要存款的金额:");
Scanner scan = new Scanner(System.in);
int setmoney = scan.nextInt();
if (setmoney + this.cash > this.MAX_CASH) {
System.out.println("你放入的金额超出最大存储金额!");
} else if (setmoney < 0 && setmoney % 100 != 0) {
System.out.println("你放入的金额不符合存储范围,必须为100的倍数!");
} else {
this.cash += setmoney;
this.theUser.setAccount(this.theUser.getAccount() + setmoney);
System.out.println("你放入的金额为" + setmoney);
System.out.println("你当前的余额为:" + this.theUser.getAccount());
}
}
// 修改密码
private void changePwd() {
Scanner scan = new Scanner(System.in);
String inputPwd;
String Pwd;
String Pwd1;
while (true) {
System.out.println("请输入你的旧密码:");
inputPwd = scan.next();
if (inputPwd.equals(this.theUser.getPassword())) {
break;
} else {
System.out.println("重新输入旧密码:");
}
}
while (true) {
System.out.println("请输入你的新密码:");
Pwd = scan.next();
System.out.println("请再次确认你的密码:");
Pwd1 = scan.next();
if(Pwd.equals(Pwd1)){
this.theUser.setPassword(Pwd);
System.out.println("修改成功");
System.out.println("修改后密码是:"+this.theUser.getPassword());
break;
}else{
System.out.println("两次不一致,重新输入!");
}
}
}
// 退出
private void exit() {
System.out.println("谢谢使用,期待下次光临!");
System.exit(0);
}
}
//用户信息(对象)
public class UserInfo {
private String username;//用户名
private String password;//密码
private float account;//账户
public UserInfo(){
}
public UserInfo(String username, String password, float account) {
this.username = username;
this.password = password;
this.account = account;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public float getAccount() {
return account;
}
public void setAccount(float account) {
this.account = account;
}
}
ATM机 运行
public class TestMain {
public static void main(String[] args) {
ATMMachine atm = new ATMMachine();
atm.run();
}
}