培训生涯:atm

Paradise丶GeMinI 2016-04-16 10:29:12

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("********************************************");
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();

}

}
...全文
76 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧