银行账户(设计模式--状态模式)

    题目描述:某银行系统中定义了账户的三种状态:

(1)如果账户(Account)余额不小于0,账户状态为绿色,即正常状态,既可以存,也可以取款;

(2)如果余额小于0,并且大于等于-1000,账户状态为蓝色,即欠费状态,既可以存,也可以取款;

(3)如果账户中余额小于-1000,账户状态为红色,即透支状态,只能存款,不能取款。

    实现截图:

                                    银行账户(设计模式--状态模式)_第1张图片

    UML图:

 

 

                              银行账户(设计模式--状态模式)_第2张图片

    ATM.java

package bank_state_5;

public class ATM {
    /*所有状态*/
    private ATMState readyState;
    private ATMState noCashState;
    private ATMState noServiceState;

    private ATMState currState;//当前状态
    private int totalAmount;//账户现钞总数

    /*测试用的临时变量*/
    private String pwd;//密码
    private int balance;//余额
    private int amount;//取款金额

    public ATM(int totalAmount, int balance, int amount, String pwd) throws Exception{
        //初始化所有状态
        readyState = new ReadyState(this);
        noCashState = new NoCashState(this);
        noServiceState = new NoServiceState(this);

        if(totalAmount > 0){
            currState = readyState;
        }
        else if(totalAmount == 0){
            currState = noCashState;
        }
        else{
            throw new Exception();
        }

        //初始化测试数据
        this.totalAmount = totalAmount;
        this.balance = balance;
        this.amount = amount;
        this.pwd = pwd;
    }

    /*把具体行为委托给状态对象*/
    /**
     * 插卡
     */
    public void insertCard(){
        currState.insertCard();
    }

    /**
     * 提交密码
     */
    public void submitPwd(){
        currState.submitPwd();
    }

    /**
     * 取款
     */
    public void getCash(){
        currState.getCash();
    }

    /**
     * 查询余额
     */
    public void queryBalance(){
        currState.queryBalance();
    }

    /**
     * 取卡
     */
    public void ejectCard(){
        currState.ejectCard();
    }
    public String toString(){
        return "账户总额¥" + totalAmount;
    }
    public String SKT() {
        if(totalAmount >= 0) {
            return  "账户状态为绿色正常状态,既可以存,也可以取款";
        }
        if(totalAmount >= -1000 && totalAmount < 0) {
            return "账户状态为蓝色欠费状态,既可以存,也可以取款";
        }
        if(totalAmount < -1000) {
            return "账户状态为红色透支状态,只能存款";
        }
        return "";
    }

    /*此处略去大量getter and setter*/

    public String getPwd(){
        return pwd;
    }
    public int getBalance() {
        return balance;
    }
    public int getTotalAmount() {
        return totalAmount;
    }
    public int getAmount() {
        return amount;
    }

    public void setTotalAmount(int totalAmount) {
        this.totalAmount = totalAmount;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public ATMState getNoCashState() {
        return noCashState;
    }

    public void setCurrState(ATMState currState) {
        this.currState = currState;
    }
}

     ATMState.java

package bank_state_5;

public interface ATMState {
    public abstract void insertCard();
    public abstract void submitPwd();
    public abstract void getCash();
    public abstract void queryBalance();
    public abstract void ejectCard();
}

     NoCashState.java

package bank_state_5;

public class NoCashState implements ATMState{
    private ATM atm;//保留状态持有者的引用,以便对其进行操作

    public NoCashState(ATM atm){
        this.atm = atm;
    }

    @Override
    public void insertCard() {
        System.out.println("插卡完成");
    }

    @Override
    public void submitPwd() {
        System.out.println("密码提交完成");
        //验证密码并做相应处理
        if("123".equals(atm.getPwd())){
            System.out.println("密码验证通过");
        }
        else{
            System.out.println("密码验证失败");
            //弹出卡片
            ejectCard();
        }
    }

    @Override
    public void getCash() {
        System.out.println("取款失败,机内无钞");
    }

    @Override
    public void queryBalance() {
        System.out.println("账户余额¥" + atm.getBalance());
        System.out.println("账户余额查询完成");
    }

    @Override
    public void ejectCard() {
        System.out.println("取卡完成");
    }
}

    NoServiceState.java

package bank_state_5;

public class NoServiceState implements ATMState{
    private ATM atm;//保留状态持有者的引用,以便对其进行操作

    public NoServiceState(ATM atm){
        this.atm = atm;
    }

    @Override
    public void insertCard() {
        System.out.println("插卡失败,机器发生了故障");
    }

    @Override
    public void submitPwd() {
        System.out.println("密码提交失败,机器发生了故障");
    }

    @Override
    public void getCash() {
        System.out.println("取款失败,机器发生了故障");
    }

    @Override
    public void queryBalance() {
        System.out.println("余额查询失败,机器发生了故障");
    }

    @Override
    public void ejectCard() {
        System.out.println("取卡失败,机器发生了故障");
    }
}

    ReadyState.java

package bank_state_5;

public class ReadyState implements ATMState{
    private ATM atm;//保留状态持有者的引用,以便对其进行操作
    public ReadyState(ATM atm){
        this.atm = atm;
    }

    @Override
    public void insertCard() {
        System.out.println("插卡完成");
    }
    @Override
    public void submitPwd() {
        System.out.println("密码提交完成");
        //验证密码并做相应处理
        if("6300".equals(atm.getPwd())){
            System.out.println("密码验证通过");
        }
        else{
            System.out.println("密码验证失败");
            //弹出卡片
            ejectCard();
        }
    }

    @Override
    public void getCash() {
        //atm.getTotalAmount() >= atm.getAmount() && atm.getBalance() >= atm.getAmount()
        if(atm.getTotalAmount() != 9999){
            //更新账户余额
            atm.setBalance(atm.getBalance() - atm.getAmount());
            //更新机内现钞总数
            atm.setTotalAmount(atm.getTotalAmount() - atm.getAmount());
            System.out.println("吐出¥" + atm.getAmount());
            System.out.println("取款完成");
            //弹出卡片
            ejectCard();
            //检查机内余钞
            if(atm.getTotalAmount() == 0){//若无钞,切换到NoService状态
                atm.setCurrState(atm.getNoCashState());
                System.out.println("无钞信息已经发送至银行");
            }
        }
        else{
            System.out.println("取款失败,余额不足");
            //弹出卡片
            ejectCard();
        }
    }

    @Override
    public void queryBalance() {
        System.out.println("余额¥" + atm.getBalance());
        System.out.println("余额查询完成");
    }

    @Override
    public void ejectCard() {
        System.out.println("取卡完成");
    }
}

    test.java

package bank_state_5;

public class shijian5_yinhangzhanghu {
    public static void main(String[] args) {
        /*测试数据*/
        /* 机内总数  账户余额    取款金额   密码
         * 1000 500     200     123
         * 1000 300     500     123
         * 0    500     200     123
         * */
        try {
            test(1000, 500, 200, "6300");
            System.out.println("------------------------------------------------------------");
            test(1000, 300, 1500, "6300");
            System.out.println("------------------------------------------------------------");
            test(-1500, 500, 200, "6300");
        } catch (Exception e) {
            System.out.println("账户余额小于-1000,账户状态为红色透支状态,请存钱后再取");
            //System.out.println("机器故障,维修请求已经发送至维修方");
        }
    }

    private static void test(int totalAmount, int balance, int amount, String pwd)throws Exception{
        //创建ATM
        ATM atm;
        atm = new ATM(totalAmount, balance, amount, pwd);
        //输出初始状态
        System.out.println(atm.toString());
        System.out.println(atm.SKT());
        atm.insertCard();
        atm.submitPwd();
        atm.getCash();
        //输出结束状态
        System.out.println(atm.toString());
    }
}

 

你可能感兴趣的:(银行账户(设计模式--状态模式))