ATM

             

import java.util.*;
class Account{
    private String str;
    private String name;
    private String date;
    private int caozuo;
    private String passwd;
    private double money;
    public Account(String str,String name,String passwd){
        this.str=str;
        this.name=name;
        this.passwd=passwd;
        money=0;
    }
    public String get_str(){
        return str;
    }
    public String get_name(){
        return name;
    }
    public String get_date(){
        return date;
    }
    public int get_caozuo(){
        return caozuo;
    }
    public boolean in_passwd(String passwd){
        if(this.passwd.equals(passwd)){
            return true;
        }
        else 
            return false;
    }
    public double get_money(){
        return money;
    }
    public void set_money(double money){
        this.money=money;
    }
    public void set_date(String date){
        this.date=date;
    }
    public void set_caozuo(int caozuo){
        this.caozuo=caozuo;
    }
    public void set_passwd(String passwd){
        this.passwd=passwd;
    }
    public void add_money(double money){
        this.money+=money;
    }
    public void jian_money(int n){

        if(n==1){
            money-=100;
        }
        else if(n==2){
            money-=500;
        }
        else if(n==3){
            money-=1000;
        }
        else if(n==4){
            money-=1500;
        }
        else if(n==5){
            money-=2000;
        }
        else if(n==6){
            money-=5000;
        }
    }
}
//错误定义
class ATMException extends Exception{
    public ATMException(String pad){
        super(pad);
    }
}
public class ttt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String p1="000000";String p2="111111";
        Account a1=new Account("12345678901","Tom",p1);
        Account a2=new Account("12345678902","Tom",p2);
        Scanner input =new Scanner(System.in);
        String p;
        char pc;
        int pot=0;
        while(true){
            System.out.println("输入密码:");
            p=input.next();
            try{
                for(int i=0;i<p.length();i++){
                    pc=p.charAt(i);
                    if(pc<'0'||pc>'9'){
                        ATMException e=new ATMException("密码出现数字以外的错误字符,输入失败。");
                        throw e;
                    }
                }
            }
            catch(ATMException e){
                System.out.println(e);
            }
            if(a1.in_passwd(p)){
                break;
            }
            System.out.println("密码出错");
            try{
                if(++pot>=3){
                    ATMException e=new ATMException("密码错误超过三次。");
                    throw e;
                };
            }
            catch(ATMException e){
                System.out.println(e);
                return ;
            }
        }
        while(true){
            System.out.println("1.存款"+"
"+"2.取款"+"
"+"3.转账汇款"+"
"+"4.修改密码"+"
"+"5.查询余额"+"
"+"6.退卡");
            //选项出现字符(“这个使用的是非自定义错误”)
            try{
                int m=input.nextInt();
                if(m==1){
                    System.out.println("输入存款金额:");
                    double money=input.nextDouble();
                    try{
                        if(money%100!=0){
                        ATMException e=new ATMException("存款金额不是100整数倍,存款出错。");
                        throw e;
                        }
                        a1.add_money(money);
                        System.out.println(a1.get_str()+" "+a1.get_name()+" 存款"+money+" 现金"+a1.get_money());
                    }
                    catch(ATMException e){
                        System.out.println(e);
                    }
                    
                }
                else if(m==2){
                    System.out.println("1.100  2.500  3.1000  4.1500  5.2000  6.5000");
                    try{
                        int mon=input.nextInt();
                        try{
                            if(a1.get_money()<mon){
                            ATMException e=new ATMException("余额不足,取钱失败。");
                            throw e;
                            }
                            a1.jian_money(mon);
                            System.out.println(a1.get_str()+" "+a1.get_name()+" 取款"+" 现金"+a1.get_money());
                        }
                        catch(ATMException e){
                            System.out.println(e);
                        }
                    }
                    catch(InputMismatchException e){
                        System.out.println("输入有错,不是数字");
                    }
                    //余额不足
                    
                    
                }
                else if(m==3){
                    System.out.println("输入转账卡号:");
                    String id=input.next();
                    if(id.equals(a2.get_str())){
                        System.out.println("输入转账金额:");
                        double money=input.nextDouble();
                        //余额不足,转账失败
                        try{
                            if(a1.get_money()<money){
                            ATMException e=new ATMException("余额不足,转账失败。");
                            throw e;
                            }
                            a2.add_money(money);
                            a1.add_money(-money);
                            System.out.println(a1.get_str()+" "+a1.get_name()+" 取款"+money+" 现金"+a1.get_money());
                            System.out.println(a2.get_str()+" "+a2.get_name()+" 存款"+money+" 现金"+a2.get_money());
                        }
                        catch(ATMException e){
                            System.out.println(e);
                        }
                        
                        
                    }
                    else{
                        System.out.println("转账失败。");
                    }
                }
                else if(m==4){
                    System.out.println("输入密码:");
                    p=input.next();
                    //密码出现字符
                    try{
                        for(int i=0;i<p.length();i++){
                            pc=p.charAt(i);
                            if(pc<'0'||pc>'9'){
                                ATMException e=new ATMException("密码出现数字以外的错误字符,输入失败。");
                                throw e;
                            }
                        }
                        a1.set_passwd(p);
                        System.out.println(a1.get_str()+" "+a1.get_name()+" 修改密码");
                    }
                    catch(ATMException e){
                        System.out.println(e);
                    }
                    
                }
                else if(m==5){
                    System.out.println(a1.get_str()+" "+a1.get_name()+" 查询金额 "+" 现金"+a1.get_money());
                }
                else if(m==6){
                    break;
                }
                else{
                    System.out.println("输入有误");
                }
            }
            catch(InputMismatchException e){
                System.out.println("输入有错,不是数字");
            }
        }    
        input.close();    
    }

}
原文地址:https://www.cnblogs.com/xhxdeblog/p/6101126.html