Advertisement
purshink

Santa PR.F belejki

Jun 27th, 2020
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args)  {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String[] boxes = scanner.nextLine().split(" ");
  14.         String[] magic = scanner.nextLine().split(" ");
  15.  
  16.  
  17.         ArrayDeque<Integer> boxesValues = new ArrayDeque<>();
  18.         ArrayDeque<Integer> magicValues = new ArrayDeque<>();
  19.  
  20.         for (String box : boxes) {
  21.             boxesValues.push(Integer.parseInt(box));
  22.         }
  23.  
  24.         for (String value : magic) {
  25.             magicValues.offer(Integer.parseInt(value));
  26.         }
  27.  
  28.         int dollCounter = 0;
  29.         int woodenTrainCounter = 0;
  30.         int teddyBearCounter = 0;
  31.         int bicycleCounter = 0;
  32.  
  33.  
  34.         while (!boxesValues.isEmpty() && !magicValues.isEmpty()) {
  35.             //napravi si promenlivi, za d (b,m ) a izbegnesh nullPointerExeption
  36.             int b = boxesValues.peek();
  37.             int m = magicValues.peek();
  38.  
  39.             int sum = boxesValues.peek() * magicValues.peek();
  40.  
  41.             //tuk e dobre da pochnesh da proverqvash s negativnite rezultati parvo
  42.             if (sum < 0) {
  43.                 sum = boxesValues.peek() + magicValues.peek();
  44.                 remove(boxesValues, magicValues);
  45.                 boxesValues.push(sum);
  46.  
  47.             }
  48.             else if (m == 0 || b == 0){
  49.                 //tuk trqbva da e dva pati if, an ne if / else if, zashtoto inache ako i dvete sa nula, samo ednoto shte se mahne
  50.                 if(m  == 0){
  51.                     // ne s remove()
  52.                     magicValues.poll();
  53.                 }
  54.                 if(b == 0){
  55.                     boxesValues.pop();
  56.                 }
  57.             }
  58.  
  59.           else  if (sum == 150) {
  60.                 dollCounter++;
  61.                 remove(boxesValues, magicValues);
  62.             } else if (sum == 250) {
  63.                 woodenTrainCounter++;
  64.                 remove(boxesValues, magicValues);
  65.             } else if (sum == 300) {
  66.                 teddyBearCounter++;
  67.                 remove(boxesValues, magicValues);
  68.             } else if (sum == 400) {
  69.                 bicycleCounter++;
  70.                 remove(boxesValues, magicValues);
  71.             }
  72.  
  73.             else {
  74.                 magicValues.poll();
  75.                 int newMaterial= b + 15;
  76.                 boxesValues.pop();
  77.                 boxesValues.push(newMaterial);
  78.             }
  79.         }
  80.  
  81.      
  82.            
  83.             // parvo printirash saobshtenieto
  84.             if(dollCounter > 0 && woodenTrainCounter > 0 || bicycleCounter > 0 && teddyBearCounter > 0){
  85.                 System.out.println("The presents are crafted! Merry Christmas!");
  86.  
  87.             }
  88.  
  89.             else {
  90.             System.out.println("No presents this Christmas!");
  91.             }
  92.        
  93.         //proverqvash purvo  dali sa prazni ili ne i printirash ako ne sa
  94.         if (boxesValues.size() > 0 ) {
  95.             System.out.print("Materials left: ");
  96.             for (int i = 0; i < boxesValues.size(); i++) {
  97.                 if (i == boxesValues.size() - 1) {
  98.                     System.out.print(boxesValues.pop());
  99.                 } else {
  100.                     System.out.print(String.format("%d, ", boxesValues.pop()));
  101.                     i--;
  102.                 }
  103.             }
  104.         }
  105.         if (magicValues.size() > 0 ) {
  106.             System.out.print("Materials left: ");
  107.             for (int i = 0; i < boxesValues.size(); i++) {
  108.                 if (i == boxesValues.size() - 1) {
  109.                     System.out.print(boxesValues.poll());
  110.                 } else {
  111.                     System.out.print(String.format("%d, ", magicValues.poll()));
  112.                     i--;
  113.                 }
  114.             }
  115.         }
  116.             System.out.println();
  117.  
  118.        
  119.         //tova printirane go pravish s TreeMap, zashtoto igrachkite trqbvA da se sortirat po azbuchen red
  120.             print(dollCounter, woodenTrainCounter, teddyBearCounter, bicycleCounter);
  121.  
  122.        
  123.     }
  124.  
  125.     private static void print(int dollCounter, int woodenTrainCounter, int teddyBearCounter, int bicycleCounter) {
  126.         if(dollCounter > 0){
  127.             System.out.println(String.format("Doll: %d", dollCounter));
  128.         }
  129.         if(woodenTrainCounter > 0){
  130.             System.out.println(String.format("Wooden train: %d", woodenTrainCounter));
  131.         }
  132.         if(teddyBearCounter > 0){
  133.             System.out.println(String.format("Teddy bear: %d", teddyBearCounter));
  134.         }
  135.         if(bicycleCounter > 0){
  136.             System.out.println(String.format("Bicycle: %d", bicycleCounter));
  137.         }
  138.     }
  139.  
  140.  
  141.     private static void remove(ArrayDeque<Integer> boxes, ArrayDeque<Integer> magics) {
  142.         boxes.pop();
  143.         magics.poll();
  144.  
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement