2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -23,5 +23,31 @@
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit4">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit5.8.1">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
107
src/RecipeInstructions.java
Normal file
107
src/RecipeInstructions.java
Normal file
@@ -0,0 +1,107 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class RecipeInstructions
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
String userInstruction;
|
||||
|
||||
ArrayList<RecipeNode> ingredients = new ArrayList<>();
|
||||
RecipeTree fullInstructions = new RecipeTree();
|
||||
Scanner userInput = new Scanner(System.in);
|
||||
|
||||
do
|
||||
{
|
||||
System.out.println("Enter instruction: ");
|
||||
userInstruction = userInput.nextLine();
|
||||
|
||||
RecipeCreation(userInstruction,fullInstructions);
|
||||
fullInstructions.printTree();
|
||||
System.out.println(fullInstructions.sortTopology());
|
||||
|
||||
} while(!userInstruction.equalsIgnoreCase("done"));
|
||||
}
|
||||
|
||||
//Magical Regex
|
||||
public static boolean patternMatch(String pattern, String input)
|
||||
{
|
||||
Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
|
||||
return p.matcher(input).find();
|
||||
}
|
||||
|
||||
// This creates an action + children
|
||||
public static void RecipeCreation(String instructionIn, RecipeTree fullInstructions)
|
||||
{
|
||||
//Need to not hardcode verbs maybe a global variable for it? etc etc
|
||||
String[] verbs = {"add", "mix", "stir", "wait", "bake", "heat", "sift", "boil", "melt", "fry", "cool",
|
||||
"remove", "eat", "cut"};
|
||||
//this is where it gets juicy
|
||||
//FIRST WE FIND A VERB FOR THE ACTION
|
||||
for (String verb : verbs)
|
||||
{
|
||||
//we regex the input for the thing
|
||||
if (patternMatch(verb, instructionIn))
|
||||
{
|
||||
//get Action
|
||||
RecipeNode newAction = new RecipeNode(verb, 0);
|
||||
//Split after verb
|
||||
//this is where we split the string
|
||||
String[] split = instructionIn.split(verb);
|
||||
//We get the words only POG
|
||||
String[] words = split[1].split(" ");
|
||||
construct_children(newAction, words);
|
||||
//we add the thing to the tree (this also needs to be checked for WHERE it gets added)
|
||||
fullInstructions.actionAdd(newAction);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//MMMMM children yes??
|
||||
//Seriously this takes the action node and the rest of the words in sentence and adds them as children
|
||||
public static void construct_children(RecipeNode node, String[] words)
|
||||
{
|
||||
int Number = 0;
|
||||
String Ingredient = null;
|
||||
for (String word : words)
|
||||
{
|
||||
if (word.matches("\\d+"))
|
||||
{
|
||||
Number = Integer.parseInt(word);
|
||||
} else if (word.matches("[a-zA-Z]+") && !word.equals("and"))
|
||||
{
|
||||
Ingredient = word;
|
||||
}
|
||||
if (Number != 0 && Ingredient != null)
|
||||
{
|
||||
RecipeNode newIngredient = new RecipeNode(Ingredient, Number);
|
||||
node.addChild(newIngredient);
|
||||
newIngredient.parent = node;
|
||||
Number = 0;
|
||||
Ingredient = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Tests duh they work :DDDDDD
|
||||
@Test
|
||||
void testRecipeCreation()
|
||||
{
|
||||
}
|
||||
@Test
|
||||
void testconstruct_children()
|
||||
{
|
||||
RecipeNode node = new RecipeNode("", 0);
|
||||
String[] words = {"1", "eggs", "and", "2", "milk"};
|
||||
construct_children(node, words);
|
||||
System.out.println(node.getChildren());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,6 +11,10 @@ public class RecipeNode {
|
||||
this.ingredient = ingredient;
|
||||
this.portion = portion;
|
||||
}
|
||||
public RecipeNode(String name)
|
||||
{
|
||||
this.ingredient = name;
|
||||
}
|
||||
|
||||
public void setIngredient(String ingredient) {
|
||||
this.ingredient = ingredient;
|
||||
|
||||
@@ -130,5 +130,37 @@ public class RecipeTree
|
||||
}
|
||||
}
|
||||
|
||||
// public void actionAdd(RecipeNode root,RecipeNode actionNchild) {
|
||||
// if(root == null) {
|
||||
// this.root = actionNchild;
|
||||
// } else {
|
||||
// for(RecipeNode node : root.getChildren()) {
|
||||
// if(node.getPortion() != 0 || !actionNchild.getIngredient().equals(node.getIngredient())) {
|
||||
// actionAdd(node,actionNchild);
|
||||
// } else if(node.getPortion() == 0 && actionNchild.getIngredient().equals(node.getIngredient())) {
|
||||
// addNode(node, actionNchild);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public void actionAdd(RecipeNode root,RecipeNode actionNchild) {
|
||||
if(root == null) {
|
||||
this.root = actionNchild;
|
||||
} else {
|
||||
for(RecipeNode node : root.getChildren()) {
|
||||
if(node.getPortion() == 0 && node.getChildren() != null)
|
||||
actionAdd(node,actionNchild);
|
||||
else
|
||||
addNode(node,actionNchild);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
public void actionAdd(RecipeNode actionNchild)
|
||||
{
|
||||
actionAdd(root, actionNchild);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user