From b0160719bcece3b9d6c89d121968fa226a498282 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 20 May 2015 09:41:19 +0200 Subject: Week13 bar --- Week13 Bar/build.xml | 73 ++ Week13 Bar/manifest.mf | 3 + Week13 Bar/nbproject/build-impl.xml | 1413 +++++++++++++++++++++ Week13 Bar/nbproject/genfiles.properties | 8 + Week13 Bar/nbproject/project.properties | 73 ++ Week13 Bar/nbproject/project.xml | 15 + Week13 Bar/src/com/camilstaps/bar/Bar.java | 70 + Week13 Bar/src/com/camilstaps/bar/Glass.java | 46 + Week13 Bar/src/com/camilstaps/bar/ParchedMan.java | 84 ++ Week13 Bar/src/com/camilstaps/bar/Tap.java | 50 + Week13 Bar/src/com/camilstaps/bar/Week13Bar.java | 41 + 11 files changed, 1876 insertions(+) create mode 100644 Week13 Bar/build.xml create mode 100644 Week13 Bar/manifest.mf create mode 100644 Week13 Bar/nbproject/build-impl.xml create mode 100644 Week13 Bar/nbproject/genfiles.properties create mode 100644 Week13 Bar/nbproject/project.properties create mode 100644 Week13 Bar/nbproject/project.xml create mode 100644 Week13 Bar/src/com/camilstaps/bar/Bar.java create mode 100644 Week13 Bar/src/com/camilstaps/bar/Glass.java create mode 100644 Week13 Bar/src/com/camilstaps/bar/ParchedMan.java create mode 100644 Week13 Bar/src/com/camilstaps/bar/Tap.java create mode 100644 Week13 Bar/src/com/camilstaps/bar/Week13Bar.java (limited to 'Week13 Bar') diff --git a/Week13 Bar/build.xml b/Week13 Bar/build.xml new file mode 100644 index 0000000..3fadad5 --- /dev/null +++ b/Week13 Bar/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Week13 Bar. + + + diff --git a/Week13 Bar/manifest.mf b/Week13 Bar/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/Week13 Bar/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/Week13 Bar/nbproject/build-impl.xml b/Week13 Bar/nbproject/build-impl.xml new file mode 100644 index 0000000..50ae022 --- /dev/null +++ b/Week13 Bar/nbproject/build-impl.xml @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week13 Bar/nbproject/genfiles.properties b/Week13 Bar/nbproject/genfiles.properties new file mode 100644 index 0000000..3fa80da --- /dev/null +++ b/Week13 Bar/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=b8664875 +build.xml.script.CRC32=a64171bd +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=b8664875 +nbproject/build-impl.xml.script.CRC32=52cda460 +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/Week13 Bar/nbproject/project.properties b/Week13 Bar/nbproject/project.properties new file mode 100644 index 0000000..b96c37e --- /dev/null +++ b/Week13 Bar/nbproject/project.properties @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/Week13_Bar.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=com.camilstaps.bar.Week13Bar +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/Week13 Bar/nbproject/project.xml b/Week13 Bar/nbproject/project.xml new file mode 100644 index 0000000..4d2d01c --- /dev/null +++ b/Week13 Bar/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + Week13 Bar + + + + + + + + + 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 + * + * 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 glasses; + private final List 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 + * + * 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 + * + * 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 + * + * 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 + * + * 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(); + } + +} -- cgit v1.2.3