Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Santa {
- private static Map<String, List<Wishes>> children = new HashMap<>();
- static class Wishes {
- private String itemName;
- private String childName;
- private double itemPrice;
- Wishes(String itemName, String childName, double itemPrice) {
- this.itemName = itemName;
- this.childName = childName;
- this.itemPrice = itemPrice;
- }
- String getItemName() {
- return itemName;
- }
- @Override
- public String toString() {
- return String.format("{%s;%s;%.2f}", itemName, childName, itemPrice);
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int numberOfCommands = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < numberOfCommands; i++) {
- String input = scanner.nextLine();
- String[] commands = input.split(" ");
- String command = commands[0];
- String[] commandParameters = input.substring(commands[0].length() + 1).split(";");
- switch (command) {
- case "AddWish":
- addWish(commandParameters);
- break;
- case "DeleteWishes":
- deleteWish(commandParameters);
- break;
- case "FindWishesByPriceRange":
- findWishesByPriceRange(commandParameters);
- break;
- case "FindWishesByChild":
- findWishesByChild(commandParameters);
- break;
- }
- }
- }
- private static void addWish(String[] command) {
- String itemName = command[0];
- double price = Double.parseDouble(command[1]);
- String childName = command[2];
- Wishes wish = new Wishes(itemName, childName, price);
- if (!children.containsKey(wish.childName)) {
- children.put(wish.childName, new ArrayList<>());
- }
- children.get(wish.childName).add(wish);
- System.out.println("Wish added");
- }
- private static void deleteWish(String[] command) {
- String childName = command[0];
- if (!children.containsKey(childName)) {
- System.out.println("No Wishes found");
- return;
- }
- int wishesCount = children.get(childName).size();
- children.remove(childName);
- System.out.printf("%d Wishes deleted", wishesCount);
- System.out.println();
- }
- private static void findWishesByChild(String[] command) {
- String childName = command[0];
- if (!children.containsKey(childName)) {
- System.out.println("No Wishes found");
- } else {
- StringBuilder strBuilder = new StringBuilder();
- children.get(childName)
- .stream()
- .sorted(Comparator.comparing(Wishes::getItemName))
- .forEach(wish -> strBuilder.append(wish).append(System.lineSeparator()));
- System.out.println(strBuilder.toString().trim());
- }
- }
- private static void findWishesByPriceRange(String[] command) {
- double fromPrice = Double.parseDouble(command[0]);
- double toPrice = Double.parseDouble(command[1]);
- StringBuilder strBuilder = new StringBuilder();
- if (children.isEmpty()) {
- System.out.println("No Wishes found");
- return;
- }
- children.values().stream()
- .flatMap(Collection::stream)
- .filter(wish -> !(wish.itemPrice < fromPrice) && !(wish.itemPrice > toPrice))
- .sorted(Comparator.comparing(Santa.Wishes::getItemName))
- .forEach(wish -> strBuilder.append(wish).append(System.lineSeparator()));
- if (strBuilder.length() < 1) {
- System.out.println("No Wishes found");
- return;
- }
- System.out.println(strBuilder.toString().trim());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement