Merge pull request #105 from Trophonix/master

Enhancements and features
This commit is contained in:
Trent Hensler
2017-06-26 11:06:18 -07:00
committed by GitHub
4 changed files with 48 additions and 21 deletions

View File

@@ -9,4 +9,8 @@ public class BurntException extends Exception {
super("Potato is badly burnt by trying to boil it at " + degrees + " degrees!!"); 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)");
}
} }

View File

@@ -14,4 +14,7 @@ public class NotDeliciousException extends Exception {
this.notDeliciousReason = notDeliciousReason; this.notDeliciousReason = notDeliciousReason;
} }
public NotDeliciousReason getReason() {
return notDeliciousReason;
}
} }

View File

@@ -5,7 +5,8 @@ package org.drtshock;
*/ */
public enum NotDeliciousReason { public enum NotDeliciousReason {
NOT_BAKED, UNDERCOOKED,
NOT_DELICIOUS_CONDIMENT NOT_DELICIOUS_CONDIMENT,
EXPIRED_CONDIMENT
} }

View File

@@ -19,7 +19,7 @@ public class Potato implements Tuber {
potato.prepare(); potato.prepare();
System.out.println("Of course Potato is prepared and delicious."); System.out.println("Of course Potato is prepared and delicious.");
} catch (NotDeliciousException e) { } 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", this.addCondiments("sour cream", "chives", "butter", "crumbled bacon", "grated cheese", "ketchup", "pepper",
"salt", "tabasco", "tomatoes"); "salt", "tabasco", "tomatoes");
this.listCondiments(); 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) { for (String condimentName : names) {
Condiment condiment = new Condiment(condimentName, true); Condiment condiment = new Condiment(condimentName, true);
if (!condiment.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_DELICIOUS_CONDIMENT); if (!condiment.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.NOT_DELICIOUS_CONDIMENT);
if (condiment.isExpired()) throw new NotDeliciousException(NotDeliciousReason.EXPIRED_CONDIMENT);
this.getCondiments().add(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 * @return true if potato is in the oven, false if otherwise
* @throws OvenException if the oven encounters an internal exception * @throws OvenException if the oven encounters an internal exception
*/ */
public boolean isPutIntoOven() throws OvenException { public boolean isPutIntoOven() throws OvenException, BurntException {
try { try {
long begin = System.currentTimeMillis();
final URL url = new URL("https://www.google.com/search?q=potato"); final URL url = new URL("https://www.google.com/search?q=potato");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.addRequestProperty("User-Agent", "Potato/1.7.5"); connection.addRequestProperty("User-Agent", "Potato/1.7.5");
connection.connect(); connection.connect();
int inOven = connection.getResponseCode(); int inOven = connection.getResponseCode();
long bakeTime = (System.currentTimeMillis() - begin);
if (bakeTime > 1100) throw new BurntException(bakeTime);
return inOven == 200; return inOven == 200;
} catch (IOException ex) { } catch (IOException ex) {
throw new OvenException(ex); throw new OvenException(ex);
@@ -97,20 +101,7 @@ public class Potato implements Tuber {
public boolean isBaked() { public boolean isBaked() {
try { try {
return this.isPutIntoOven(); return this.isPutIntoOven();
} catch (OvenException e) { } catch (OvenException | BurntException 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) {
return false; return false;
} }
} }
@@ -132,6 +123,19 @@ public class Potato implements Tuber {
return true; 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()}. * Checks if this potato is delicious. Returns the result of {@link #isBaked()}.
* *
@@ -139,7 +143,7 @@ public class Potato implements Tuber {
*/ */
@Override @Override
public boolean isDelicious() { 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 class Condiment {
private final String name; private final String name;
private final boolean delicious; 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.name = name;
this.delicious = delicious; 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; 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. * Gets the name of this condiment.
* *