1 package ch.hslu.exercises.sw08.ex3;
2
3 import java.util.concurrent.atomic.AtomicInteger;
4
5 public final class BankAccount {
6
7 private AtomicInteger balance;
8
9 /**
10 * Erzeugt ein Bankkonto mit einem Anfangssaldo.
11 *
12 * @param balance Anfangssaldo
13 */
14 public BankAccount(final int balance) {
15 this.balance = new AtomicInteger(balance);
16 }
17
18 /**
19 * Erzeugt ein Bankkonto mit Kontostand Null.
20 */
21 public BankAccount() {
22 this(0);
23 }
24
25 /**
26 * Gibt den aktuellen Kontostand zurück.
27 *
28 * @return Kontostand.
29 */
30 public int getBalance() {
31 return this.balance.get();
32 }
33
34 /**
35 * Addiert zum bestehen Kontostand einen Betrag hinzu.
36 *
37 * @param amount Einzuzahlender Betrag
38 */
39 public void deposite(final int amount) {
40 this.balance.addAndGet(amount);
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 this.balance.addAndGet(-amount);
51 target.deposite(amount);
52 }
53 }