diff options
Diffstat (limited to 'Week13 Bar/src/com/camilstaps/bar/ParchedMan.java')
-rw-r--r-- | Week13 Bar/src/com/camilstaps/bar/ParchedMan.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Week13 Bar/src/com/camilstaps/bar/ParchedMan.java b/Week13 Bar/src/com/camilstaps/bar/ParchedMan.java new file mode 100644 index 0000000..6821130 --- /dev/null +++ b/Week13 Bar/src/com/camilstaps/bar/ParchedMan.java @@ -0,0 +1,84 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps <info@camilstaps.nl> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.camilstaps.bar; + +/** + * A parched man wants to drink + * @author camilstaps + */ +public class ParchedMan implements Runnable { + + private static final int DRINK_TIME_PER_CC = 5; + private static final int NUMBER_OF_GLASSES_TO_DRINK = 5; + private static final int DIGEST_TIME = 10; + + private final int id; + private int numberOfGlassesDrunk = 0; + private final Bar bar; + private final Tap tap; + + public ParchedMan(int id, Bar bar, Tap tap) { + this.id = id; + this.bar = bar; + this.tap = tap; + } + + @Override + public void run() { + drawAndDrink(); + } + + /** + * While not satisfied, take a glass if possible, and drink + */ + public void drawAndDrink() { + while (!isSatisfied()) { + Glass glass = bar.getGlass(); + if (glass != null) { + glass = tap.fillUp(glass); + try { + System.out.println("Man " + id + " drinks one glass (" + glass.toString() + ")"); + Thread.sleep(glass.getVolume() * DRINK_TIME_PER_CC); + } catch (InterruptedException e) {} + glass.empty(); + numberOfGlassesDrunk++; + System.err.println("Man " + id + " puts glass back (" + glass.toString() + ")"); + bar.putGlass(glass); + try { + Thread.sleep(DIGEST_TIME); + } catch (InterruptedException e) {} + } + } + System.out.println("Man " + id + " satisfied"); + } + + /** + * Check if the man is satisfied + * @return + */ + public boolean isSatisfied() { + return numberOfGlassesDrunk >= NUMBER_OF_GLASSES_TO_DRINK; + } + +} |