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.180 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.179 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.178 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.177 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.176 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.175 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.174 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.173 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.172 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.171 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.170 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.169 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.167 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.166 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.165 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.164 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.163 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.162 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.161 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.160 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java