Skip to content

Instantly share code, notes, and snippets.

@cuneyt76
Created May 18, 2020 14:08
Show Gist options
  • Save cuneyt76/29e9b502aaa5e7ea5adbdffe11c100f0 to your computer and use it in GitHub Desktop.
Save cuneyt76/29e9b502aaa5e7ea5adbdffe11c100f0 to your computer and use it in GitHub Desktop.
Java mooc / Part 7 Recipe Search exercise
Expected the output to contain the string Pancake dough, cooking time: 15.
When the contents of the file are: Pancake dough 15 milk Meatballs 10 ground meat
Test the program with the commands: test-758359 list stop .
import java.util.ArrayList;
import java.util.List;
public class Recipe {
private String name;
private int time;
private List<String> ingr;
public Recipe(String name, int time, List<String> ingr) {
this.name = name;
this.time = time;
this.ingr = ingr;
}
public String getName() {
return this.name;
}
public int getTime() {
return this.time;
}
public List<String> getIngr() {
return this.ingr;
}
public void setName(String name) {
this.name = name;
}
public void setTime(int time) {
this.time = time;
}
public void setIngr(ArrayList<String> ingr) {
this.ingr = ingr;
}
@Override
public String toString() {
return this.name + ", cooking time: " + this.time;
}
}
Pancake dough
60
milk
egg
flour
salt
butter
Meatballs
20
ground meat
egg
breadcrumbs
Tofu rolls
30
tofu
rice
water
carrot
cucumber
avocado
wasabi
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class RecipeSearch {
public static void main(String[] args) {
ArrayList<String> recipes = new ArrayList<>(); // ALL recipes in recipe.txt
ArrayList<Recipe> recipesO = new ArrayList<>(); // recipes splitted & grouped in objects
ArrayList<Integer> indices = new ArrayList<>(); // cooking times in all recipes list
List<String> ingr; // ingredients separated by recipe
// create recipes - all lines together
try (Scanner scanner = new Scanner(Paths.get("recipes.txt"))) {
while (scanner.hasNextLine()) {
String row = scanner.nextLine();
if (row.isEmpty()) {
continue;
}
recipes.add(row);
}
}
catch (Exception e) {
System.out.println("An error occured..");
}
// find cooking times (integers) in file
for (String r: recipes) {
if (isInteger(r)) {
indices.add(recipes.indexOf(r));
}
}
// split and group recipes
for (int i = 0; i < indices.size() - 1; i++) {
int time = Integer.valueOf(recipes.get(indices.get(i)));
String name = recipes.get(indices.get(i) - 1);
recipesO.add(new Recipe(name, time, recipes.subList(indices.get(i) + 1, indices.get(i + 1) - 1)));
}
int j = indices.size() - 1;
int time = Integer.valueOf(recipes.get(indices.get(j)));
String name = recipes.get(indices.get(j) - 1);
recipesO.add(new Recipe(name, time, recipes.subList(indices.get(j) + 1, recipes.size())));
/* for (Recipe r: recipesO) {
System.out.println(r);
System.out.println(r.getIngr());
} */
System.out.println("File to read: recipes.txt");
System.out.println("");
System.out.println("Commands:");
System.out.println("list - lists the recipes");
System.out.println("stop - stops the program");
System.out.println("find name - searches recipes by name");
System.out.println("find cooking time - searches recipes by cooking time");
System.out.println("find ingredient - searches recipes by ingredient");
System.out.println("");
// accept command from user and execute command
Scanner user = new Scanner(System.in);
while (true) {
System.out.print("Enter command: ");
String command = user.nextLine();
// command "stop" implementation
if (command.equals("stop")) {
break;
}
// command "list" implementation
if (command.equals("list")) {
System.out.println("");
System.out.println("Recipes:");
for (Recipe r: recipesO) {
System.out.println(r);
}
System.out.println("");
}
// command "find name" implementation
if (command.equals("find name")) {
System.out.print("Searched word: ");
String search = user.nextLine();
System.out.println("");
System.out.println("Recipes:");
for (Recipe r: recipesO) {
if (r.getName().contains(search)) {
System.out.println(r);
}
}
System.out.println("");
}
// command "find cooking time" implementation
if (command.equals("find cooking time")) {
System.out.print("Max cooking time: ");
int maxTime = Integer.valueOf(user.nextLine());
System.out.println("");
System.out.println("Recipes:");
for (Recipe r: recipesO) {
if (r.getTime() <= maxTime) {
System.out.println(r);
}
}
System.out.println("");
}
// command "find ingredient" implementation
if (command.equals("find ingredient")) {
System.out.print("Ingredient: ");
String ingredient = user.nextLine();
System.out.println("");
System.out.println("Recipes:");
for (Recipe r: recipesO) {
if (r.getIngr().contains(ingredient)) {
System.out.println(r);
}
}
System.out.println("");
}
}
}
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
return true;
}
catch (Exception e) {
return false;
}
}
}
@singfungcheung
Copy link

singfungcheung commented Jan 5, 2021

nice code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment