Last active
August 29, 2015 14:21
-
-
Save egonelbre/fe5ab69bb4593d2e138c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Hashtable; | |
public class Bank { | |
public static class InsufficientFunds extends Exception {} | |
final public interface Account { | |
Integer balance(); | |
void increase (Integer amount); | |
void decrease (Integer amount); | |
} | |
final private class AccountImpl implements Account { | |
public Integer balance; | |
public AccountImpl(Integer initialBalance){ balance = initialBalance; } | |
public Integer balance() { return balance; } | |
public void increase (Integer amount) { balance += amount; } | |
public void decrease (Integer amount) { balance -= amount; } | |
} | |
private Hashtable<Integer, Bank.AccountImpl> accounts; | |
public Bank() { accounts = new Hashtable<Integer, Bank.AccountImpl>(); } | |
public Bank.Account accountByNumber(Integer number) { | |
return accounts.get(number); | |
} | |
public void addAccount(Integer number , Integer initialBalance) { | |
accounts.put(number, new Bank.AccountImpl(initialBalance)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
private Bank bank = new Bank(); | |
public static void main(String[] args) { | |
Test t = new Test(); | |
} | |
public Test () { | |
bank.addAccount(111, 1000); | |
bank.addAccount(222, 0); | |
System.out.println("Before:"); | |
System.out.println("#111 = " + bank.accountByNumber(111).balance().toString()); | |
System.out.println("#222 = " + bank.accountByNumber(222).balance().toString()); | |
try { | |
new Transfer(bank, 300, 111, 222); | |
} catch(Bank.InsufficientFunds ex) { | |
System.out.println("Not enough money..."); | |
} | |
System.out.println("After:"); | |
System.out.println("#111 = " + bank.accountByNumber(111).balance().toString()); | |
System.out.println("#222 = " + bank.accountByNumber(222).balance().toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Transfer { | |
public class InsufficientFunds extends Exception {} | |
public Transfer( | |
Bank bank, | |
Integer amt, | |
Integer from, | |
Integer to | |
) throws Transfer.InsufficientFunds { | |
Amount = amt; | |
Source = bank.accountByNumber(from); | |
Destination = bank.accountByNumber(to); | |
Source.transferFrom(); | |
} | |
role Amount extends Integer {} | |
role Source extends Bank.Account { | |
void transferFrom() throws Transfer.InsufficientFunds { | |
if(balance() < amount) { | |
throw new Transfer.InsufficientFunds(); | |
} | |
decrease(amount); | |
Destination.transferTo(); | |
} | |
} | |
role Destination extends Bank.Account { | |
void transferTo() { | |
increase(Amount); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment