方法_记录
/*
把ATM用方法的方式写
*/
import java.util.*;
public class MethodATM{
public static String U_ACCOUNT = "xiaohuan";
public static String U_PWD = "123";
static Scanner sc = new Scanner(System.in);
static int money = 3000;
static boolean exitOS = false;
public static void main(String[] args){
bankOS();
}
//登录验证
public static boolean login(){
System.out.println("*************ATM系统**************");
System.out.print("请输入您的帐号:");
String account = sc.nextLine();
System.out.print("请输入您的密码:");
String pwd = sc.nextLine();
if(account.equals(U_ACCOUNT)&& pwd.equals(U_PWD)){
return true;
}else{
return false;
}
}
//验证锁卡与进入系统
public static void bankOS(){
int count = 0;
while(true){
if(login()){
bankUI();
if(exitOS){
break;
}
}else{
count++;
if(count>=3){
System.out.println("帐号或密码错误3次,此卡已被锁定");
break;
}
System.out.println("密码输入错误,请重新输入!");
}
}
}
public static void saveMoney(){
int saveMoney = 0;
System.out.println("********存款*********");
while(saveMoney>=0){
System.out.print("请输入您要存入的金额:");
saveMoney = sc.nextInt();
if(saveMoney<0){
System.out.println("〓〓〓〓〓〓〓〓\n存款金额有误!\n〓〓〓〓〓〓〓〓");
saveMoney = 0;
continue;
}else{
money += saveMoney;
System.out.println("存款成功!返回主界面"); break;
}
}
}
public static void getMoney(){
int getMoney = 0;
System.out.println("********取款*********");
while(getMoney>=0){
System.out.print("请输入您要取出的金额:");
getMoney = sc.nextInt();
if(getMoney>money||getMoney<0){
System.out.println("〓〓〓〓〓〓〓〓\n存款金额有误!\n〓〓〓〓〓〓〓〓");
getMoney = 0;
continue;
}else{
money -= getMoney;
System.out.println("取款成功!返回主界面");
break;
}
}
}
//系统界面
public static void bankUI(){
while(true){
System.out.println("********主界面*********");
System.out.println("\t1.存款\n\t2.取款\n\t3.查询\n\t4.退出");
System.out.println("***********************");
System.out.print("请输入您的操作:");
int num = sc.nextInt();
if(num==1){
saveMoney();
continue;
}else if(num==2){
getMoney();
continue;
}else if(num==3){
System.out.println("※※※※※※※※※※");
System.out.println("您的余额为:"+money);
System.out.println("※※※※※※※※※※");
System.out.println("返回主界面");
}else if(num==4){
exitOS = true;
break;
}else{
System.out.println("没有此操作!");
continue;
}
}
}
}