方法后续

方法后续_第1张图片



在这里弄上一段对上述知识点综合比较强的代码,ATM取款机的模拟程序



package test;


import java.util.Scanner;


public class ATMMachine {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 初始化用户数据
String username = "J129";
String password = "123456";
float account = 10000.0f;


// 画出欢迎界面
welcome();
// 实现登录
boolean flag = login(username,password);
if (flag) {
// 选择菜单
while (true) {
int choice = chooseMenu();
switch (choice) {
case 1:
query(account);
break;
case 2:
account = storeMoney(account);
break;
case 3:
account = getMoney(account);
break;
case 4:
password = changePwd(password);
break;
case 5:
System.out.println("退出");
// System.exit(0);//关闭虚拟机
return;
default:
System.out.println("没有该选项");
break;
}
}
} else {
System.out.println("三次机会用完,卡被没收,请到柜台处理!");
}


}

public static void welcome(){
System.out.println("*******************************************");
System.out.println("*   欢迎来到我的银行                                                             *");
System.out.println("*                                         *");
System.out.println("*            也许我们不是最好的银行                      *");
System.out.println("*                                         *");
System.out.println("*                     但至少老子有银行!        *");
System.out.println("*******************************************");
}

public static boolean login(String username, String password){
Scanner scan = new Scanner(System.in);
boolean flag = false;
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String inputName = scan.next();
System.out.println("请输入密   码:");
String inputPwd = scan.next();


if (inputName.equals(username) && inputPwd.equals(password)) {
System.out.println("登录成功!");
flag = true;
break;
} else {
System.out.println("用户名,密码有误!你还有" + (2 - i) + "次机会!");
}


}

return flag;
}

public static int chooseMenu(){
Scanner scan = new Scanner(System.in);
System.out.println("请选择你要执行的操作:");
System.out.println("1、查询余额;2、存款;3、取款;4、修改密码;5、退出");
int choice = scan.nextInt();
return choice;
}

public static void query(float account){
System.out.println("您当前的余额是:" + account + "元。");
}

public static float storeMoney(float account){
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("请输入您要存款的金额:");
int inputMoney = scan.nextInt();
if(inputMoney <= 0){
System.out.println("您见过哪家银行存这种数目?");
}else if(inputMoney % 100 != 0){
System.out.println("本机只接收100元的整钱!");
}else{
account += inputMoney;
System.out.println("存款成功!");
break;
}
System.out.println("是否继续存钱?(y/n)");
String c = scan.next();
if(c.equals("n") || c.equals("N")){
break;
}
}
return account;
}

public static float getMoney(float account){
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("请输入您要取款的金额:");
int inputMoney = scan.nextInt();
if(inputMoney <= 0){
System.out.println("你还想越取越多?贱人!");
}else if(inputMoney % 100 != 0){
System.out.println("本机只发放100元的整钱!");
}else if(inputMoney >= account){
System.out.println("当前余额不足,请减少取款数目!");
}else{
account -= inputMoney;
System.out.println("取款成功!");
break;
}
System.out.println("是否继续取钱?(y/n)");
String c = scan.next();
if(c.equals("n") || c.equals("N")){
break;
}
}
return account;
}

public static String changePwd(String password){
Scanner scan = new Scanner(System.in);
System.out.println("请输入旧密码:");
String oldPwd = scan.next();
if(!oldPwd.equals(password)){
System.out.println("旧密码输入有误!");
return password;
}else{
System.out.println("请输入新密码:");
String newPwd = scan.next();
System.out.println("再输入新密码:");
String reNewPwd = scan.next();
if(newPwd.equals(reNewPwd)){
System.out.println("密码修改成功!");
return newPwd;
}else{
System.out.println("新密码两次输入不一致!");
return password;
}
}
}


}

你可能感兴趣的:(Java)