1 package ch.hslu.exercises.sw05.input.ex2;
2
3 public final class BankAccount {
4
5 private int balance;
6
7 /**
8 * Erzeugt ein Bankkonto mit einem Anfangssaldo.
9 *
10 * @param balance Anfangssaldo
11 */
12 public BankAccount(final int balance) {
13 this.balance = balance;
14 }
15
16 /**
17 * Erzeugt ein Bankkonto mit Kontostand Null.
18 */
19 public BankAccount() {
20 this(0);
21 }
22
23 /**
24 * Gibt den aktuellen Kontostand zurück.
25 *
26 * @return Kontostand.
27 */
28 public int getBalance() {
29 return this.balance;
30 }
31
32 /**
33 * Addiert zum bestehen Kontostand einen Betrag hinzu.
34 *
35 * @param amount Einzuzahlender Betrag
36 */
37 public void deposite(final int amount) {
38 synchronized (this) {
39 this.balance += amount;
40 }
41 }
42
43 /**
44 * Überweist einen Betrag vom aktuellen Bankkonto an ein Ziel-Bankkonto.
45 *
46 * @param target Bankkonto auf welches der Betrag überwiesen wird.
47 * @param amount zu überweisender Betrag.
48 */
49 public void transfer(final BankAccount target, final int amount) {
50 synchronized (this) {
51 this.balance -= amount;
52 }
53 target.deposite(amount);
54 }
55 }