Bank account

in ita •  7 months ago 

import groovy.transform.Synchronized
class BankAccount {

private int balance 
boolean isOpen


// 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() {
if(isOpen) throw new UnsupportedOperationException()
balance = 0
isOpen = true
}

// you cannot do any operations after you close the account
@Synchronized
void close() {
    if(!isOpen) throw new UnsupportedOperationException()
    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) {
    if(!isOpen) throw new UnsupportedOperationException()
    if(amount < 0) throw new IllegalArgumentException()
    balance += amount
}


// this should decrement the balance
// you cannot withdraw into a closed account
// you cannot withdraw a negative amount 
@Synchronized
void withdraw(int amount) {
    if(!isOpen) throw new UnsupportedOperationException()
    if(amount < 0 || amount > balance) throw new IllegalArgumentException()
    balance -= amount
}

// returns the current balance
@Synchronized
int getBalance() {
    if(!isOpen) throw new UnsupportedOperationException()
    return balance
}

}

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.