Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. //Spencer Sullivan-Hayes
  2. //2/9/16
  3. //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
  4.  
  5. import java.util.Scanner;
  6. import java.io.PrintWriter;
  7. //import java.io.File;
  8. //import java.io.FileNotFoundException;
  9. import java.io.*;
  10.  
  11.  
  12. public class RecipeNShopping
  13. {
  14.     static Scanner stapletextIn;
  15.     static Scanner textIn = null;
  16.     static PrintWriter pw = null;
  17.  
  18.         static Scanner userIn = new Scanner(System.in);
  19.         String[] recipes = new String[15];
  20.  
  21.         public static void main(String[]args)
  22.         {
  23.             RecipeNShopping rns = new RecipeNShopping();
  24.             rns.initializer();
  25.             rns.userRecipeNames();
  26.             rns.recipeSearch();
  27.             rns.stapleAdder();
  28.             pw.close();
  29.         }
  30.  
  31.         public void initializer()
  32.         {
  33.                // try
  34.                // {
  35.                         textIn = new Scanner("Recipes.txt");
  36.                // }
  37.               //  catch(FileNotFoundException e)
  38.                // {
  39.                 //        System.out.println("Can't find Recipes file");
  40.                //         System.exit(1);
  41.                // }
  42.               //  try
  43.                // {
  44.                         stapletextIn = new Scanner("Staples.txt");
  45.               //  }
  46.                // catch(FileNotFoundException e)
  47.               //  {
  48.                //         System.out.println("Can't find Staples file");
  49.                //         System.exit(1);
  50.               //  }
  51.                 try
  52.                 {
  53.                         pw = new PrintWriter(new FileWriter("ShoppingList.txt", true));
  54.                 }
  55.                 catch(IOException e)
  56.                 {
  57.                         System.out.println("Can't find Recipes file");
  58.                         System.exit(1);
  59.                 }
  60.                System.out.println("lol yo shits initialized"); //dlete me
  61.         }
  62.  
  63.         public void userRecipeNames()
  64.         {
  65.                 int c = 0;
  66.                 String input;
  67.                 System.out.println("Please type the names of your requested recipies, one at a time. Type QUIT when you're done.");
  68.  
  69.         do{
  70.                 input = userIn.nextLine();
  71.                 recipes[c] = input;
  72.                 c++;
  73.         } while(!input.equals("QUIT"));
  74.        
  75.         }
  76.            
  77.         public void recipeSearch()
  78.         {
  79.                 RecipeNShopping rns = new RecipeNShopping();
  80.                 int x = 0;
  81.                 while(!recipes[x].equals("QUIT"))
  82.                         {
  83.                                 rns.ingredientFinder(recipes[x]);
  84.                                 x++;
  85.                         }
  86.  
  87.         }
  88.  
  89.         public void ingredientFinder(String recipe)
  90.         {
  91.                 RecipeNShopping rns = new RecipeNShopping();
  92.                 String currentline;
  93.                 String recipeName;
  94.                 Boolean foundRecipe = false;
  95.                
  96.                 while(textIn.hasNextLine())
  97.                     {
  98.                     currentline = textIn.nextLine();
  99.                     System.out.println(currentline);
  100.                     if(currentline.indexOf(' ') != -1)
  101.                         if(currentline.substring(0, currentline.indexOf(' ')).equals("Recipe:"))
  102.                         {
  103.                             recipeName = currentline.substring(currentline.indexOf(" "), currentline.indexOf("-")).trim();
  104.                             if(recipeName.equals(recipe.trim()))
  105.                             {
  106.                                 do{
  107.                                 currentline = textIn.nextLine();
  108.                                 }while(!currentline.trim().equals("Ingredients:"));
  109.                                
  110.                                 do{
  111.                                     currentline = textIn.nextLine();
  112.                                     rns.addToList(currentline);
  113.                                 }while(!currentline.trim().equals(""));
  114.                                
  115.                                 foundRecipe = true;
  116.                             }
  117.                             else
  118.                             {}
  119.                     }
  120.                     else
  121.                     {}
  122.                     }
  123.                 if(foundRecipe == false)
  124.                     rns.addToList(recipe + "(1)");
  125.                
  126.         }
  127.      
  128.         public void stapleAdder()
  129.         {
  130.             RecipeNShopping rns = new RecipeNShopping();
  131.             String stapleLine;
  132.            
  133.             while(stapletextIn.hasNext())
  134.             {
  135.             stapleLine = stapletextIn.nextLine();
  136.             rns.addToList(stapleLine);
  137.             }
  138.         }
  139.        
  140.         public void addToList(String ingredLine)
  141.         {
  142.             pw.println(ingredLine);
  143.         }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement