ATM机
2016-04-17 17:08
253 查看
package com.lovo.bean; 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("J122", "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(){ System.out.println("请输入取款金额:"); Scanner scan = new Scanner(System.in); int inputMoney = scan.nextInt(); if(inputMoney <= 0){ System.out.println("取款金额不能为负数"); }else if(inputMoney % 100 != 0){ System.out.println("取款金额为100的整数!"); }else if(inputMoney >= this.theUser.getAccount()){ System.out.println("您账户的余额不足!"); }else if(inputMoney > this.cash){ System.out.println("本ATM现金不足!"); }else{ this.cash -= inputMoney; this.theUser.setAccount(this.theUser.getAccount() - inputMoney); System.out.println("取款成功!现在余额是:" + this.theUser.getAccount()); } } //存款 private void storeMoney(){ System.out.println("请输入存款金额"); Scanner scan=new Scanner(System.in); int inputMoney = scan.nextInt(); if(inputMoney <= 0){ System.out.println("存款金额不能为负数"); }else if(inputMoney % 100 != 0){ System.out.println("存款金额为100的整数!"); }else if(inputMoney+this.cash>=this.MAX_CASH){ System.out.println("对不起,本ATM现金已满!"); }else{ this.cash += inputMoney; this.theUser.setAccount(this.theUser.getAccount() +inputMoney); System.out.println("存款成功!现在余额是:" + this.theUser.getAccount()); } } //修改密码 private void changePwd(){ System.out.println("请输入您的旧密码"); Scanner scan=new Scanner(System.in); String inputpwd = scan.next(); if(inputpwd.equals(this.theUser.getPassword())){ System.out.println("请输入您的新密码"); String changepwd = scan.next(); System.out.println("请再输入输入您的新密码"); String changepwd1 = scan.next(); if(changepwd.equals(changepwd1)){ System.out.println("修改密码成功"); this.theUser.setPassword(changepwd); }else{ System.out.println("您的输入有误,修改密码失败"); } }else{ System.out.println("您的输入有误,无法修改"); } } //退出 private void exit(){ System.out.println("谢谢使用,期待下次光临!"); System.exit(0); } } package com.lovo.bean; //用户信息 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; } } package com.lovo.test; import com.lovo.bean.ATMMachine; public class TestMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ATMMachine atm = new ATMMachine(); atm.run(); } }
相关文章推荐
- 异常 SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
- 【汇编】进制转换程序
- 学习进度条7
- RunTime机制详解(一)
- 单一职责原则(SRP:Single responsibility principle)
- Python运算符
- opengl之vsh、fsh简易介绍+cocos2dx 3.0 shader 变灰
- Bitmap创建图片副本
- 在导入eclipse项目到Android Studio出现这样的错误, 非法字符: ‘\ufeff‘ 解决方案|错误: 需要class, interface或enum
- mysql监控
- 进程调度与进程切换的过程分析
- Caffe+Ubuntu14.04+CUDA7.5安装笔记
- PHP文件操作
- Android中的六大布局
- JVM中Java对象的创建
- uva 11400 Lighting System Design
- SQL绑定变量
- MBTI性格自测
- 设计模式C++学习笔记之一(Strategy策略模式)
- poj2823单调对列和poj3250单调栈