Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Spencer Sullivan-Hayes
- //2/9/16
- //This program will take recipe names from the user, reference a recipe file to find the corresponding ingredients, and add them to a shopping list along with weekly staples
- import java.util.Scanner;
- import java.io.PrintWriter;
- //import java.io.File;
- //import java.io.FileNotFoundException;
- import java.io.*;
- public class RecipeNShopping
- {
- static Scanner stapletextIn;
- static Scanner textIn = null;
- static PrintWriter pw = null;
- static Scanner userIn = new Scanner(System.in);
- String[] recipes = new String[15];
- public static void main(String[]args)
- {
- RecipeNShopping rns = new RecipeNShopping();
- rns.initializer();
- rns.userRecipeNames();
- rns.recipeSearch();
- rns.stapleAdder();
- pw.close();
- }
- public void initializer()
- {
- // try
- // {
- textIn = new Scanner("Recipes.txt");
- // }
- // catch(FileNotFoundException e)
- // {
- // System.out.println("Can't find Recipes file");
- // System.exit(1);
- // }
- // try
- // {
- stapletextIn = new Scanner("Staples.txt");
- // }
- // catch(FileNotFoundException e)
- // {
- // System.out.println("Can't find Staples file");
- // System.exit(1);
- // }
- try
- {
- pw = new PrintWriter(new FileWriter("ShoppingList.txt", true));
- }
- catch(IOException e)
- {
- System.out.println("Can't find Recipes file");
- System.exit(1);
- }
- System.out.println("lol yo shits initialized"); //dlete me
- }
- public void userRecipeNames()
- {
- int c = 0;
- String input;
- System.out.println("Please type the names of your requested recipies, one at a time. Type QUIT when you're done.");
- do{
- input = userIn.nextLine();
- recipes[c] = input;
- c++;
- } while(!input.equals("QUIT"));
- }
- public void recipeSearch()
- {
- RecipeNShopping rns = new RecipeNShopping();
- int x = 0;
- while(!recipes[x].equals("QUIT"))
- {
- rns.ingredientFinder(recipes[x]);
- x++;
- }
- }
- public void ingredientFinder(String recipe)
- {
- RecipeNShopping rns = new RecipeNShopping();
- String currentline;
- String recipeName;
- Boolean foundRecipe = false;
- while(textIn.hasNextLine())
- {
- currentline = textIn.nextLine();
- System.out.println(currentline);
- if(currentline.indexOf(' ') != -1)
- if(currentline.substring(0, currentline.indexOf(' ')).equals("Recipe:"))
- {
- recipeName = currentline.substring(currentline.indexOf(" "), currentline.indexOf("-")).trim();
- if(recipeName.equals(recipe.trim()))
- {
- do{
- currentline = textIn.nextLine();
- }while(!currentline.trim().equals("Ingredients:"));
- do{
- currentline = textIn.nextLine();
- rns.addToList(currentline);
- }while(!currentline.trim().equals(""));
- foundRecipe = true;
- }
- else
- {}
- }
- else
- {}
- }
- if(foundRecipe == false)
- rns.addToList(recipe + "(1)");
- }
- public void stapleAdder()
- {
- RecipeNShopping rns = new RecipeNShopping();
- String stapleLine;
- while(stapletextIn.hasNext())
- {
- stapleLine = stapletextIn.nextLine();
- rns.addToList(stapleLine);
- }
- }
- public void addToList(String ingredLine)
- {
- pw.println(ingredLine);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement