Sha256: 847f9b57e7042d3268aadfe0b40a3a75801cc1c2e6d17df8eee7da74dcd374ad

Contents?: true

Size: 1.24 KB

Versions: 71

Compression:

Stored size: 1.24 KB

Contents

class BankAccount {
    boolean isOpen=false;
    int balance=0;

    // You cannot do any operations before you open the account.
    // An account opens with a balance of 0
    // You can reopen an account
    synchronized void open(){         
        isOpen=true;
        balance=0;
    } 

    // you cannot do any operations after you close the account
    synchronized void close() { 
        checkOpen();
        balance=0;
        isOpen=false;
    }

    // this should increment the balance
    // you cannot deposit into a closed account
    // you cannot deposit a negative amount 
    synchronized void deposit(int amount) {
        checkOpen();
        if(amount<=0)
            throw new Exception("Amount must be greater than 0")
        balance+=amount;        
    }

    synchronized void withdraw(int amount) {
        checkOpen();
        if(amount<=0)
            throw new Exception("Amount must be greater than 0")
        if(amount>balance)
            throw new Exception("You cannot withdraw more than your balance")
        balance -= amount;
    }
    synchronized int getBalance(){
        checkOpen();
        return balance
    }

    void checkOpen() {
        if(!isOpen)
            throw new Exception("Account must be open")
    }
}

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.179 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.178 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.177 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.176 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.175 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.174 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.173 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.172 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.171 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.170 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.169 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.167 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.166 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.165 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.164 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.163 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.162 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.161 tracks/groovy/exercises/bank-account/Example.groovy
trackler-2.2.1.160 tracks/groovy/exercises/bank-account/Example.groovy