Last active
August 29, 2015 14:07
-
-
Save freeslugs/b413860e38a971d68b44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.lang.Math; | |
public class YummySoup extends Ingredient{ | |
static Oil canola = new Oil("canola"); | |
static Ingredient[] favoriteVeggies = {new Ingredient("onions"), new Ingredient("peppers")}; | |
static Spice[] sAndP = {new Spice("salt"), new Spice("pepper")}; | |
static Ingredient water = new Ingredient("water"); | |
public static void main(String[] args){ | |
if (args.length != 4){ | |
System.out.println("Please rerun, with the args of your choice, as: java YummySoup protein bouillon flavoring carb"); | |
System.exit(args.length); | |
} | |
Ingredient protein = new Ingredient(args[0]); | |
Ingredient bouillon = new Ingredient(args[1]); | |
Ingredient flavor = new Ingredient(args[2]); | |
Ingredient carb = new Ingredient(args[3]); //Assume it's cooked... | |
try{ | |
System.out.println(""); | |
System.out.println("Note: cooking time has been sped up by a factor of "+Timer.speedup+" for brevity."); | |
System.out.println(""); | |
YummySoup eatMe = new YummySoup(protein, bouillon, flavor, carb); | |
eatMe.eat(); | |
} catch (InterruptedException e) { | |
System.out.println("Cooking was interupted somehow!"); | |
} | |
} | |
void eat(){ | |
System.out.println("That "+this.toString()+" was good!"); | |
} | |
YummySoup(Ingredient protein, Ingredient bouillon, Ingredient flavor, Ingredient cookedCarb) throws InterruptedException{ | |
super( | |
"Soup made of: "+Ingredient.mix(new Ingredient[]{ | |
cook(new Ingredient[]{protein}, sAndP, canola), | |
cook(favoriteVeggies, new Ingredient[]{bouillon, flavor}, canola), cookedCarb | |
}) | |
); //().replaceAll("\\+", ", ") | |
} | |
static Ingredient cook(Ingredient[] ingredients, Ingredient[] spices, Oil oil) throws InterruptedException{ | |
int elapsedTime = 0; | |
System.out.println("Now cooking: "+Arrays.toString(ingredients)); | |
Pan hotPan = new Pan(); | |
hotPan.setTemp(Temp.MEDIUM); | |
System.out.println("Start by preheating the pan"); | |
elapsedTime += Timer.wait(1); | |
hotPan.add(oil); | |
hotPan.addAll(Arrays.asList(spices)); | |
System.out.println("Now add ["+oil+"] and "+Arrays.toString(spices)+" to the hot pan"); | |
elapsedTime += Timer.wait(1); | |
System.out.println("Now add the main ingredients to the hot pan"); | |
hotPan.addAll(Arrays.asList(ingredients)); | |
boolean raw = true; | |
while( raw ){ | |
if (Math.random() > 0.75){ //TODO.. | |
System.out.println("Maybe we should turn up the heat on the hot pan?"); | |
hotPan.setTemp(Temp.HIGH); | |
} | |
if (hotPan.getTemp().compareTo(Temp.MEDIUM) > 0){ | |
System.out.println("Oh no, the pan is too hot! Add some water and lower the heat!"); | |
hotPan.setTemp(Temp.LOW); | |
hotPan.add(water); | |
} | |
System.out.println("Now let's cover the hot pan and let the ingredients cook"); | |
hotPan.cover(); | |
elapsedTime += Timer.wait(2); | |
System.out.println("Now let's uncover the hot pan to check on the ingredients"); | |
hotPan.uncover(); | |
raw = false; | |
for (Ingredient ingred: hotPan.getAll()){ | |
if (ingred.isRaw(elapsedTime)) { | |
System.out.println("Your "+ingred+" has been cooking for "+elapsedTime+" minutes, but is still raw."); | |
raw = true; | |
} | |
} | |
} | |
System.out.println("Now all of the ingredients are cooked, so let's turn off the heat on the pan."); | |
hotPan.setTemp(Temp.OFF); | |
System.out.println("Your "+Arrays.toString(ingredients)+" is done!"); | |
System.out.println(""); | |
return Ingredient.mix(hotPan.getAll()); | |
} | |
} | |
class Timer{ | |
static int speedup = 20; | |
static int wait(int min) throws InterruptedException{ | |
System.out.println("Wait for "+min+" minutes.."); | |
long realtime = 60000/speedup; | |
Thread.sleep(realtime); | |
return min; | |
} | |
} | |
class Oil extends Ingredient{ | |
Oil(String s){ | |
super(s); | |
} | |
boolean isRaw(int elapsed){ | |
return false; | |
} | |
} | |
class Spice extends Ingredient{ | |
Spice(String s){ | |
super(s); | |
} | |
boolean isRaw(int elapsed){ | |
return false; | |
} | |
} | |
class Ingredient { | |
public final String name; | |
Ingredient(String s){ | |
name = s; | |
} | |
boolean isRaw(int elapsed){ | |
//System.out.println("Your "+this+" has been cooking for "+elapsed+" minutes now.."); | |
return Math.random() < Math.pow(elapsed, -0.8); //TODO... Actually check if your food is cooked, please. | |
} | |
static Ingredient mix(Ingredient[] ingredients){ | |
String bigName = ""; | |
for (Ingredient ingred: ingredients){ | |
if (bigName.length() > 0) { | |
bigName += "\\+"+ingred.name; | |
} | |
} | |
return new Ingredient(bigName); | |
} | |
public String toString(){ | |
/* | |
if (this.getClass() == Ingredient.class){ | |
return name; | |
} else { | |
return ""; | |
}*/ | |
return name; | |
} | |
} | |
class Pan extends ArrayList<Ingredient>{ | |
Temp temperature = Temp.OFF; | |
boolean isCovered = false; | |
void setTemp(Temp newT){ | |
temperature = newT; | |
} | |
Temp getTemp(){ | |
return temperature; | |
} | |
void cover(){ | |
isCovered = true; | |
} | |
void uncover(){ | |
isCovered = false; | |
} | |
Ingredient[] getAll(){ | |
if (isCovered){ | |
return null; | |
} else { | |
return this.toArray(new Ingredient[0]); | |
} | |
} | |
} | |
enum Temp { //implements Comparable<Temp> | |
OFF(0), LOW(100), MEDIUM(150), HIGH(200); | |
final int temp; | |
Temp(int t){ | |
temp = t; | |
} | |
/* | |
int compareTo(Temp a){ | |
if (this.temp == a.temp){ | |
return 0; | |
} else if (this.temp > a.temp) { | |
return 1; | |
} else { | |
return -1; | |
} | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment