Advertisement
IrinaIgnatova

Spaceship crafting - stack and queue

Oct 24th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.io.IOException;
  5.  
  6.  
  7. import java.time.LocalTime;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.*;
  10. import java.util.stream.Collectors;
  11.  
  12. public class Main {
  13.  
  14.  
  15.     public static void main(String[] args) {
  16.         Scanner scanner = new Scanner(System.in);
  17.  
  18.  
  19. //        ArrayList<Integer>liquidsLeft=new ArrayList<>();
  20. //        ArrayList<Integer>physicalMaterialsLeft=new ArrayList<>();
  21.         ArrayList<String> itemsObtained = new ArrayList<>();
  22.         TreeMap<String, Integer> materialAndAmount = new TreeMap<>();
  23.         materialAndAmount.put("Aluminium", 0);
  24.         materialAndAmount.put("Carbon fiber", 0);
  25.         materialAndAmount.put("Glass", 0);
  26.         materialAndAmount.put("Lithium", 0);
  27.  
  28.  
  29.         int[] chemicalLiquids = Arrays.stream(scanner.nextLine().split(" "))
  30.                 .mapToInt(Integer::parseInt)
  31.                 .toArray();
  32.  
  33.         int[] physicalItems = Arrays.stream(scanner.nextLine().split(" "))
  34.                 .mapToInt(Integer::parseInt)
  35.                 .toArray();
  36.  
  37.         ArrayDeque<Integer> queueLiquids = new ArrayDeque<>();
  38.         for (int liquid : chemicalLiquids) {
  39.             queueLiquids.offer(liquid);
  40.         }
  41.  
  42.         ArrayDeque<Integer> stackPhysicalItem = new ArrayDeque<>();
  43.         for (int item : physicalItems) {
  44.             stackPhysicalItem.push(item);
  45.         }
  46.  
  47.         int sum = 0;
  48.  
  49.         for (int i = 0; i < physicalItems.length; i++) {
  50.  
  51.             sum = queueLiquids.peek() + stackPhysicalItem.peek();
  52.             //System.out.println(sum);
  53.  
  54.             if (sum == 25) {
  55.                 String item = "Glass";
  56.                 queueLiquids.poll();
  57.                 stackPhysicalItem.pop();
  58.                 itemsObtained.add(item);
  59.                 if (!materialAndAmount.containsKey(item)) {
  60.                     materialAndAmount.put(item, 1);
  61.                 } else {
  62.                     materialAndAmount.put(item, materialAndAmount.get(item) + 1);
  63.                 }
  64.             } else if (sum == 50) {
  65.                 String item = "Aluminium";
  66.                 queueLiquids.poll();
  67.                 stackPhysicalItem.pop();
  68.                 itemsObtained.add(item);
  69.                 if (!materialAndAmount.containsKey(item)) {
  70.                     materialAndAmount.put(item, 1);
  71.                 } else {
  72.                     materialAndAmount.put(item, materialAndAmount.get(item) + 1);
  73.                 }
  74.             } else if (sum == 75) {
  75.                 String item = "Lithium";
  76.                 queueLiquids.poll();
  77.                 stackPhysicalItem.pop();
  78.                 itemsObtained.add(item);
  79.                 if (!materialAndAmount.containsKey(item)) {
  80.                     materialAndAmount.put(item, 1);
  81.                 } else {
  82.                     materialAndAmount.put(item, materialAndAmount.get(item) + 1);
  83.                 }
  84.             } else if (sum == 100) {
  85.                 String item = "Carbon fiber";
  86.                 queueLiquids.poll();
  87.                 stackPhysicalItem.pop();
  88.                 itemsObtained.add(item);
  89.                 if (!materialAndAmount.containsKey(item)) {
  90.                     materialAndAmount.put(item, 1);
  91.                 } else {
  92.                     materialAndAmount.put(item, materialAndAmount.get(item) + 1);
  93.                 }
  94.             } else {
  95.                 //презаписвам стойността в стека
  96.                 queueLiquids.poll();
  97.                 int currentItem = stackPhysicalItem.pop();
  98.                 stackPhysicalItem.push(currentItem + 3);
  99.  
  100.  
  101.             }
  102.         }
  103.  
  104.         if (itemsObtained.contains("Glass") && itemsObtained.contains("Aluminium") && itemsObtained.contains("Lithium") && itemsObtained.contains("Carbon fiber")) {
  105.             System.out.println("Wohoo! You succeeded in building the spaceship!");
  106.         } else {
  107.             System.out.println("Ugh, what a pity! You didn't have enough materials to build the spaceship.");
  108.         }
  109.  
  110.         if (queueLiquids.size() == 0) {
  111.             System.out.println("Liquids left: none");
  112.         } else {
  113.             System.out.print("Liquids left: ");
  114.  
  115.             while (queueLiquids.size() > 1) {
  116.                 System.out.print(queueLiquids.poll() + ", ");
  117.             }
  118.             System.out.println(queueLiquids.poll());
  119.         }
  120.  
  121.             if (stackPhysicalItem.size() == 0) {
  122.                 System.out.println("Physical items left: none");
  123.             } else {
  124.                 System.out.print("Physical items left: ");
  125.  
  126.                 while (stackPhysicalItem.size() > 1) {
  127.                     System.out.print(stackPhysicalItem.pop() + ", ");
  128.                 }
  129.                 System.out.println(stackPhysicalItem.pop());
  130.  
  131.  
  132.             }
  133.  
  134.             for (Map.Entry<String, Integer> entry : materialAndAmount.entrySet()) {
  135.                 String itemToPrint = entry.getKey();
  136.                 int valueToPrint = entry.getValue();
  137.                 System.out.printf("%s: %d%n", entry.getKey(), entry.getValue());
  138.             }
  139.  
  140.         }
  141.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement