Advertisement
afeyajahin

Untitled

Nov 7th, 2021
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package candy;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.Set;
  9.  
  10. /**
  11.  * This program reads candies and their ingredients from a file.  It then asks
  12.  * the user which of those ingredients they like/dislike.  Finally, it rates
  13.  * the candies and tells the user which ones they would probably like.
  14.  */
  15. public class CandyRecommender {
  16.     // All known candies
  17.     private static List<Candy> candies = new ArrayList<>();
  18.    
  19.     // The ingredients the user likes
  20.     private static List<String> likes = new ArrayList<>();
  21.    
  22.     // The ingredients the user dislikes
  23.     private static List<String> dislikes = new ArrayList<>();
  24.    
  25.     // A map from ingredients to candies containing the ingredient.
  26.     private static IngredientMap ingredientMap = new IngredientMap();
  27.  
  28.     /**
  29.      * Read the candy information from the file called Candy.txt.  The file
  30.      * should be formatted like this:  candy name:ingredient 1, ingredient 2
  31.      * Initializes the candies list and the ingredientMap.
  32.      * @throws FileNotFoundException if the Candy.txt file cannot be found.
  33.      */
  34.     static void readCandyFile(String filename) throws FileNotFoundException {
  35.         try (Scanner in = new Scanner (new File (filename))) {
  36.             // Read in each candy
  37.             while (in.hasNextLine()) {
  38.                 String line = in.nextLine();
  39.                 String[] parsedLine = line.split(":");
  40.                 String name = parsedLine[0];
  41.                 String[] ingredients = parsedLine[1].split(",");
  42.                 Candy candy = new Candy( name, ingredients);
  43.                 candies.add(candy);
  44.                
  45.                 // Add the information to the ingredient map.
  46.                 for (String ingredient : ingredients) {
  47.                     ingredientMap.add(ingredient, candy);
  48.                 }
  49.             }
  50.         }
  51.         System.out.println("Read " + candies.size() +
  52.                            " candies from the file.");
  53.  
  54.     }
  55.     List<Candy> getCandy() {
  56.         return candies;
  57.            
  58.     }
  59.     /**
  60.      * Recommend candies that you think the user will like based upon their
  61.      * likes and dislikes and what the candies contain.  It will not suggest
  62.      * any candidates that contain ingredients the user dislikes.  It will
  63.      * give a score to candies that contain only ingredients the user likes.
  64.      * A higher score is better.
  65.      * @return the candies that this user would probably like
  66.      */
  67.     static Set<Candy> findLikedCandies() {
  68.         Set<Candy> possible = new HashSet<>();
  69.         for (String ingredient : likes) {
  70.             possible.addAll (ingredientMap.getCandyWith(ingredient));
  71.         }
  72.         for (String ingredient : dislikes) {
  73.             possible.removeAll (ingredientMap.getCandyWith(ingredient));
  74.         }
  75.         return possible;
  76.     }
  77.  
  78.     /**
  79.      * For each ingredient found in any candy, ask the user if they like,
  80.      * dislike, or are neutral about that ingredient.
  81.      */
  82.     private static void getUserPreferences() {
  83.         try (Scanner in = new Scanner (System.in)) {
  84.        
  85.             // Ask the user about each ingredient
  86.             for (String ingredient : ingredientMap.ingredients()) {
  87.                 System.out.print ("How much do you like " + ingredient +
  88.                                   "? (1-like, 0-ok, -1-dislike) ");
  89.                 String answer = in.nextLine();
  90.                 try {
  91.                     int pref = Integer.parseInt(answer);
  92.                     switch (pref) {
  93.                     case 1:
  94.                         likes.add(ingredient);
  95.                         break;
  96.                     case -1:
  97.                         dislikes.add(ingredient);
  98.                         break;
  99.                     default:
  100.                     }
  101.                 } catch (NumberFormatException e) {
  102.                     System.out.println("Please enter 1, 0, or -1");
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.  
  109.  
  110.     /**
  111.      * Outputs the list of candies to the screen
  112.      * @param possible the candies to output
  113.      */
  114.     private static void recommendCandies(Set<Candy> possible) {
  115.         for (Candy c: possible) {
  116.             System.out.println(c.getName() + ": " + c.score(likes));
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Reads the candies from a file, asks the user for their likes and
  122.      * dislikes, and makes a recommendation
  123.      * @param args none
  124.      */
  125.     public static void main(String[] args) {
  126.         try {
  127.             readCandyFile("Candy.txt");
  128.             getUserPreferences();
  129.             Set<Candy> likedCandies = findLikedCandies();
  130.             recommendCandies(likedCandies);
  131.         } catch (FileNotFoundException e) {
  132.             System.out.println ("Unable to read the Candy.txt file");
  133.         }
  134.     }
  135.  
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement