001
002 /**
003 * Konto version A.
004 * Uses synchronized methods to display, add and remove money from the account.
005 * @author Heinz Kredel.
006 */
007 public class KontoA implements Konto {
008
009 private double stand; // Konto Stand
010
011 public KontoA() { stand = 0.0; } //sets "kontostand" to a desired value
012
013 public KontoA(double a) { stand = a; }
014
015
016 /**
017 * Displays account balance.
018 * @return stand.
019 */
020 public synchronized double zeigeGeld() { return stand; }
021
022
023 /**
024 * @param b adds desired value to current account.
025 * @return s after booking (a call to "WorkA" is done,
026 * where current thread sleeps for a random time).
027 * current account balance is given back.
028 */
029 public synchronized double putGeld(double b) { // stand += b
030 double s = stand + b;
031 WorkA.schaffen(100);
032 stand = s;
033 return s;
034 }
035
036
037 /**
038 * @param b draws desired value from the account.
039 * @return s gives back current account balance.
040 */
041 public synchronized double getGeld(double b) { // stand -= b
042 double s = stand - b;
043 WorkA.schaffen(100);
044 stand = s;
045 return s;
046 }
047
048 }
049
050
051 /**
052 * Sending current thread to sleep for a random time.
053 */
054 class WorkA {
055
056 /**
057 * @param max defines range of numbers for Math.random().
058 */
059 public static void schaffen(int max) {
060 try {
061 Thread.currentThread().sleep( (int)(max*Math.random()) );
062 } catch (InterruptedException e) {
063 System.out.println("Dies sollte nicht passieren "+e);
064 }
065 }
066 }