Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
20 changes: 20 additions & 0 deletions src/main/java/org/drtshock/Fries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.drtshock;

import org.drtshock.exceptions.BurntException;
import org.drtshock.exceptions.NotDeliciousException;
import org.drtshock.types.NotDeliciousReason;

public class Fries extends Potato {

public Fries(boolean isVegan) {
super(isVegan);
}

public void prepare() throws NotDeliciousException, BurntException {
this.addCondiments("salt", "pepper");
if (!this.isVegan()) this.addCondiments("ketchup", "mayo");
if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED);
else if (Math.random() < 0.1) throw new BurntException(250 + (int) (Math.random() * 100));
}

}
24 changes: 24 additions & 0 deletions src/main/java/org/drtshock/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.drtshock;

import org.drtshock.exceptions.BurntException;
import org.drtshock.exceptions.NotDeliciousException;

/**
* Where the potato deliciousness begins.
*/
public class Main {
public static void main(String[] args) {
Potato potato = new Potato(args.length == 1 && args[0].equals("--vegan"));
potato = Math.random() < 0.75 ? potato : potato.turnIntoFries();
if (potato.isVegan()) System.out.println("This potato is vegan.");
try {
potato.prepare();
potato.listCondiments();
System.out.println("Of course Potato is prepared and delicious.");
} catch (NotDeliciousException e) {
System.err.println("Fatal error! How could Potato not be delicious?\nReason: " + e.getReason());
} catch (BurntException e) {
System.err.println("Fatal error! Potato has been burnt to a crisp!\nTemperature: " + e.getTemperature() + " degrees");
}
}
}
40 changes: 26 additions & 14 deletions src/main/java/org/drtshock/Potato.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.drtshock;

import org.drtshock.exceptions.BurntException;
import org.drtshock.exceptions.NotDeliciousException;
import org.drtshock.types.NotDeliciousReason;
import org.drtshock.exceptions.OvenException;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
Expand All @@ -14,21 +19,19 @@ public class Potato implements Tuber {
private final boolean isVegan;
private final List<Condiment> condiments = new ArrayList<>();

public static void main(String[] args) {
final Potato potato = new Potato(args.length == 1 && args[0].equals("--vegan"));
if (potato.isVegan) System.out.println("This potato is vegan.");
try {
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?\nReason: " + e.getReason());
}
}

public Potato(boolean isVegan) {
this.isVegan = isVegan;
}

/**
* Checks if this potato is vegan.
*
* @return true if this potato is vegan, false if otherwise
*/
public boolean isVegan() {
return this.isVegan;
}

/**
* Gets the condiments on this potato.
*
Expand All @@ -44,10 +47,9 @@ public List<Condiment> getCondiments() {
*
* @throws NotDeliciousException If the potato is not delicious
*/
public void prepare() throws NotDeliciousException {
public void prepare() throws NotDeliciousException, BurntException {
this.addCondiments("chives", "butter", "pepper", "salt", "tabasco", "tomatoes", "onion");
if (!this.isVegan) this.addCondiments("sour cream", "crumbled bacon", "grated cheese", "ketchup");
this.listCondiments();
if (!this.isDelicious()) throw new NotDeliciousException(NotDeliciousReason.UNDERCOOKED);
}

Expand Down Expand Up @@ -76,6 +78,16 @@ public void listCondiments() {
}
}

/**
* Turns this potato into fries.
*
* @return A new {@link Fries} object
*/
public Fries turnIntoFries() {
System.out.println("Turning potato into fries...");
return new Fries(isVegan);
}

/**
* Checks if the potato is put into the oven.
*
Expand Down Expand Up @@ -165,7 +177,7 @@ public Tuber propagate() {
/**
* A type of food added to tubers.
*/
private class Condiment {
private static class Condiment {
private final String name;
private final boolean delicious;
private final boolean expired;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/drtshock/Tuber.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.drtshock;

import org.drtshock.exceptions.BurntException;
import org.drtshock.exceptions.NotDeliciousException;

public interface Tuber {
boolean isDelicious();

Tuber propagate();

void prepare() throws NotDeliciousException, BurntException;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package org.drtshock;
package org.drtshock.exceptions;

/**
* An exception to describe that something went wrong with our oven!
*/
public class BurntException extends Exception {

private int temperature;
private long bakeTime;

public BurntException(int degrees) {
super("Potato is badly burnt by trying to boil it at " + degrees + " degrees!!");
this.temperature = degrees;
}

public BurntException(long bakeTime) {
super("Potato is badly burnt by baking for too long!! (" + bakeTime + "ms)");
this.bakeTime = bakeTime;
}

public int getTemperature() {
return temperature;
}

public long getBakeTime() {
return bakeTime;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package org.drtshock;
package org.drtshock.exceptions;

import org.drtshock.types.NotDeliciousReason;

/**
* An exception to describe that the potato isn't delicious!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.drtshock;
package org.drtshock.exceptions;

/**
* An exception to describe that something went wrong with our oven!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.drtshock;
package org.drtshock.types;

/**
* Different ways the potato can be not delicious
Expand Down