Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class Demo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> materials = Arrays
- .stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt)
- .collect(Collectors.toList());
- List<Integer> magic = Arrays
- .stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt)
- .collect(Collectors.toList());
- Map<String, Integer> toys = new TreeMap<>();
- while (!materials.isEmpty() && !magic.isEmpty()) {
- int materialsForCrafting = materials.get(materials.size() - 1);
- int magicForCrafting = magic.get(0);
- int craftedToysValue = materialsForCrafting * magicForCrafting;
- if (craftedToysValue < 0) {
- int newMaterial = (magicForCrafting + materialsForCrafting);
- removeParts(materials,magic);
- materials.add(newMaterial);
- continue;
- }
- if (materialsForCrafting == 0) {
- materials.remove(materials.get(materials.size()-1));
- continue;
- }
- if (magicForCrafting == 0) {
- magic.remove(magic.remove(0));
- continue;
- }
- switch (craftedToysValue) {
- case 150:
- addToys(toys, "Doll");
- removeParts(materials, magic);
- break;
- case 250:
- addToys(toys, "Wooden Train");
- removeParts(materials, magic);
- break;
- case 300:
- addToys(toys, "Teddy bear");
- removeParts(materials, magic);
- break;
- case 400:
- addToys(toys, "Bicycle");
- removeParts(materials, magic);
- break;
- default:
- magic.remove(magic.get(0));
- int newMatForAdding = materialsForCrafting +15;
- materials.remove(materials.get(materials.size()-1));
- materials.add(newMatForAdding);
- //• If the product doesn’t equal one of the magic levels in the table and is a positive number,
- // remove only the magic value and increase the material value with 15.
- break;
- }
- }
- if (successValidation(toys)) {
- System.out.println("The presents are crafted! Merry Christmas!");
- } else {
- System.out.println("No presents this Christmas!");
- }
- if (!materials.isEmpty()) {
- System.out.print("Materials left: ");
- for (int i = materials.size()-1; i >= 0 ; i--) {
- if(i != 0){
- System.out.print(materials.get(i) + ", ");
- }else{
- System.out.print(materials.get(i) );
- }
- }
- System.out.println();
- }
- if (!magic.isEmpty()) {
- }
- toys.entrySet()
- .stream()
- .forEach(e -> System.out.printf("%s: %d%n", e.getKey(), e.getValue()));
- }
- private static void removeParts(List<Integer> materials, List<Integer> magic) {
- magic.remove(0);
- materials.remove(materials.size() - 1);
- }
- private static void addToys(Map<String, Integer> toys, String currentPresent) {
- if (toys.containsKey(currentPresent)) {
- int count = toys.get(currentPresent);
- toys.put(currentPresent, count + 1);
- } else {
- toys.put(currentPresent, 1);
- }
- }
- private static boolean successValidation(Map<String, Integer> toys) {
- if (toys.containsKey("Teddy bear") && toys.containsKey("Bicycle")) {
- return true;
- } else if (toys.containsKey("Doll") && toys.containsKey("Wooden train")) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement