Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Santa {
- private static void testInput() {
- String input = "10\n" +
- "AddWish Electric Scooter 2000Z;1536.50;Stefan Petrov\n" +
- "AddWish Fortnite Skin;3000;Stefan\n" +
- "AddWish AMD Radeon;4099.99;Pesho\n" +
- "AddWish Apple AirPods;200000;Kiril\n" +
- "AddWish Socks;10000;Tosho\n" +
- "AddWish Swater;999;Stefan\n" +
- "FindWishesByChild Stefan Gosho\n" +
- "DeleteWishes Stefan Petrov\n" +
- "FindWishesByChild Stefan\n" +
- "FindWishesByPriceRange 100000;200000";
- String input2 = "3\n" +
- "DeleteWishes BBB\n" +
- "FindWishesByChild XXX\n" +
- "FindWishesByPriceRange 0;10000\n";
- System.setIn(new ByteArrayInputStream(input.getBytes()));
- }
- public static class Wish {
- String itemName;
- double price;
- String childName;
- Wish(String itemName, double price, String childName) {
- this.itemName = itemName;
- this.price = price;
- this.childName = childName;
- }
- @Override
- public String toString() {
- return String.format(Locale.ROOT, "||%s, %.2f, %s||", itemName, price, childName);
- }
- }
- private static List<Wish> wishesList = new ArrayList<>();
- private static Map<String, List<Wish>> mapByChildren = new HashMap<>();
- public static void main(String[] args) throws IOException {
- testInput();
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int numberOfCommands = Integer.parseInt(reader.readLine());
- for (int i = 0; i < numberOfCommands; i++) {
- String[] command = reader.readLine().split(" ");
- switch (command[0]) {
- case "AddWish":
- addWish(command);
- break;
- case "DeleteWishes":
- deleteWishes(command);
- break;
- case "FindWishesByChild":
- findWishesByChild(command);
- break;
- case "FindWishesByPriceRange":
- findWishesByPriceRange(command);
- break;
- }
- }
- }
- private static void findWishesByPriceRange(String[] partialCommand) {
- String join = String.join(" ", partialCommand);
- join = join.replace("FindWishesByPriceRange ", "");
- String[] command = join.split(";");
- double fromPrice = Double.parseDouble(command[0]);
- double toPrice = Double.parseDouble(command[1]);
- List<Wish> list = new ArrayList<>();
- for (Wish value : wishesList) {
- if (value.price >= fromPrice && value.price <= toPrice) {
- list.add(value);
- }
- }
- if (list.size() == 0) {
- System.out.println("No Wishes found");
- } else {
- list.sort(Comparator.comparing(o -> o.itemName));
- for (Wish wish : list) {
- System.out.println(String.format(Locale.ROOT, "{%s;%s;%.2f}", wish.itemName, wish.childName, wish.price));
- }
- }
- }
- private static void findWishesByChild(String[] command) {
- String name;
- if (command.length == 2) {
- name = command[1];
- } else {
- name = command[1] + " " + command[2];
- }
- if (mapByChildren.get(name) == null) {
- System.out.println("No Wishes found");
- } else {
- List<Wish> list = new ArrayList<>(mapByChildren.get(name));
- list.sort(Comparator.comparing(o -> o.itemName));
- for (Wish wish : list) {
- System.out.println(String.format(Locale.ROOT, "{%s;%s;%.2f}", wish.itemName, wish.childName, wish.price));
- }
- }
- }
- private static void deleteWishes(String[] command) {
- String name;
- if (command.length == 2) {
- name = command[1];
- } else {
- name = command[1] + " " + command[2];
- }
- if (mapByChildren.get(name) == null) {
- System.out.println("No Wishes found");
- return;
- }
- System.out.println(String.format("%d Wishes deleted", mapByChildren.get(name).size()));
- mapByChildren.remove(name);
- for (int i = 0; i < wishesList.size(); i++) {
- if (wishesList.get(i).childName.equals(name)) {
- wishesList.remove(i);
- i--;
- }
- }
- }
- private static void addWish(String[] partialCommand) {
- String join = String.join(" ", partialCommand);
- join = join.replace("AddWish ", "");
- String[] command = join.split(";");
- Wish wish = new Wish(command[0], Double.parseDouble(command[1]), command[2]);
- List<Wish> wishes = mapByChildren.getOrDefault(wish.childName, new ArrayList<>());
- wishes.add(wish);
- mapByChildren.put(wish.childName, wishes);
- wishesList.add(wish);
- System.out.println("Wish added");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement