Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.ByteArrayInputStream;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class SantasHelper {
- static List<Wish> wishes = new ArrayList<>();
- public static void testInput() {
- String input = "8\n" +
- "AddWish Electric Scooter 2000Z;3500.05;Rayko Petrov\n" +
- "AddWish Fortnite Skin;3000;Rayko Petrov\n" +
- "AddWish AMD Radeon;16400;Hristo\n" +
- "AddWish Apple AirPods;21111.50;Nadya\n" +
- "FindWishesByChild Rayko Petrov\n" +
- "DeleteWishes Rayko Petrov\n" +
- "FindWishesByChild Rayko Petrov\n" +
- "FindWishesByPriceRange 5000;30000";
- System.setIn(new ByteArrayInputStream(input.getBytes()));
- }
- public static void main(String[] args) {
- testInput();
- Scanner scanner = new Scanner(System.in);
- int commandsCount = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < commandsCount; i++) {
- String input = scanner.nextLine();
- executeCommand(input);
- }
- }
- private static void executeCommand(String input) {
- int index = input.indexOf(" ");
- String command = input.substring(0, index);
- String remainder = input.substring(index + 1);
- switch (command) {
- case "AddWish":
- addWishCommand(remainder);
- break;
- case "FindWishesByChild":
- findWishesByChild(remainder);
- break;
- case "DeleteWishes":
- deleteWishes(remainder);
- break;
- case "FindWishesByPriceRange":
- findWishesByPriceRange(remainder);
- break;
- default:
- }
- }
- private static void addWishCommand(String remainder) {
- String[] parameters = remainder.split(";");
- Wish wish = new Wish(parameters[0], parameters[2], Double.parseDouble(parameters[1]));
- wishes.add(wish);
- System.out.println("Wish added");
- }
- private static void deleteWishes(String remainder) {
- String name = remainder.trim();
- List<Wish> wishesLeft =
- wishes.stream().filter(wish -> !name.equals(wish.childName)).collect(Collectors.toList());
- int count = wishes.size() - wishesLeft.size();
- wishes = wishesLeft;
- if (count <= 0) {
- System.out.println("No Wishes deleted");
- } else {
- System.out.printf("%d Wishes deleted%n", count);
- }
- }
- private static void findWishesByPriceRange(String remainder) {
- String[] prices = remainder.split(";");
- double minPrice = Double.parseDouble(prices[0]);
- double maxPrice = Double.parseDouble(prices[1]);
- List<Wish> withinPriceRange =
- wishes.stream().filter(wish -> wish.price >= minPrice && wish.price <= maxPrice)
- .sorted(Comparator.comparing(Wish::toString))
- .collect(Collectors.toList());
- printWishes(withinPriceRange);
- }
- private static void findWishesByChild(String remainder) {
- String name = remainder.trim();
- List<Wish> wishesByChild = wishes.stream().filter(wish -> name.equals(wish.childName)).
- sorted(Comparator.comparing(Wish::toString))
- .collect(Collectors.toList());
- printWishes(wishesByChild);
- }
- private static void printWishes(List<Wish> wishesLeft) {
- if (wishesLeft.size() <= 0) {
- System.out.println("No Wishes found");
- } else {
- wishesLeft.forEach(wish -> System.out.println(wish.toString()));
- }
- }
- static class Wish {
- private String itemName;
- private String childName;
- private double price;
- public Wish(String itemName, String childName, double price) {
- this.itemName = itemName;
- this.childName = childName;
- this.price = price;
- }
- @Override
- public String toString() {
- return String.format("{%s;%s;%.2f}", itemName, childName, price);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement