Yargi

PlantDiscovery2

Aug 9th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class PlantDiscovery {
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         int number = Integer.parseInt(sc.nextLine());
  13.  
  14.         List<Plant> plants = new ArrayList<>();
  15.        
  16.         while (number > 0){
  17.  
  18.             String[] nextPlant = sc.nextLine().split("<->");
  19.             String name = nextPlant[0];
  20.             int rarity = Integer.parseInt(nextPlant[1]);
  21.  
  22.             if (containsPlant(plants, name)){
  23.  
  24.                 Plant x = getPlant(plants, name);
  25.                 x.rarity = rarity;
  26.             }
  27.             else {
  28.  
  29.                 plants.add(new Plant(name, rarity));
  30.             }
  31.             number--;
  32.         }
  33.  
  34.         String command;
  35.         while (!"Exhibition".equals(command = sc.nextLine())){
  36.  
  37.             String[] token = command.split(": | - ");
  38.             String task = token[0];
  39.             String name = token[1];
  40.             int rarity;
  41.             double rating;
  42.  
  43.             if (task.equals("Rate") && containsPlant(plants, name)){
  44.  
  45.                 rating = Double.parseDouble(token[2]);
  46.                 getPlant(plants, name).ratings.add(rating);
  47.             }
  48.             else if (task.equals("Update") && containsPlant(plants, name)){
  49.  
  50.                 rarity = Integer.parseInt(token[2]);
  51.                 getPlant(plants, name).rarity = rarity;
  52.             }
  53.             else if (task.equals("Reset") && containsPlant(plants, name)){
  54.  
  55.                 getPlant(plants, name).ratings.clear();
  56.             }
  57.             else {
  58.  
  59.                 System.out.println("error");
  60.             }
  61.         }
  62.  
  63.         System.out.println("Plants for exhibition:");
  64.         plants.stream()
  65.                 .sorted(Comparator.comparing(Plant::getAverageRating).reversed())
  66.                 .sorted(Comparator.comparing(Plant::getRarity).reversed())
  67.                 .forEach(plant -> System.out.printf("- %s; Rarity: %d; Rating: %.2f%n", plant.name, plant.rarity, plant.getAverageRating()));
  68.     }
  69.  
  70.     private static boolean containsPlant(List<Plant> plants, String name){
  71.  
  72.         for (Plant plant : plants){
  73.  
  74.             if (name.equals(plant.name)){
  75.  
  76.                 return true;
  77.             }
  78.         }
  79.         return false;
  80.     }
  81.  
  82.     private static Plant getPlant(List<Plant> plants, String name){
  83.  
  84.         return plants.stream().filter(o -> o.name.equals(name)).findFirst().get();
  85.     }
  86.  
  87.     static class Plant{
  88.  
  89.         private final String name;
  90.         private int rarity;
  91.         private final List<Double> ratings;
  92.  
  93.         public Plant(String name, int rarity){
  94.  
  95.             this.name = name;
  96.             this.rarity = rarity;
  97.             this.ratings = new ArrayList<>();
  98.         }
  99.  
  100.         public double getAverageRating(){
  101.  
  102.             return this.ratings.stream().collect(Collectors.averagingDouble(Double::doubleValue));
  103.         }
  104.  
  105.         public int getRarity(){
  106.  
  107.             return this.rarity;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment