diff --git a/src/main/java/org/drtshock/BurntException.java b/src/main/java/org/drtshock/BurntException.java index 7297d39..aec2d35 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(long bakeTime) { + super("Potato is badly burnt by baking for too long!! (" + bakeTime + "ms)"); + } + } 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 0687304..1be8deb 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 { - NOT_BAKED, - NOT_DELICIOUS_CONDIMENT + UNDERCOOKED, + 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..0d1d4c2 100644 --- a/src/main/java/org/drtshock/Potato.java +++ b/src/main/java/org/drtshock/Potato.java @@ -19,7 +19,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()); } } @@ -42,7 +42,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.UNDERCOOKED); } /** @@ -54,6 +54,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); } } @@ -75,14 +76,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(bakeTime); return inOven == 200; } catch (IOException ex) { throw new OvenException(ex); @@ -97,20 +101,7 @@ public class Potato implements Tuber { public boolean isBaked() { try { return this.isPutIntoOven(); - } catch (OvenException e) { - return false; - } - } - - /** - * Checks if this potato is cooked. Returns the result of {@link #hasBeenBoiledInWater()}. - * - * @return true if this potato is baked, false if otherwise - */ - public boolean isCooked() { - try { - return this.hasBeenBoiledInWater(); - } catch (BurntException e) { + } catch (OvenException | BurntException e) { return false; } } @@ -132,6 +123,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()}. * @@ -139,7 +143,7 @@ public class Potato implements Tuber { */ @Override public boolean isDelicious() { - return this.isBaked() || this.isCooked(); + return this.isBaked() || this.isBoiled(); } /** @@ -158,10 +162,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, Math.random() * 100 < 3); } /** @@ -173,6 +183,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. *