ATM深化(1)
package com.lovo.bean;
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 void storeMoney() {
//1、接收用户输入
//2、判断--不能为0,不能为负,不能不是100的倍数,不能超过存取上限,不能超过现金容量--给出提示
//3、判断通过--加现金,加余额
Scanner s=new Scanner(System.in);
//存款的金额
System.out.println("请您放入要存款的金额");
int h=s.nextInt();
if(h>GET_SET_MAX){
System.out.println("您放入要存款的金额过大,每次最大存款金额不能大于2000");
return ;
}
if(h+this.cash>MAX_CASH){
System.out.println("您放入要存款的金额过大");
return;
}
if(h==0||h%100!=0){
System.out.println("您放入要存款的金额不正确,存款金额必须大于或等于100且必须为100的倍数");
}
else{
System.out.println("您的现有余额:"+(this.user.getAccount()+h));
float A=this.user.getAccount()+h;
this.user.setAccount(A);
}
}
// 取款
private void getMoney() {
Scanner s=new Scanner(System.in);
//取款金额
int r=0;
System.out.println("请输入要取出的金额");
r=s.nextInt();
if(r==0||r%100!=0||r<0){
System.out.println("取出金额必须大于或等于100,且必须为100的倍数");
}
else if(r>this.user.getAccount()){
System.out.println("您的余额不足");
}
else if(r>this.cash){
System.out.println("你输入的取款金额过大");
}
else if(r>GET_SET_MAX){
System.out.println("你输入的取款金额过大,每次取款金额不能大于2000");
}
else{
System.out.println("您的现有余额:"+(this.user.getAccount()-r));
float B=this.user.getAccount()+r;
this.user.setAccount(B);
}
}
// 修改密码
private void changePwd() {
/*Scanner s=new Scanner(System.in);
String oldPassword = "";
String newPassword1 = "";
String newPassword2 = "";
while(true){
System.out.println("请输入原密码:");
oldPassword= s.next();
if (oldPassword.equals(this.user.getPassword())) {
break;
}
else {
System.out.println("密码错误,请重新输入");
}
}
while(true){
System.out.println("请输入新密码");
newPassword1 = s.next();
System.out.println("确认输入新密码");
newPassword2 = s.next();
if (newPassword1.equals(newPassword2)) {
if (!xiangtong(newPassword1)) {
oldPassword=newPassword1;
System.out.println("修改成功");
this.user.setPassword(newPassword1);
System.out.println("账户密码为:"+this.user.getPassword());
break;
}
}
else {
System.out.println("两次输入不一致,重新输入");
}
}
}
//两次密码相同
private boolean xiangtong(String string){
boolean bool = false;
for (int i = 0; i < string.length() - 1; i++) {
char char1 = string.charAt(i);
for (int j = i + 1; j < string.length(); j++) {
char char2 = string.charAt(j);
if (char1 == char2) {
bool = true;
break;
}
}
}
return bool; */
Scanner s=new Scanner(System.in);
System.out.println("请输入原密码:");
String oldPassword= s.next();
if (oldPassword.equals(this.user.getPassword())) {
System.out.println("请输入新密码");
String newPassword1 = s.next();
System.out.println("确认输入新密码");
String newPassword2 = s.next();
if(newPassword1.equals(newPassword2)){
this.user.setPassword(newPassword1);
System.out.println("账户密码为:"+this.user.getPassword());
}
}
else {
System.out.println("密码错误,请重新输入");
return;}
//System.out.println("请输入原密码:");
//Scanner s=new Scanner(System.in);
//String oldPassword= s.next();
//if(oldPassword.equals(this.user.getPassword())){
}
// 退出
private void exit() {
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}
}