少说废话,上代码:
关注小红旗: 我的博客是:http://blog.csdn.net/happylaoxu
代码如下:
package com.xuyaowen.bank;
import java.util.*;
//interface Service{
// boolean checkUser(String name, String passWord);
// boolean checkUser(int id, String passWord);
// void pushIn();
// void getOut();
// void showMoney();
//}
class User {
private String name;
public static int allNum;
private String passWord;
private double money;
private int id;
private Scanner scan = new Scanner(System.in);
User(){
System.out.println("现在还没有人使用机器!");
}
User(String name, String passWord, double money){
this.name = name;
User.allNum++;
this.id = allNum;
this.passWord = passWord;
this.money = money;
System.out.println(this.name + " 用户生成完毕!");
}
public boolean checkUser(String name, String passWord){
if((this.name.equals(name)) && (this.passWord.equals(passWord))){
System.out.println("欢迎您的使用");
return true;
}else{
return false;
}
}
public void pushIn(){
System.out.println("请输入你要存入的钱数:");
double tmp = scan.nextDouble();
if(tmp<0){
System.out.println("请输入正确的钱数,本次存入失败,请重新尝试!");
}else{
this.money += tmp;
System.out.println("存取成功");
showMoney();
}
}
public void getOut(){
System.out.println("请输入你要取的钱数:");
double tmp = scan.nextDouble();
if(tmp<0){
System.out.println("请输入正确的钱数,本次存入失败,请重新尝试!");
}
else if(this.money-tmp<0){
System.out.println("你的余额不足了不能取出此数目的金额");
System.out.println("本次取款失败!");
}else{
this.money -= tmp;
System.out.println("取款成功");
showMoney();
}
}
public String getName(){
return this.name;
}
public void showMoney(){
System.out.println("亲爱的"+this.name+"顾客,你好!");
System.out.println("你现在的存款是:"+this.money);
}
public String toString(){
String tmp = "ID " +this.id + " NAME " + this.name +" MONEY " + this.money;
return tmp;
}
}
class ATM{
private Scanner scan = new Scanner(System.in);
private User usingPeople = null;
private ArrayList<User> users = new ArrayList<User>();
ATM(){
System.out.println("ATM 机初始化!");
users.add(new User("root", "root" ,10000));
users.add(new User("xiaohongqi", "xiaohongqi", 10000));
users.add(new User("client" , "client", 10000));
}
private boolean login(){
System.out.println("----------------进入用户登陆的模式----------------");
System.out.println("请输入你的用户名:");
String tmpName = scan.nextLine();
System.out.println("请输入你的密码:");
String passWord = scan.nextLine();
int flag = 0;
System.out.println("正在检验!");
for(int i = 0; i<users.size(); i++){
if(users.get(i).checkUser(tmpName, passWord)){
usingPeople = users.get(i);
break;
}
flag++;
}
if(flag == users.size()){
System.out.println("对不起,你输入的密码和用户名错误,请重新输入!");
return false;
}else{
System.out.println("Welcome! 欢迎新顾客!");
return true;
}
}
private void register(){
System.out.println("----------------用户注册模式----------------");
System.out.println("请入你的姓名:");
String tmpName = scan.nextLine();
System.out.println("请输入你的密码:");
String tmpPassword = scan.nextLine();
System.out.print("作为新加入的用户,我们赠送你们的金钱为10000元 ");
users.add(new User(tmpName, tmpPassword, 10000));
System.out.println("注册成功");
}
private void showMenu(){
System.out.println("----------------菜单选择模式----------------");
System.out.println("1. 进行取款操作");
System.out.println("2. 进行存款操作");
System.out.println("3. 展示当前余额");
System.out.println("4. 退出");
System.out.println("----------------请您选择操作----------------");
}
private void welcomeMenu(){
System.out.println("----------------用户登录模式----------------");
System.out.println("1. 登陆");
System.out.println("2. 注册");
System.out.println("3. 退出");
System.out.println("----------------请您选择操作----------------");
}
public void run(){
System.out.println("ATM 机器正在运行,请进行操作");
while(true){
int count = 5;
boolean tmpBool = false;
int chioceOfWelcome = 0;
while((count--)!=0){
welcomeMenu();
try{
chioceOfWelcome = scan.nextInt();
scan.nextLine();
}
catch(Exception e){
System.out.println("你输入的不合法!");
scan.nextLine();
}
System.out.println("你选择的是: " + chioceOfWelcome);
switch(chioceOfWelcome){
case 1:
tmpBool = login();
break;
case 2:
register();
break;
case 3:
System.out.println("程序已经终止!");
System.exit(0);
break;
default:
System.out.println("你的输入错误请重新输入!");
break;
}
if(tmpBool){
break;
}
}
tmpBool = false;
//这里面有我做的bug;
int count2 =100;
while((count2--) != 0){
showMenu();
int chioceOfShowMenu = 0;
try{
try{
chioceOfShowMenu = scan.nextInt();
scan.nextLine();
}
catch(InputMismatchException e){
throw new RuntimeException(e);
}
}
catch(RuntimeException e){
System.out.println("您输入的不是合法的选项!请重新输入!");
scan.next();
}
switch(chioceOfShowMenu){
case 1:
usingPeople.getOut();
break;
case 2:
usingPeople.pushIn();
break;
case 3:
usingPeople.showMoney();
break;
case 4:
System.out.println("当前用户:"+ usingPeople.getName()+ "注销!");
usingPeople = null;
tmpBool = true;
break;
default:
break;
}
if(tmpBool){
break;
}
}
}
//System.out.println(users);
}
}
public class Bank {
static ATM a = new ATM();
public static void main(String[] args){
a.run();
}
}