Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class PlantDiscovery {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int number = Integer.parseInt(sc.nextLine());
- List<Plant> plants = new ArrayList<>();
- while (number > 0){
- String[] nextPlant = sc.nextLine().split("<->");
- String name = nextPlant[0];
- int rarity = Integer.parseInt(nextPlant[1]);
- if (containsPlant(plants, name)){
- Plant x = getPlant(plants, name);
- x.rarity = rarity;
- }
- else {
- plants.add(new Plant(name, rarity));
- }
- number--;
- }
- String command;
- while (!"Exhibition".equals(command = sc.nextLine())){
- String[] token = command.split(": | - ");
- String task = token[0];
- String name = token[1];
- int rarity;
- double rating;
- if (task.equals("Rate") && containsPlant(plants, name)){
- rating = Double.parseDouble(token[2]);
- getPlant(plants, name).ratings.add(rating);
- }
- else if (task.equals("Update") && containsPlant(plants, name)){
- rarity = Integer.parseInt(token[2]);
- getPlant(plants, name).rarity = rarity;
- }
- else if (task.equals("Reset") && containsPlant(plants, name)){
- getPlant(plants, name).ratings.clear();
- }
- else {
- System.out.println("error");
- }
- }
- System.out.println("Plants for exhibition:");
- plants.stream()
- .sorted(Comparator.comparing(Plant::getAverageRating).reversed())
- .sorted(Comparator.comparing(Plant::getRarity).reversed())
- .forEach(plant -> System.out.printf("- %s; Rarity: %d; Rating: %.2f%n", plant.name, plant.rarity, plant.getAverageRating()));
- }
- private static boolean containsPlant(List<Plant> plants, String name){
- for (Plant plant : plants){
- if (name.equals(plant.name)){
- return true;
- }
- }
- return false;
- }
- private static Plant getPlant(List<Plant> plants, String name){
- return plants.stream().filter(o -> o.name.equals(name)).findFirst().get();
- }
- static class Plant{
- private final String name;
- private int rarity;
- private final List<Double> ratings;
- public Plant(String name, int rarity){
- this.name = name;
- this.rarity = rarity;
- this.ratings = new ArrayList<>();
- }
- public double getAverageRating(){
- return this.ratings.stream().collect(Collectors.averagingDouble(Double::doubleValue));
- }
- public int getRarity(){
- return this.rarity;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment