From fda8aa6a47e6cc067a211ad2fedfb07cebdac8d5 Mon Sep 17 00:00:00 2001 From: 7kasper Date: Tue, 29 May 2018 23:03:12 +0200 Subject: [PATCH 1/2] Improved code. Added more potatential. --- .../java/org/drtshock/BurntException.java | 4 +- .../org/drtshock/NotDeliciousException.java | 4 +- .../java/org/drtshock/NotDeliciousReason.java | 10 +- src/main/java/org/drtshock/OvenException.java | 4 +- src/main/java/org/drtshock/Potato.java | 94 ++++++++++--------- src/main/java/org/drtshock/Tuber.java | 4 +- .../org/drtshock/UndercookedException.java | 13 +++ 7 files changed, 80 insertions(+), 53 deletions(-) create mode 100644 src/main/java/org/drtshock/UndercookedException.java diff --git a/src/main/java/org/drtshock/BurntException.java b/src/main/java/org/drtshock/BurntException.java index aec2d35b..a10a343f 100644 --- a/src/main/java/org/drtshock/BurntException.java +++ b/src/main/java/org/drtshock/BurntException.java @@ -5,7 +5,9 @@ */ public class BurntException extends Exception { - public BurntException(int degrees) { + private static final long serialVersionUID = 1L; + + public BurntException(int degrees) { super("Potato is badly burnt by trying to boil it at " + degrees + " degrees!!"); } diff --git a/src/main/java/org/drtshock/NotDeliciousException.java b/src/main/java/org/drtshock/NotDeliciousException.java index bfcc707b..8e9a1189 100644 --- a/src/main/java/org/drtshock/NotDeliciousException.java +++ b/src/main/java/org/drtshock/NotDeliciousException.java @@ -5,7 +5,9 @@ */ public class NotDeliciousException extends Exception { - /** + private static final long serialVersionUID = 1L; + + /** * The reason for non-deliciousness. */ private NotDeliciousReason notDeliciousReason; diff --git a/src/main/java/org/drtshock/NotDeliciousReason.java b/src/main/java/org/drtshock/NotDeliciousReason.java index 1be8deb1..e1643d30 100644 --- a/src/main/java/org/drtshock/NotDeliciousReason.java +++ b/src/main/java/org/drtshock/NotDeliciousReason.java @@ -4,9 +4,13 @@ * Different ways the potato can be not delicious */ public enum NotDeliciousReason { - - UNDERCOOKED, + NOT_DELICIOUS_CONDIMENT, - EXPIRED_CONDIMENT + EXPIRED_CONDIMENT, + UNDERCOOKED, + BURNT, + RAW, + NO_BACON, + NOT_RIGHT_TEXTURE_PERHAPS_BOTH_COOKED_AND_BAKED } diff --git a/src/main/java/org/drtshock/OvenException.java b/src/main/java/org/drtshock/OvenException.java index 000fbc49..c023210c 100644 --- a/src/main/java/org/drtshock/OvenException.java +++ b/src/main/java/org/drtshock/OvenException.java @@ -5,7 +5,9 @@ */ public class OvenException extends Exception { - public OvenException(Exception internalException) { + private static final long serialVersionUID = 1L; + + public OvenException(Exception internalException) { super(internalException); } diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index 0d1d4c2a..ceba8c61 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -5,13 +5,17 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Random; /** * A delicious tuber that is eaten by various peoples all over the world. */ public class Potato implements Tuber { + private final Random potatential = new Random(); private final List condiments = new ArrayList<>(); + private boolean boiled = false; + private boolean baked = false; public static void main(String[] args) { final Potato potato = new Potato(); @@ -39,23 +43,27 @@ public List getCondiments() { * @throws NotDeliciousException If the potato is not delicious */ public void prepare() throws NotDeliciousException { - this.addCondiments("sour cream", "chives", "butter", "crumbled bacon", "grated cheese", "ketchup", "pepper", + this.maybeAddCondiments("sour cream", "chives", "butter", "crumbled bacon", "grated cheese", "ketchup", "pepper", "salt", "tabasco", "tomatoes"); this.listCondiments(); - if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED); + if(!getCondiments().stream().anyMatch(condiment -> condiment.getName().equals("crumbled bacon"))) + throw new NotDeliciousException(NotDeliciousReason.NO_BACON); + if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_RIGHT_TEXTURE_PERHAPS_BOTH_COOKED_AND_BAKED); } /** - * Adds condiments to the potato. + * Maybe adds condiments to the potato. * * @param names Names of the condiments to add */ - public void addCondiments(String... names) throws NotDeliciousException { + public void maybeAddCondiments(String... names) throws NotDeliciousException { for (String condimentName : names) { - Condiment condiment = new Condiment(condimentName, true); - if (!condiment.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_DELICIOUS_CONDIMENT); - if (condiment.isExpired()) throw new NotDeliciousException(NotDeliciousReason.EXPIRED_CONDIMENT); - this.getCondiments().add(condiment); + if (potatential.nextBoolean()) { + Condiment condiment = new Condiment(condimentName, true); + if (!condiment.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_DELICIOUS_CONDIMENT); + if (condiment.isExpired()) throw new NotDeliciousException(NotDeliciousReason.EXPIRED_CONDIMENT); + this.getCondiments().add(condiment); + } } } @@ -71,13 +79,15 @@ public void listCondiments() { } /** - * Checks if the potato is put into the oven. + * Bakes the potato in an oven and give useful feedback about it. * - * @return true if potato is in the oven, false if otherwise + * @return true if potato is correctly baked. + * @throws BurntException if the potato is burnt to a point of no deliciousness. * @throws OvenException if the oven encounters an internal exception */ - public boolean isPutIntoOven() throws OvenException, BurntException { + public void bake() throws BurntException, OvenException { try { + System.out.println("Trying to bake potato in the oven..."); long begin = System.currentTimeMillis(); final URL url = new URL("https://www.google.com/search?q=potato"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); @@ -87,63 +97,55 @@ public boolean isPutIntoOven() throws OvenException, BurntException { int inOven = connection.getResponseCode(); long bakeTime = (System.currentTimeMillis() - begin); if (bakeTime > 1100) throw new BurntException(bakeTime); - return inOven == 200; + System.out.println("Baked potato for " + bakeTime + " millis."); + baked = (inOven == 200); + if (!baked) { + System.out.println("Potato hasn't been baked!"); + } } catch (IOException ex) { throw new OvenException(ex); } } /** - * Checks if this potato is baked. Returns the result of {@link #isPutIntoOven()}. - * - * @return true if this potato is baked, false if otherwise - */ - public boolean isBaked() { - try { - return this.isPutIntoOven(); - } catch (OvenException | BurntException e) { - return false; - } - } - - /** - * Checks if the potato is succesfully boiled at the right amount of degrees. + * Boils a potato in water and gives useful feedback about it. * * @return true if the potato has succesfully been boiled, false if otherwise + * @throws UndercookedException if the potato hasn't cooked long enough. * @throws BurntException if the potato has been burned during the process of cooking */ - public boolean hasBeenBoiledInWater() throws BurntException { + public void boil() throws BurntException, UndercookedException { int waterDegrees = (int) (Math.random() * 200); System.out.println("Trying to boil potato at " + waterDegrees + " degrees."); - if (waterDegrees < 70) { - return false; + if (waterDegrees < 70 && !baked) { + throw new UndercookedException(waterDegrees); } else if (waterDegrees > 130) { throw new BurntException(waterDegrees); } - return true; - } - - /** - * Checks if this potato is cooked. Returns the result of {@link #hasBeenBoiledInWater()}. - * - * @return true if this potato is baked, false if otherwise - */ - public boolean isBoiled() { - try { - return this.hasBeenBoiledInWater(); - } catch (BurntException e) { - return false; - } + boiled = true; } /** - * Checks if this potato is delicious. Returns the result of {@link #isBaked()}. + * Checks if this potato is delicious. + * A potato can only be delicious if it has been either cooked or baked. * * @return true if this potato is delicious, false if otherwise + * @throws NotDeliciousException if the potato is not delicious! */ @Override - public boolean isDelicious() { - return this.isBaked() || this.isBoiled(); + public boolean isDelicious() throws NotDeliciousException { + try { + if (potatential.nextBoolean()) bake(); + if (potatential.nextBoolean()) boil(); + } catch (BurntException e) { + throw new NotDeliciousException(NotDeliciousReason.BURNT); + } catch (OvenException | UndercookedException e) { + throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED); + } + if (!boiled && ! baked) { + throw new NotDeliciousException(NotDeliciousReason.RAW); + } + return boiled ^ baked; } /** diff --git a/src/main/java/org/drtshock/Tuber.java b/src/main/java/org/drtshock/Tuber.java index feb752bf..e2ffa986 100644 --- a/src/main/java/org/drtshock/Tuber.java +++ b/src/main/java/org/drtshock/Tuber.java @@ -1,7 +1,9 @@ package org.drtshock; public interface Tuber { - boolean isDelicious(); + + boolean isDelicious() throws NotDeliciousException; Tuber propagate(); + } diff --git a/src/main/java/org/drtshock/UndercookedException.java b/src/main/java/org/drtshock/UndercookedException.java new file mode 100644 index 00000000..1eb94435 --- /dev/null +++ b/src/main/java/org/drtshock/UndercookedException.java @@ -0,0 +1,13 @@ +package org.drtshock; + +/** + * An exception to describe that something hasn't cooked enough! + */ +public class UndercookedException extends Exception { + + private static final long serialVersionUID = 1L; + + public UndercookedException(int degrees) { + super("Potato is badly undercooked at " + degrees + " degrees and thus too hard and not nice!"); + } +} From 9a4e596a4dc97498a546747e76878bcae058ef92 Mon Sep 17 00:00:00 2001 From: 7kasper Date: Wed, 30 May 2018 16:51:25 +0200 Subject: [PATCH 2/2] Tabs - --- .../java/org/drtshock/BurntException.java | 4 +-- .../org/drtshock/NotDeliciousException.java | 4 +-- src/main/java/org/drtshock/OvenException.java | 4 +-- src/main/java/org/drtshock/Potato.java | 36 +++++++++---------- .../org/drtshock/UndercookedException.java | 9 ++--- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/drtshock/BurntException.java b/src/main/java/org/drtshock/BurntException.java index a10a343f..2f141627 100644 --- a/src/main/java/org/drtshock/BurntException.java +++ b/src/main/java/org/drtshock/BurntException.java @@ -5,9 +5,9 @@ */ public class BurntException extends Exception { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public BurntException(int degrees) { + public BurntException(int degrees) { super("Potato is badly burnt by trying to boil it at " + degrees + " degrees!!"); } diff --git a/src/main/java/org/drtshock/NotDeliciousException.java b/src/main/java/org/drtshock/NotDeliciousException.java index 8e9a1189..f9daaf89 100644 --- a/src/main/java/org/drtshock/NotDeliciousException.java +++ b/src/main/java/org/drtshock/NotDeliciousException.java @@ -5,9 +5,9 @@ */ public class NotDeliciousException extends Exception { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** + /** * The reason for non-deliciousness. */ private NotDeliciousReason notDeliciousReason; diff --git a/src/main/java/org/drtshock/OvenException.java b/src/main/java/org/drtshock/OvenException.java index c023210c..79411229 100644 --- a/src/main/java/org/drtshock/OvenException.java +++ b/src/main/java/org/drtshock/OvenException.java @@ -5,9 +5,9 @@ */ public class OvenException extends Exception { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public OvenException(Exception internalException) { + public OvenException(Exception internalException) { super(internalException); } diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index ceba8c61..37cc3e5a 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -12,7 +12,7 @@ */ public class Potato implements Tuber { - private final Random potatential = new Random(); + private final Random potatential = new Random(); private final List condiments = new ArrayList<>(); private boolean boiled = false; private boolean baked = false; @@ -47,7 +47,7 @@ public void prepare() throws NotDeliciousException { "salt", "tabasco", "tomatoes"); this.listCondiments(); if(!getCondiments().stream().anyMatch(condiment -> condiment.getName().equals("crumbled bacon"))) - throw new NotDeliciousException(NotDeliciousReason.NO_BACON); + throw new NotDeliciousException(NotDeliciousReason.NO_BACON); if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_RIGHT_TEXTURE_PERHAPS_BOTH_COOKED_AND_BAKED); } @@ -58,12 +58,12 @@ public void prepare() throws NotDeliciousException { */ public void maybeAddCondiments(String... names) throws NotDeliciousException { for (String condimentName : names) { - if (potatential.nextBoolean()) { + if (potatential.nextBoolean()) { Condiment condiment = new Condiment(condimentName, true); if (!condiment.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_DELICIOUS_CONDIMENT); if (condiment.isExpired()) throw new NotDeliciousException(NotDeliciousReason.EXPIRED_CONDIMENT); this.getCondiments().add(condiment); - } + } } } @@ -87,7 +87,7 @@ public void listCondiments() { */ public void bake() throws BurntException, OvenException { try { - System.out.println("Trying to bake potato in the oven..."); + System.out.println("Trying to bake potato in the oven..."); long begin = System.currentTimeMillis(); final URL url = new URL("https://www.google.com/search?q=potato"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); @@ -100,7 +100,7 @@ public void bake() throws BurntException, OvenException { System.out.println("Baked potato for " + bakeTime + " millis."); baked = (inOven == 200); if (!baked) { - System.out.println("Potato hasn't been baked!"); + System.out.println("Potato hasn't been baked!"); } } catch (IOException ex) { throw new OvenException(ex); @@ -134,18 +134,18 @@ public void boil() throws BurntException, UndercookedException { */ @Override public boolean isDelicious() throws NotDeliciousException { - try { - if (potatential.nextBoolean()) bake(); - if (potatential.nextBoolean()) boil(); - } catch (BurntException e) { - throw new NotDeliciousException(NotDeliciousReason.BURNT); - } catch (OvenException | UndercookedException e) { - throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED); - } - if (!boiled && ! baked) { - throw new NotDeliciousException(NotDeliciousReason.RAW); - } - return boiled ^ baked; + try { + if (potatential.nextBoolean()) bake(); + if (potatential.nextBoolean()) boil(); + } catch (BurntException e) { + throw new NotDeliciousException(NotDeliciousReason.BURNT); + } catch (OvenException | UndercookedException e) { + throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED); + } + if (!boiled && ! baked) { + throw new NotDeliciousException(NotDeliciousReason.RAW); + } + return boiled ^ baked; } /** diff --git a/src/main/java/org/drtshock/UndercookedException.java b/src/main/java/org/drtshock/UndercookedException.java index 1eb94435..51d0f5db 100644 --- a/src/main/java/org/drtshock/UndercookedException.java +++ b/src/main/java/org/drtshock/UndercookedException.java @@ -5,9 +5,10 @@ */ public class UndercookedException extends Exception { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; + + public UndercookedException(int degrees) { + super("Potato is badly undercooked at " + degrees + " degrees and thus too hard and not nice!"); + } - public UndercookedException(int degrees) { - super("Potato is badly undercooked at " + degrees + " degrees and thus too hard and not nice!"); - } }