Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/drtshock/BurntException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
public class BurntException extends Exception {

private static final long serialVersionUID = 1L;

public BurntException(int degrees) {
super("Potato is badly burnt by trying to boil it at " + degrees + " degrees!!");
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/drtshock/NotDeliciousException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
public class NotDeliciousException extends Exception {

private static final long serialVersionUID = 1L;

/**
* The reason for non-deliciousness.
*/
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/drtshock/NotDeliciousReason.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
2 changes: 2 additions & 0 deletions src/main/java/org/drtshock/OvenException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
public class OvenException extends Exception {

private static final long serialVersionUID = 1L;

public OvenException(Exception internalException) {
super(internalException);
}
Expand Down
92 changes: 47 additions & 45 deletions src/main/java/org/drtshock/Potato.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Condiment> condiments = new ArrayList<>();
private boolean boiled = false;
private boolean baked = false;

public static void main(String[] args) {
final Potato potato = new Potato();
Expand Down Expand Up @@ -39,23 +43,27 @@ public List<Condiment> 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", "onion");
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);
}
}
}

Expand All @@ -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();
Expand All @@ -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;
boiled = true;
}

/**
* Checks if this potato is cooked. Returns the result of {@link #hasBeenBoiledInWater()}.
* 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 baked, false if otherwise
* @return true if this potato is delicious, false if otherwise
* @throws NotDeliciousException if the potato is not delicious!
*/
public boolean isBoiled() {
@Override
public boolean isDelicious() throws NotDeliciousException {
try {
return this.hasBeenBoiledInWater();
if (potatential.nextBoolean()) bake();
if (potatential.nextBoolean()) boil();
} catch (BurntException e) {
return false;
throw new NotDeliciousException(NotDeliciousReason.BURNT);
} catch (OvenException | UndercookedException e) {
throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED);
}
}

/**
* Checks if this potato is delicious. Returns the result of {@link #isBaked()}.
*
* @return true if this potato is delicious, false if otherwise
*/
@Override
public boolean isDelicious() {
return this.isBaked() || this.isBoiled();
if (!boiled && ! baked) {
throw new NotDeliciousException(NotDeliciousReason.RAW);
}
return boiled ^ baked;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/drtshock/Tuber.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.drtshock;

public interface Tuber {
boolean isDelicious();

boolean isDelicious() throws NotDeliciousException;

Tuber propagate();

}
14 changes: 14 additions & 0 deletions src/main/java/org/drtshock/UndercookedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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!");
}

}