Simple Bank System

模拟一个银行系统,存钱取钱.

主要是判断一下各种可能情况.

class Bank {

    private long[] b;
    private int n;
    public Bank(long[] b) {
        this.n = b.length;
        this.b = new long[n + 1];
        for(int i = 0; i < n; i++){
            this.b[i+1] = b[i];
        }
    }
    private boolean exist(int a){
        return 1 <= a && a <= n;
    }
    public boolean transfer(int a1, int a2, long m) {
        if(exist(a1) && exist(a2) && b[a1] >= m){
            b[a1] -= m;
            b[a2] += m;
            return true;
        }
        else
            return false;
    }
    
    public boolean deposit(int a, long m) {
        if(!exist(a))
            return false;
        b[a] += m;
        return true;
    }
    
    public boolean withdraw(int a, long m) {
        if(!exist(a))
            return false;
        if(b[a] < m)
            return false;
        b[a] -= m;
        return true;
    }
}

/**
 * Your Bank object will be instantiated and called as such:
 * Bank obj = new Bank(balance);
 * boolean param_1 = obj.transfer(account1,account2,money);
 * boolean param_2 = obj.deposit(account,money);
 * boolean param_3 = obj.withdraw(account,money);
 */