diff options
Diffstat (limited to 'Week13 Bar/src/com/camilstaps/bar')
-rw-r--r-- | Week13 Bar/src/com/camilstaps/bar/Bar.java | 70 | ||||
-rw-r--r-- | Week13 Bar/src/com/camilstaps/bar/Glass.java | 46 | ||||
-rw-r--r-- | Week13 Bar/src/com/camilstaps/bar/ParchedMan.java | 84 | ||||
-rw-r--r-- | Week13 Bar/src/com/camilstaps/bar/Tap.java | 50 | ||||
-rw-r--r-- | Week13 Bar/src/com/camilstaps/bar/Week13Bar.java | 41 |
5 files changed, 291 insertions, 0 deletions
diff --git a/Week13 Bar/src/com/camilstaps/bar/Bar.java b/Week13 Bar/src/com/camilstaps/bar/Bar.java new file mode 100644 index 0000000..800ffa7 --- /dev/null +++ b/Week13 Bar/src/com/camilstaps/bar/Bar.java @@ -0,0 +1,70 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.List; + +/** + * A bar + * @author Camil Staps + */ +public class Bar { + + private final Tap tap; + private final List<Glass> glasses; + private final List<ParchedMan> drinkers; + + public Bar (int numberOfGlasses) { + tap = new Tap(); + glasses = new ArrayList<>(); + for (int i = 0; i < numberOfGlasses; i++) { + glasses.add(new Glass()); + } + drinkers = new ArrayList<>(); + } + + public synchronized Glass getGlass() { + if (glasses.isEmpty()) + return null; + return glasses.remove(0); + } + + public void putGlass(Glass glass) { + glasses.add(glass); + } + + public void addDrinkers(int number) { + for (int i = 0; i < number; i++) { + drinkers.add(new ParchedMan(i, this, tap)); + } + } + + public void startDrinking() { + for (ParchedMan man : drinkers) { + (new Thread(man)).start(); + } + } + +} diff --git a/Week13 Bar/src/com/camilstaps/bar/Glass.java b/Week13 Bar/src/com/camilstaps/bar/Glass.java new file mode 100644 index 0000000..1102339 --- /dev/null +++ b/Week13 Bar/src/com/camilstaps/bar/Glass.java @@ -0,0 +1,46 @@ +/* + * 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 glass can be filled and emptied + * @author Camil Staps, s4498062 + */ +public class Glass { + + private int volume = 0; + + public int getVolume() { + return volume; + } + + public void fill(int cc) { + volume = cc; + } + + public void empty() { + volume = 0; + } + +} 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; + } + +} diff --git a/Week13 Bar/src/com/camilstaps/bar/Tap.java b/Week13 Bar/src/com/camilstaps/bar/Tap.java new file mode 100644 index 0000000..af22d01 --- /dev/null +++ b/Week13 Bar/src/com/camilstaps/bar/Tap.java @@ -0,0 +1,50 @@ +/* + * 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 tap can fill glasses + * @author Camil Staps, s4498062 + */ +public class Tap { + + private static final int CC_PER_GLASS = 200; + private static final int DRAW_TIME_PER_CC = 1; + + /** + * Fill a glass. This method can fill one glass at a time. + * @param glass + * @return + */ + public synchronized Glass fillUp(Glass glass) { + System.err.println("Tap fills glass " + glass.toString()); + try { + Thread.sleep(DRAW_TIME_PER_CC * (CC_PER_GLASS - glass.getVolume())); + } catch (InterruptedException e) {} + glass.fill(CC_PER_GLASS); + System.err.println("Tap finished filling glass " + glass.toString()); + return glass; + } + +} diff --git a/Week13 Bar/src/com/camilstaps/bar/Week13Bar.java b/Week13 Bar/src/com/camilstaps/bar/Week13Bar.java new file mode 100644 index 0000000..5035b62 --- /dev/null +++ b/Week13 Bar/src/com/camilstaps/bar/Week13Bar.java @@ -0,0 +1,41 @@ +/* + * 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; + +/** + * Solution to Week13, part 2 + * @author Camil Staps, s4498062 + */ +public class Week13Bar { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + Bar bar = new Bar(7); + bar.addDrinkers(10); + bar.startDrinking(); + } + +} |