新ATM机 学会使用面向对象的思想来编程

来源:互联网 发布:淘宝直通车一天多少钱 编辑:程序博客网 时间:2024/06/05 16:05
package com.lovo.bean;


import java.util.Scanner;
//2个对象   一个ATM  一个使用者
public class Atm {


private UserBean user;// 用户


private int cash;// ATM中的现金


private String bankName;// 所属银行


public final int MAX_CASH = 100000;


public Atm() {
// 初始化数据
this.cash = 50000;
this.bankName = "ICBC爱存不存银行";
this.user = new UserBean("001003002132", "132231", 520);
}


// 运行方法---流程控制
public void run() {

this.wlecome();


boolean flag = this.login();
if (flag) {
while (true) {
int choice = this.chooseMenu();
switch (choice) {
case 1:
this.query();
break;
case 2:
this.getMoney();
break;
case 3:
this.storeMoney();
break;
case 4:
boolean pc = this.changePwd();
if (!pc) {
this.exit();
}
break;
case 5:
this.exit();
break;
default:
System.out.println("没有该选项,请重新选择");
break;
}
}
} else {
this.exit();
}


}


// 欢迎
private void wlecome() {
System.out.println("**********************");
System.out.println("**欢迎来到***************");
System.out.println("********" + this.bankName + "*");
System.out.println("**********************");
System.out.println("*************verson 1.0");
}


// 登录
private boolean login() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入卡号:");
String inputCardNum = scan.next();
System.out.println("请输入密码:");
String inputPwd = scan.next();


if (inputCardNum.equals(this.user.getCardNum())
&& inputPwd.equals(this.user.getPassword())) {
System.out.println("卡号:" + this.user.getCardNum() + "用户,登录成功!");
return true;
} else {
System.out.println("卡号或密码有误,请查证。");
}


}
System.out.println("三次机会使用完毕,您的卡被没收,请到柜台处理!");
return false;


}


// 菜单选择
private int chooseMenu() {
Scanner scan = new Scanner(System.in);
System.out.println("请选择你要执行的操作:");
System.out.println("1、查询;2、取款;3、存款;4、修改密码;5、退出;");
int choice = scan.nextInt();
return choice;
}


// 查询余额
private void query() {
System.out.println("您当前余额为:" + this.user.getAccount());
}


// 取钱
private void getMoney() {
System.out.println("请输入你要取款的金额");
Scanner scan = new Scanner(System.in);
int getMyMoney = scan.nextInt();
if (getMyMoney <= this.cash) {
if (getMyMoney % 100 == 0) {
if (getMyMoney <= this.user.getAccount()) {
this.user.setAccount(this.user.getAccount() - getMyMoney);
System.out.println("取款成功");
} else {
System.out.println("你的账户余额不足");


}


}


else {
System.out.println("请输入100面值的取款金额");
getMoney();


}
} else {
System.out.println("ATM机余额不足");
getMoney();


}
}


// 存钱
private void storeMoney() {


Scanner scan = new Scanner(System.in);
System.out.println("请输入你要存入的金额");
int saveMoney = scan.nextInt();
if (this.cash + saveMoney <= MAX_CASH) {
if (saveMoney % 100 == 0) {
this.user.setAccount(this.user.getAccount() + saveMoney);
System.out.println("储存成功");
this.cash = this.cash + saveMoney;
} else {
System.out.println("请存入100元面值的金额");
storeMoney();
}
} else {
System.out.println("今日存款额度已满,请到其他ATM存款");
}
}


// 修改密码
private boolean changePwd() {


for (int i = 1; i <= 3; i++) {
System.out.println("请输入原密码");
Scanner scan = new Scanner(System.in);
String password = scan.next();
if (password.equals(this.user.getPassword())) {
System.out.println("请输入你要修改的密码");
String password1 = scan.next();
this.user.setPassword(password1);
System.out.println("密码修改成功");
return true;
} else {
System.out.println("你输入的密码不正确,你还有" + (3 - i) + "次机会");
}
}
System.out.println("3次机已经用完,你的卡将被没收,请到柜台进行处理");
return false;
}


// 退出
private void exit() {
System.out.println("谢谢您的光顾,请下次再来!");
System.exit(1);// 关闭虚拟机
}


}








package com.lovo.bean;


public class UserBean {

private String cardNum;//卡号

private String password;//密码

private double account;//余额

public UserBean(){

}

public UserBean(String cardNum, String password, double account) {
this.cardNum = cardNum;
this.password = password;
this.account = account;
}


public String getCardNum() {
return cardNum;
}


public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public double getAccount() {
return account;
}


public void setAccount(double account) {
this.account = account;
}
}





0 0
原创粉丝点击