Java 模拟ATM(修正)

时间:2021-06-23 14:41:32

  ATM机的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,操作日期(Date),操作类型,账户密码(六位的数字,可以用0开头),当前的余额(可以为0)。 模拟ATM的功能设计,用户插卡后显示选择语言界面,输入密码界面,用户输入正确密码(用户输入错误密码,则提示该卡已被锁定,无法操作),则弹出选择界面:存款、取款、转账汇款、修改密码、查询余额。 选择“取款”,则显示100元、500元、1000元、1500元、2000元、5000元、其他金额、退卡、返回操作供用户选择; 选择“存款”,则提示用户输入存款金额,修改余额; 选择“转账”,则提示用户输入转账行号、转账金额,并提示转账成功。

  要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。 自定义如下异常类,并在程序中验证: 取款时输入非整数时,抛出 “数字格式异常”,提示信息为“请输入正确的数字”; 如果输入数字非100的整倍数时,抛出“数字录入异常”,提示信息为“录入错误,请输出100的整数倍”; 三次密码输入错误,抛出“锁卡异常”,提示信息自定义。

 import java.util.*;
public class ShowATM {
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws NumberErrorException {
Scanner in=new Scanner(System.in);
PATM atm=new ATM();
NumberErrorException nee=new NumberErrorException("输入非法!");
NotHundredTimesException nhte=new NotHundredTimesException("不是一百的整数倍!");
PasswordErrorException pee=new PasswordErrorException("输入错误的密码超过三次!");
int passwordErrorTimes=0;
int choose=-1,number=0,x=0;
String num="";
String pw="";
next:while(true){
System.out.println("是否进入账户(0否1是):");
int kk=-1;
String c=in.next();
if(whetherNum(c))
kk=Integer.parseInt(c);
if(kk==0) break;
else if(kk!=1){
System.out.println("输入错误!");
continue;
}
System.out.println("输入账户密码:");
pw=in.next();
if(atm.ifPass(pw)){
while(true){
showFace();
String cc=in.next();
if(whetherNum(cc))
choose=Integer.parseInt(cc);
switch(choose){
case 1:
System.out.println("输入存款金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
atm.save(number);
System.out.println("存款成功!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 2:
System.out.println("请选择:");
int a[]={100,500,1000,1500,2000,5000};
for(int i=0;i<a.length;i++)
System.out.println((i+1)+"."+a[i]+"元");
System.out.println("7.其他");
String bb="";
int ch=-1;
bb=in.next();
if(whetherNum(bb)){
ch=Integer.parseInt(bb);
}
if(ch>=1&&ch<=6){
if(atm.withdraw(a[ch-1]))
System.out.println("取款成功!");
else
System.out.println("余额不足!");
}
else if(ch==7){
System.out.println("请输入取款金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
if(atm.withdraw(number))
System.out.println("取款成功!");
else
System.out.println("余额不足!");
}
else
System.out.println("输入有误!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 3:
System.out.println("账户号:");
String s=in.next();
System.out.println("转账金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
if(atm.transfer(s, number))
System.out.println("转账成功!");
else
System.out.println("转账失败!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 4:
System.out.println("输入新的六位数密码:");
String p=in.next();
if(p.length()==6&&whetherNum(p))
atm.setPassword(p);
else{
System.out.println("不是六位或者不是数字!");
}
break;
case 5:
System.out.println("当前余额:"+atm.getRest()+"元");
break;
default:
continue next;
}
}
}
else{
System.out.println("密码错误!");
try{
passwordErrorTimes++;
if(passwordErrorTimes>3)
throw pee;
}
catch(PasswordErrorException e){
System.out.println(e.getMessage()+"账户暂时冻结!");
return ;
}
} }
}
//显示菜单方法
public static void showFace(){
System.out.println("********************");
System.out.println(" 1.存款:");
System.out.println(" 2.取款:");
System.out.println(" 3.转账汇款:");
System.out.println(" 4.修改密码:");
System.out.println(" 5.查询余额:");
System.out.println(" 6.退卡:");
System.out.println("********************");
System.out.println("请选择:");
}
//判断字符串是否为整数(0-999999)
public static boolean whetherNum(String s){
boolean flag=true;
if(s.length()>=7)
return false;
char arr[]=s.toCharArray();
for(int i=0;i<s.length();i++){
if(arr[i]<'0'||arr[i]>'9')
flag=false;
}
return flag;
}
} class PersonalAccount{
private String passWord="123456";//密码
@SuppressWarnings("unused")
private String number;//银行卡号
private int money=0;
public int getMoney(){return money;}//余额
public void setPw(String s){passWord=s;}//设置密码
public void addMoney(int x){money+=x;}//加钱
public void minusMoney(int x){money-=x;}//减钱
public boolean whetherPwTrue(String s){//密码是否正确
if(s.equals(passWord))
return true;
else return false;
}
}
abstract class PATM{
public abstract boolean withdraw(int x);//取款
public abstract void save(int x);//存款
public abstract boolean transfer(String s,int x);//转账
public abstract boolean ifPass(String s);//判断输入的密码是否正确
public abstract int getRest();//查询余额
public abstract void setPassword(String s);//设置密码
}
class ATM extends PATM{
private String numbers[]={"6227000000000000071","6227000000000000072",
"6227000000000000073","6227000000000000074"};//数据库中已有的账户卡号
private PersonalAccount account=new PersonalAccount();
public boolean withdraw(int x) {
if(x>account.getMoney())
return false;
else{
account.minusMoney(x);
return true;
}
}
public void save(int x) {
account.addMoney(x);
}
public boolean transfer(String s, int x) {
//转账
//先判断转到账户号是否存在
//再判断余额是否足够
boolean flag=false;
for(int i=0;i<numbers.length;i++)
if(s.equals(numbers[i])) flag=true;
if(x>account.getMoney()) flag=false;
if(x<=account.getMoney()&&flag) account.minusMoney(x);;
return flag;
}
public boolean ifPass(String s) {
return account.whetherPwTrue(s);
}
public int getRest() {
return account.getMoney();
}
public void setPassword(String s) {
account.setPw(s);
}
}
@SuppressWarnings("serial")
class NumberErrorException extends Exception{
public NumberErrorException(String msg){
super(msg);
}
}
@SuppressWarnings("serial")
class PasswordErrorException extends Exception{
public PasswordErrorException(String msg){
super(msg);
}
}
@SuppressWarnings("serial")
class NotHundredTimesException extends Exception{
public NotHundredTimesException(String msg){
super(msg);
}
}

Java 模拟ATM(修正)

Java 模拟ATM(修正)

Java 模拟ATM(修正)

Java 模拟ATM(修正)

Java 模拟ATM(修正)