From 5b6db03bbdd8773ace496c296479616a14ad5b6a Mon Sep 17 00:00:00 2001 From: Trophonix <502badgamer@gmail.com> Date: Wed, 21 Jun 2017 08:58:25 -0500 Subject: [PATCH 1/7] Add condiment expiry functionality --- .../java/org/drtshock/NotDeliciousReason.java | 3 ++- src/main/java/org/drtshock/Potato.java | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/drtshock/NotDeliciousReason.java b/src/main/java/org/drtshock/NotDeliciousReason.java index 0687304..5b69139 100644 --- a/src/main/java/org/drtshock/NotDeliciousReason.java +++ b/src/main/java/org/drtshock/NotDeliciousReason.java @@ -6,6 +6,7 @@ package org.drtshock; public enum NotDeliciousReason { NOT_BAKED, - NOT_DELICIOUS_CONDIMENT + NOT_DELICIOUS_CONDIMENT, + EXPIRED_CONDIMENT } diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index fbe939e..e5b9ff3 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -5,6 +5,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ThreadLocalRandom; /** * A delicious tuber that is eaten by various peoples all over the world. @@ -54,6 +55,7 @@ public class Potato implements Tuber { 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); } } @@ -158,10 +160,16 @@ public class Potato implements Tuber { private class Condiment { private final String name; private final boolean delicious; + private final boolean expired; - public Condiment(String name, boolean delicious) { + public Condiment(String name, boolean delicious, boolean expired) { this.name = name; this.delicious = delicious; + this.expired = expired; + } + + public Condiment(String name, boolean delicious) { + this(name, delicious, ThreadLocalRandom.current().nextInt(100) < 3); } /** @@ -173,6 +181,15 @@ public class Potato implements Tuber { return this.delicious; } + /** + * Returns if this condiment is expired or not. + * + * @return true if expired, false if otherwise + */ + public boolean isExpired() { + return expired; + } + /** * Gets the name of this condiment. * From a016415cfd6c7a5ff0d19da065d9bab8cdf81da5 Mon Sep 17 00:00:00 2001 From: Trophonix <502badgamer@gmail.com> Date: Wed, 21 Jun 2017 08:59:24 -0500 Subject: [PATCH 2/7] Rename NOT_BAKED to RAW --- src/main/java/org/drtshock/NotDeliciousReason.java | 2 +- src/main/java/org/drtshock/Potato.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/drtshock/NotDeliciousReason.java b/src/main/java/org/drtshock/NotDeliciousReason.java index 5b69139..5939f97 100644 --- a/src/main/java/org/drtshock/NotDeliciousReason.java +++ b/src/main/java/org/drtshock/NotDeliciousReason.java @@ -5,7 +5,7 @@ package org.drtshock; */ public enum NotDeliciousReason { - NOT_BAKED, + RAW, NOT_DELICIOUS_CONDIMENT, EXPIRED_CONDIMENT diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index e5b9ff3..b52c269 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -43,7 +43,7 @@ public class Potato implements Tuber { this.addCondiments("sour cream", "chives", "butter", "crumbled bacon", "grated cheese", "ketchup", "pepper", "salt", "tabasco", "tomatoes"); this.listCondiments(); - if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_BAKED); + if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.RAW); } /** From c69264a5521da1c12a506aa844d9dfcdd471eb34 Mon Sep 17 00:00:00 2001 From: Trophonix <502badgamer@gmail.com> Date: Wed, 21 Jun 2017 09:13:23 -0500 Subject: [PATCH 3/7] Add overcooked reason, rename raw to undercooked --- .../org/drtshock/NotDeliciousException.java | 3 +++ .../java/org/drtshock/NotDeliciousReason.java | 3 ++- src/main/java/org/drtshock/Potato.java | 20 +++++++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/drtshock/NotDeliciousException.java b/src/main/java/org/drtshock/NotDeliciousException.java index 8e9b5cf..bfcc707 100644 --- a/src/main/java/org/drtshock/NotDeliciousException.java +++ b/src/main/java/org/drtshock/NotDeliciousException.java @@ -14,4 +14,7 @@ public class NotDeliciousException extends Exception { this.notDeliciousReason = notDeliciousReason; } + public NotDeliciousReason getReason() { + return notDeliciousReason; + } } diff --git a/src/main/java/org/drtshock/NotDeliciousReason.java b/src/main/java/org/drtshock/NotDeliciousReason.java index 5939f97..d093f08 100644 --- a/src/main/java/org/drtshock/NotDeliciousReason.java +++ b/src/main/java/org/drtshock/NotDeliciousReason.java @@ -5,7 +5,8 @@ package org.drtshock; */ public enum NotDeliciousReason { - RAW, + UNDERCOOKED, + OVERCOOKED, NOT_DELICIOUS_CONDIMENT, EXPIRED_CONDIMENT diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index b52c269..46f9546 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -20,7 +20,7 @@ public class Potato implements Tuber { potato.prepare(); System.out.println("Of course Potato is prepared and delicious."); } catch (NotDeliciousException e) { - System.err.println("Fatal error! How could Potato not be delicious?"); + System.err.println("Fatal error! How could Potato not be delicious?\nReason: " + e.getReason()); } } @@ -43,7 +43,7 @@ public class Potato implements Tuber { this.addCondiments("sour cream", "chives", "butter", "crumbled bacon", "grated cheese", "ketchup", "pepper", "salt", "tabasco", "tomatoes"); this.listCondiments(); - if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.RAW); + if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED); } /** @@ -96,9 +96,13 @@ public class Potato implements Tuber { * * @return true if this potato is baked, false if otherwise */ - public boolean isBaked() { + public boolean isBaked() throws NotDeliciousException { try { - return this.isPutIntoOven(); + long begin = System.currentTimeMillis(); + boolean isInOven = this.isPutIntoOven(); + long bakeTime = (System.currentTimeMillis() - begin); + if (bakeTime > 1100) throw new NotDeliciousException(NotDeliciousReason.OVERCOOKED); + return isInOven; } catch (OvenException e) { return false; } @@ -109,7 +113,7 @@ public class Potato implements Tuber { * * @return true if this potato is baked, false if otherwise */ - public boolean isCooked() { + public boolean isBoiled() { try { return this.hasBeenBoiledInWater(); } catch (BurntException e) { @@ -141,7 +145,11 @@ public class Potato implements Tuber { */ @Override public boolean isDelicious() { - return this.isBaked() || this.isCooked(); + try { + return this.isBaked() || this.isBoiled(); + } catch (NotDeliciousException e) { + return false; + } } /** From b0ea26519deef3229758025f8312cffdf8530763 Mon Sep 17 00:00:00 2001 From: Trophonix <502badgamer@gmail.com> Date: Wed, 21 Jun 2017 09:16:59 -0500 Subject: [PATCH 4/7] Re-order methods for consistency --- src/main/java/org/drtshock/Potato.java | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index 46f9546..25205f7 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -108,19 +108,6 @@ public class Potato implements Tuber { } } - /** - * 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; - } - } - /** * Checks if the potato is succesfully boiled at the right amount of degrees. * @@ -138,6 +125,19 @@ public class Potato implements Tuber { 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; + } + } + /** * Checks if this potato is delicious. Returns the result of {@link #isBaked()}. * From 40831b6e015c9d1b2679657af9f673f129ab2011 Mon Sep 17 00:00:00 2001 From: Trophonix <502badgamer@gmail.com> Date: Wed, 21 Jun 2017 09:21:57 -0500 Subject: [PATCH 5/7] Add BurntException for baking too long --- .../java/org/drtshock/BurntException.java | 4 ++++ .../java/org/drtshock/NotDeliciousReason.java | 1 - src/main/java/org/drtshock/Potato.java | 21 +++++++------------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/drtshock/BurntException.java b/src/main/java/org/drtshock/BurntException.java index 7297d39..e76b7f9 100644 --- a/src/main/java/org/drtshock/BurntException.java +++ b/src/main/java/org/drtshock/BurntException.java @@ -9,4 +9,8 @@ public class BurntException extends Exception { super("Potato is badly burnt by trying to boil it at " + degrees + " degrees!!"); } + public BurntException() { + super("Potato is badly burnt by baking for too long!"); + } + } diff --git a/src/main/java/org/drtshock/NotDeliciousReason.java b/src/main/java/org/drtshock/NotDeliciousReason.java index d093f08..1be8deb 100644 --- a/src/main/java/org/drtshock/NotDeliciousReason.java +++ b/src/main/java/org/drtshock/NotDeliciousReason.java @@ -6,7 +6,6 @@ package org.drtshock; public enum NotDeliciousReason { UNDERCOOKED, - OVERCOOKED, NOT_DELICIOUS_CONDIMENT, EXPIRED_CONDIMENT diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index 25205f7..abb0305 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -77,14 +77,17 @@ public class Potato implements Tuber { * @return true if potato is in the oven, false if otherwise * @throws OvenException if the oven encounters an internal exception */ - public boolean isPutIntoOven() throws OvenException { + public boolean isPutIntoOven() throws OvenException, BurntException { try { + long begin = System.currentTimeMillis(); final URL url = new URL("https://www.google.com/search?q=potato"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.addRequestProperty("User-Agent", "Potato/1.7.5"); connection.connect(); int inOven = connection.getResponseCode(); + long bakeTime = (System.currentTimeMillis() - begin); + if (bakeTime > 1100) throw new BurntException(); return inOven == 200; } catch (IOException ex) { throw new OvenException(ex); @@ -96,14 +99,10 @@ public class Potato implements Tuber { * * @return true if this potato is baked, false if otherwise */ - public boolean isBaked() throws NotDeliciousException { + public boolean isBaked() { try { - long begin = System.currentTimeMillis(); - boolean isInOven = this.isPutIntoOven(); - long bakeTime = (System.currentTimeMillis() - begin); - if (bakeTime > 1100) throw new NotDeliciousException(NotDeliciousReason.OVERCOOKED); - return isInOven; - } catch (OvenException e) { + return this.isPutIntoOven(); + } catch (OvenException | BurntException e) { return false; } } @@ -145,11 +144,7 @@ public class Potato implements Tuber { */ @Override public boolean isDelicious() { - try { - return this.isBaked() || this.isBoiled(); - } catch (NotDeliciousException e) { - return false; - } + return this.isBaked() || this.isBoiled(); } /** From b0b434b61405cc9832cc5307d6649b6d648faea2 Mon Sep 17 00:00:00 2001 From: Trophonix <502badgamer@gmail.com> Date: Wed, 21 Jun 2017 09:24:42 -0500 Subject: [PATCH 6/7] Use Math.random() for consistency --- src/main/java/org/drtshock/Potato.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index abb0305..fc4c795 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -5,7 +5,6 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ThreadLocalRandom; /** * A delicious tuber that is eaten by various peoples all over the world. @@ -172,7 +171,7 @@ public class Potato implements Tuber { } public Condiment(String name, boolean delicious) { - this(name, delicious, ThreadLocalRandom.current().nextInt(100) < 3); + this(name, delicious, Math.random() * 100 < 3); } /** From 59e7da2dbbee617951b90b96c6b8b08c644cadcf Mon Sep 17 00:00:00 2001 From: Trophonix <502badgamer@gmail.com> Date: Wed, 21 Jun 2017 09:26:17 -0500 Subject: [PATCH 7/7] Add bakeTime to BurntException --- src/main/java/org/drtshock/BurntException.java | 4 ++-- src/main/java/org/drtshock/Potato.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/drtshock/BurntException.java b/src/main/java/org/drtshock/BurntException.java index e76b7f9..aec2d35 100644 --- a/src/main/java/org/drtshock/BurntException.java +++ b/src/main/java/org/drtshock/BurntException.java @@ -9,8 +9,8 @@ public class BurntException extends Exception { super("Potato is badly burnt by trying to boil it at " + degrees + " degrees!!"); } - public BurntException() { - super("Potato is badly burnt by baking for too long!"); + public BurntException(long bakeTime) { + super("Potato is badly burnt by baking for too long!! (" + bakeTime + "ms)"); } } diff --git a/src/main/java/org/drtshock/Potato.java b/src/main/java/org/drtshock/Potato.java index fc4c795..0d1d4c2 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -86,7 +86,7 @@ public class Potato implements Tuber { connection.connect(); int inOven = connection.getResponseCode(); long bakeTime = (System.currentTimeMillis() - begin); - if (bakeTime > 1100) throw new BurntException(); + if (bakeTime > 1100) throw new BurntException(bakeTime); return inOven == 200; } catch (IOException ex) { throw new OvenException(ex);