本周学习了面向对象编程,所以重做了Atm机代码

一.

public class TestMain {

public static void main(String[] args) {

ATMMachine machine = new ATMMachine();
machine.run();

}

}

 

 

 

二.

//用户信息
public class UserInfo {
//账号
private String username;
//密码
private String password;
//余额
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;
}
}

 

 

 

三.

import java.util.Scanner;


//ATM机
public class ATMMachine {
// 用户信息
private UserInfo user;
// 现金
private int cash;
// 现金容量上限
public final int MAX_CASH = 200000;
// 存取上限
public final int GET_SET_MAX = 2000;
private float account;
private Object choose;

public ATMMachine() {
// 预载入用户信息
this.user = new UserInfo("J124", "123456", 500);
this.cash = 100000;
}

// 运行
public void run() {

this.welcome();
boolean flag = this.login();
if (flag) {
System.out.println("欢迎您,用户" + this.user.getUsername() + "!");
while (true) {
int choice = this.choiceMenu();
switch (choice) {
case 1:
this.query();

break;
case 2:
this.storeMoney();
break;
case 3:
this.getMoney();
break;
case 4:
this.changePwd();
break;
case 5:
this.exit();
break;
default:
System.out.println("对不起,没有该选项!");
break;
}
}
} else {
System.out.println("您的账户被冻结!请到柜台处理!");
}
}

// 显示欢迎
private void welcome() {
System.out.println("******************************");
System.out.println("*********欢**迎**登**录*********");
System.out.println("******************************");
System.out.println("*********爱存不存银行ATM*********");
System.out.println("******************************");
System.out.println("*****************version1.0***");
}

// 登录
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.user.getUsername())
&& inputPwd.equals(this.user.getPassword())) {
return true;
} else {
System.out.println("输入有误!您还有" + (2 - i) + "次机会");
}
}
return false;
}

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

// 查询余额
private void query() {
//直接显示user对象中的account属性值
Scanner scan=new Scanner(System.in);
System.out.println(this.user.getAccount());
System.out.println("请选择操作 1.返回 2.退出");
int choose=scan.nextInt();
switch(choose){
case 1:
break;
case 2:
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}
}


// 存钱
private void storeMoney() {
//1、接收用户输入
//2、判断--不能为0,不能为负,不能不是100的倍数,不能超过存取上限,不能超过现金容量--给出提示
//3、判断通过--加现金,加余额

float storemoney=0;
Scanner scan=new Scanner(System.in);

System.out.println("请输入存款金额!");
storemoney=scan.nextFloat();
if(storemoney<=0 ){
System.out.println("输入金额不正确,请输入正确金额!");
storemoney=0;
}
else if((int) (storemoney) % 100!=0){
System.out.println("请输入正确取款金额!");
storemoney=0;
}
else if(storemoney>this.GET_SET_MAX){
System.out.println("存取上线,请重新选择金额,或去柜台办理业务!");
storemoney=0;
}
else if(this.cash+storemoney>this.MAX_CASH){
System.out.println("金额上线,请到柜台办理该业务!");
storemoney=0;
}
else{
System.out.println("恭喜存钱成功!");
}

this.user.account+=storemoney;
this.cash+=storemoney;
System.out.println("请选择操作 1.返回 2.退出");
int choose = scan.nextInt();
switch(choose){
case 1:
break;
case 2:
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}
}

// 取款
private void getMoney() {

float getmoney=0;
Scanner scan=new Scanner(System.in);

System.out.println("请输入取款金额!");
getmoney=scan.nextFloat();
if((int)(getmoney)%100!=0){
System.out.println("请输入正确金额!");
getmoney=0;
}
else{
System.out.println("恭喜你,取钱成功,请拿好现金!");

}

this.user.account-=getmoney;
this.cash-=getmoney;
System.out.println("请选择操作 1.返回 2.退出");
int choose=scan.nextInt();
switch(choose){
case 1:
break;
case 2:
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}
}


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

Scanner scan=new Scanner(System.in);

System.out.println("请输入需要修改的密码");
String password=scan.next();
if(password.equals(this.user.getPassword())){
System.out.println("请第一次输入新的密码");
String password1=scan.next();
System.out.println("请第二次输入新的密码");
String password2=scan.next();
if(password1.equals(password2)){
System.out.println("密码修改成功");
}
else{
System.out.println("两次密码输入不一致,请重新输入");
}

}

else{
System.out.println("密码输入有误,请重新输入");
}
System.out.println("请选择操作 1.返回 2.退出");
int choose=scan.nextInt();
switch(choose){
case 1:
break;
case 2:
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}

}

// 退出
private void exit() {
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}

}

posted on 2016-05-22 22:20  喵精四哈  阅读(183)  评论(0编辑  收藏  举报