Sha256: 795ba88913efc2487204536fcab97467f616342cf749f997ce60de32d1805f68

Contents?: true

Size: 1.54 KB

Versions: 193

Compression:

Stored size: 1.54 KB

Contents

class BankAccount {
    private int balance = 0;
    private boolean isClosed = true;

    void open() {
        isClosed = false;
    }

    void close() {
        isClosed = true;
    }

    synchronized int getBalance() throws BankAccountActionInvalidException {
        checkIfClosed();
        return balance;
    }

    synchronized void deposit(int amount) throws BankAccountActionInvalidException {
        checkIfClosed();
        checkIfValidAmount(amount);

        balance += amount;
    }

    synchronized void withdraw(int amount) throws BankAccountActionInvalidException {
        checkIfClosed();
        checkIfValidAmount(amount);
        checkIfEnoughMoneyInAccount(amount);

        balance -= amount;
    }

    private void checkIfValidAmount(int amount) throws BankAccountActionInvalidException {
        if (amount < 0) {
            throw new BankAccountActionInvalidException("Cannot deposit or withdraw negative amount");
        }
    }

    private void checkIfEnoughMoneyInAccount(int amount) throws BankAccountActionInvalidException {
        if (balance == 0) {
            throw new BankAccountActionInvalidException("Cannot withdraw money from an empty account");
        }
        if (balance - amount < 0) {
            throw new BankAccountActionInvalidException("Cannot withdraw more money than is currently in the account");
        }
    }

    private void checkIfClosed() throws BankAccountActionInvalidException {
        if (isClosed) {
            throw new BankAccountActionInvalidException("Account closed");
        }
    }
}

Version data entries

193 entries across 193 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.77 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.76 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.75 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.74 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.73 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.72 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.71 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.70 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.69 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.68 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.67 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.66 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.65 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.64 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.63 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.62 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.61 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.60 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.59 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java