Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import static java.lang.Double.parseDouble;
- import static java.lang.Integer.parseInt;
- public class Problem3_SantaHelper {
- private static Map<String, List<Wish>> santa_map = new HashMap<>();
- private static final String WISH_ADDED_MSG = "Wish added";
- private static final String WISHES_DELETED_MSG = "%d Wishes deleted";
- private static final String NO_WISHES_FOUND = "No Wishes found";
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int commandCount = parseInt(scanner.nextLine());
- String[] input;
- String command;
- String[] params;
- for (int i = 0; i < commandCount; i++) {
- input = scanner.nextLine().split(" ", 2);
- command = input[0];
- params = input[1].split(";");
- switch (command) {
- case "AddWish":
- addWish(params);
- break;
- case "DeleteWishes":
- deleteWishes(params);
- break;
- case "FindWishesByPriceRange":
- findWishesByPriceRange(params);
- break;
- case "FindWishesByChild":
- findWishesByChild(params);
- break;
- }
- }
- }
- //Find Wishes
- private static void findWishesByPriceRange(String[] params) {
- List<Wish> found = new ArrayList<>();
- int countOfWishesFound = 0;
- double from = parseDouble(params[0]);
- double to = parseDouble(params[1]);
- for (Map.Entry<String, List<Wish>> entry : santa_map.entrySet()) {
- for (Wish wish : entry.getValue()) {
- if (wish.getItemPrice() >= from && wish.getItemPrice() <= to) {
- countOfWishesFound++;
- found.add(wish);
- }
- }
- }
- if (countOfWishesFound == 0) {
- System.out.println(NO_WISHES_FOUND);
- } else {
- found.stream().sorted(Comparator.comparing(Wish::getItemName)).forEach(System.out::print);
- }
- }
- private static void findWishesByChild(String[] params) {
- String childName = params[0];
- if (santa_map.containsKey(childName)) {
- System.out.println(Wish.printWishes(santa_map.get(childName)));
- } else {
- System.out.println(NO_WISHES_FOUND);
- }
- }
- //Delete Wish
- private static void deleteWishes(String[] params) {
- String childName = params[0]; //child's wishes to find
- if (santa_map.containsKey(childName)) {
- int removedListSize = santa_map.remove(childName).size(); //removes and returns removed list
- System.out.println(String.format(WISHES_DELETED_MSG, removedListSize));
- } else {
- System.out.println(NO_WISHES_FOUND);
- }
- }
- //Add Wish
- private static void addWish(String[] params) {
- String itemName = params[0]; //item name from input
- Double price = parseDouble(params[1]); //price from input
- String childName = params[2]; //child name input
- santa_map.putIfAbsent(childName, new ArrayList<>());
- santa_map.get(childName).add(new Wish(childName, itemName, price));
- System.out.println(WISH_ADDED_MSG);
- }
- //Class Wish
- static class Wish {
- private String childName;
- private String itemName;
- private Double itemPrice;
- Wish(String name, String item, Double price) {
- this.childName = name;
- this.itemName = item;
- this.itemPrice = price;
- }
- String getChildName() {
- return childName;
- }
- String getItemName() {
- return itemName;
- }
- Double getItemPrice() {
- return itemPrice;
- }
- static String printWishes(List<Wish> wishes) {
- StringBuilder strBuilder = new StringBuilder();
- wishes
- .stream()
- .sorted(Comparator.comparing(Wish::getItemName))
- .forEach(x1 -> strBuilder.append(x1).append(System.lineSeparator()));
- return strBuilder.toString().trim();
- }
- @Override
- public String toString() {
- return String.format("{%s;%s;%.2f}\n", this.getItemName(), this.getChildName(), this.getItemPrice());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement