ATM用面向对象的方式表现出来

这是功能实现部分。

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;

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属性值
System.out.println("你的账户当前余额为:"+this.user.getAccount());

}

// 存钱
private double storeMoney() {
//1、接收用户输入
//2、判断--不能为0,不能为负,不能不是100的倍数,不能超过存取上限,不能超过现金容量--给出提示
//3、判断通过--加现金,加余额
double sum=0; //用户存入的钱与余额的总和
while(true){
Scanner s=new Scanner(System.in);
System.out.println("请输入存的钱数:");
int cun=s.nextInt();
if(cun%100!=0){
System.out.println("存入的钱必须是一百的倍数,请重新输入:");continue;
}
else if(cun<=0){
System.out.println("您输入的钱数有误,请重新输入");continue;
}
else if(cun>this.GET_SET_MAX){
System.out.println("单次最多只能存入2000元,请重新输入");continue;
}
else{
System.out.println("存入成功,您的余额为"+(cun+this.user.getAccount()));
sum=cun+this.user.getAccount();
return sum;
}(这里有个问题,没有实现存钱之后余额变化)
}
}
// 取款
private void getMoney() {
while(true){
Scanner s=new Scanner(System.in);
System.out.println("请输入要取的钱数:");
int qu=s.nextInt();
if(this.user.getAccount()-qu<0){
System.out.println("您的账户余额不足,请重新输入");continue;
}
else if(qu%100!=0){
System.out.println("您输入有误,必须是100的倍数");continue;
}
else if(qu>this.GET_SET_MAX){
System.out.println("单次取款最多2000元,请重新输入");continue;
}
else{
System.out.println("取款成功,取款"+qu+"元");
System.out.println("账户当前余为额"+(this.user.getAccount()-qu));break;
}
}(这里也是上面的问题,没有实现取钱之后余额变化)
}
// 修改密码
private void changePwd() {
while(true){
Scanner s=new Scanner(System.in);
System.out.println("请输入原密码");
String psw=s.next();
if(psw.equals(this.user.getPassword())){
Scanner w=new Scanner(System.in);
System.out.println("请再次输入原密码");
String psw1=w.next();
if(psw1.equals(this.user.getPassword())){
System.out.println("请输入新密码");
Scanner x=new Scanner(System.in);
String xin=x.next();
System.out.print("密码修改成功");break;
}
else{
System.out.println("密码不一致,请重新输入");
}
}

else{
System.out.print("您输入的密码与原密码不一致请重新输入");
}
}
}

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

}

posted @ 2016-05-22 21:07  Blueses  阅读(452)  评论(0编辑  收藏  举报