My brain hurts :D
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,22 +1,28 @@
|
|||||||
import java.awt.image.AreaAveragingScaleFilter;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class RecipeInstructions {
|
public class RecipeInstructions
|
||||||
public static void main(String[] args) {
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
int usrChoice;
|
int usrChoice;
|
||||||
|
|
||||||
ArrayList<RecipeNode> ingredients = new ArrayList<>();
|
ArrayList<RecipeNode> ingredients = new ArrayList<>();
|
||||||
RecipeTree fullInstructions = new RecipeTree();
|
RecipeTree fullInstructions = new RecipeTree();
|
||||||
|
|
||||||
do {
|
do
|
||||||
|
{
|
||||||
Scanner userOption = new Scanner(System.in);
|
Scanner userOption = new Scanner(System.in);
|
||||||
|
|
||||||
System.out.println("What would you like to do?\n1.Add Ingredient\n2.Mix");
|
System.out.println("What would you like to do?\n1.Add Ingredient\n2.Mix");
|
||||||
usrChoice = userOption.nextInt();
|
usrChoice = userOption.nextInt();
|
||||||
|
|
||||||
// Switch case for user's choice
|
// Switch case for user's choice
|
||||||
switch (usrChoice) {
|
switch (usrChoice)
|
||||||
|
{
|
||||||
case 1:
|
case 1:
|
||||||
// Init 3 scanners because usually scanners crash on me with multiple input types lmao
|
// Init 3 scanners because usually scanners crash on me with multiple input types lmao
|
||||||
Scanner ingredientAddAmount = new Scanner(System.in); // Init Scanner for Added amount
|
Scanner ingredientAddAmount = new Scanner(System.in); // Init Scanner for Added amount
|
||||||
@@ -26,7 +32,8 @@ public class RecipeInstructions {
|
|||||||
System.out.print("Enter how many ingredients you're adding: ");
|
System.out.print("Enter how many ingredients you're adding: ");
|
||||||
int amountIngredient = ingredientAddAmount.nextInt(); // Defining how many ingredients will be added
|
int amountIngredient = ingredientAddAmount.nextInt(); // Defining how many ingredients will be added
|
||||||
|
|
||||||
for(int i = 0; i < amountIngredient; i++) {
|
for (int i = 0; i < amountIngredient; i++)
|
||||||
|
{
|
||||||
// Getting Ingredient Name
|
// Getting Ingredient Name
|
||||||
System.out.println("Enter ingredient Name");
|
System.out.println("Enter ingredient Name");
|
||||||
String ingredientName = ingredientAddName.nextLine();
|
String ingredientName = ingredientAddName.nextLine();
|
||||||
@@ -38,7 +45,8 @@ public class RecipeInstructions {
|
|||||||
// Creating the Ingredient's node and putting it into array list
|
// Creating the Ingredient's node and putting it into array list
|
||||||
// for safe keeping and later use
|
// for safe keeping and later use
|
||||||
RecipeNode newIngredient = new RecipeNode(ingredientName, ingredientPortion);
|
RecipeNode newIngredient = new RecipeNode(ingredientName, ingredientPortion);
|
||||||
fullInstructions.addNode((RecipeNode) null, newIngredient);
|
// fullInstructions.addNode((RecipeNode) null, newIngredient);
|
||||||
|
ingredients.add(newIngredient);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
@@ -46,12 +54,15 @@ public class RecipeInstructions {
|
|||||||
String userIngredients; // String poo
|
String userIngredients; // String poo
|
||||||
ArrayList<RecipeNode> bowl = new ArrayList<>(); // Mixing bowl for ingredients obviously
|
ArrayList<RecipeNode> bowl = new ArrayList<>(); // Mixing bowl for ingredients obviously
|
||||||
|
|
||||||
do {
|
do
|
||||||
|
{
|
||||||
System.out.println("Enter ingredient names to be mixed (type done to exit)");
|
System.out.println("Enter ingredient names to be mixed (type done to exit)");
|
||||||
userIngredients = ingredientNames.nextLine();
|
userIngredients = ingredientNames.nextLine();
|
||||||
|
|
||||||
for(RecipeNode ingredient : ingredients) {
|
for (RecipeNode ingredient : ingredients)
|
||||||
if(userIngredients.equals(ingredient.getIngredient())) {
|
{
|
||||||
|
if (userIngredients.equals(ingredient.getIngredient()))
|
||||||
|
{
|
||||||
bowl.add(ingredient);
|
bowl.add(ingredient);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -61,7 +72,8 @@ public class RecipeInstructions {
|
|||||||
RecipeNode mixture = new RecipeNode(bowl.get(0) + " mix", 69);
|
RecipeNode mixture = new RecipeNode(bowl.get(0) + " mix", 69);
|
||||||
ingredients.add(mixture);
|
ingredients.add(mixture);
|
||||||
|
|
||||||
for(RecipeNode childIngredient : bowl) {
|
for (RecipeNode childIngredient : bowl)
|
||||||
|
{
|
||||||
mixture.addChild(childIngredient);
|
mixture.addChild(childIngredient);
|
||||||
childIngredient.parent = mixture;
|
childIngredient.parent = mixture;
|
||||||
}
|
}
|
||||||
@@ -69,4 +81,98 @@ public class RecipeInstructions {
|
|||||||
}
|
}
|
||||||
} while (usrChoice != -1);
|
} while (usrChoice != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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()
|
||||||
|
{
|
||||||
|
//New tree who dis
|
||||||
|
RecipeTree fullInstructions = new RecipeTree();
|
||||||
|
//Stinky Scanner please remove or something idk
|
||||||
|
//Maybe make the method take a string in or somethiong :D
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
|
String userInput;
|
||||||
|
//Need to not hardcode verbs maybe a global variable for it? etc etc
|
||||||
|
String[] verbs = {"add", "mix"};
|
||||||
|
//init this shit
|
||||||
|
int currentNumber = 0;
|
||||||
|
String currentIngredient = null;
|
||||||
|
//Useless sout lol
|
||||||
|
System.out.println("Enter instruction: ");
|
||||||
|
// userInput = input.nextLine();
|
||||||
|
//this was simulating user input for the tests below
|
||||||
|
userInput = "mix 3 eggs and 2 milk";
|
||||||
|
//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, userInput))
|
||||||
|
{
|
||||||
|
//get Action
|
||||||
|
RecipeNode newAction = new RecipeNode(verb, 0);
|
||||||
|
//Split after verb
|
||||||
|
//this is where we split the string
|
||||||
|
String[] split = userInput.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.addNode((RecipeNode) null, newAction);
|
||||||
|
}
|
||||||
|
fullInstructions.printTree();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//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()
|
||||||
|
{
|
||||||
|
RecipeCreation();
|
||||||
|
}
|
||||||
|
@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.ingredient = ingredient;
|
||||||
this.portion = portion;
|
this.portion = portion;
|
||||||
}
|
}
|
||||||
|
public RecipeNode(String name)
|
||||||
|
{
|
||||||
|
this.ingredient = name;
|
||||||
|
}
|
||||||
|
|
||||||
public void setIngredient(String ingredient) {
|
public void setIngredient(String ingredient) {
|
||||||
this.ingredient = ingredient;
|
this.ingredient = ingredient;
|
||||||
|
|||||||
Reference in New Issue
Block a user