Advertisement
MiniMi2022

It's chocolate time

Mar 18th, 2024
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class ItIsChocolateTime {
  5.     public static void main(String[] args) {
  6.         Scanner myScan = new Scanner(System.in);
  7.         ArrayDeque<Double> milkQuantities = Arrays.stream(myScan.nextLine().split(" "))
  8.                 .map(Double::parseDouble).collect(Collectors.toCollection(ArrayDeque::new));
  9.         ArrayDeque<Double> cocoaPowderQuantities = new ArrayDeque<>();
  10.         String[] cocoaPQasArray = myScan.nextLine().split(" ");
  11.         for (int i = 0; i < cocoaPQasArray.length; i++) {
  12.             cocoaPowderQuantities.push(Double.parseDouble(cocoaPQasArray[i]));
  13.         }
  14.         Map<String, Integer> chocolates = new TreeMap<>();
  15.         while (!(milkQuantities.isEmpty() ||cocoaPowderQuantities.isEmpty())) {
  16.             double milkQuantity = milkQuantities.poll();
  17.             double cocoaPowderQuantity = cocoaPowderQuantities.pop();
  18.             double cacaoPercentage = cocoaPowderQuantity / (cocoaPowderQuantity + milkQuantity);
  19.             String kind = getKind(cacaoPercentage);
  20.             if (!kind.equals("")) {
  21.                 chocolates.putIfAbsent(kind,0);
  22.                 chocolates.put(kind, chocolates.get(kind) + 1);
  23.             }else{
  24.                 milkQuantities.offer(milkQuantity+10);
  25.             }
  26.         }
  27.         if (checkPreparedAll(chocolates)){
  28.             System.out.println("It’s a Chocolate Time. All chocolate types are prepared.");
  29.         }else{
  30.             System.out.println("Sorry, but you didn't succeed to prepare all types of chocolates.");
  31.         }
  32.         for (Map.Entry<String, Integer> chocolate : chocolates.entrySet()) {
  33.             System.out.printf("# %s --> %d\n",chocolate.getKey(),chocolate.getValue());
  34.         }
  35.     }
  36.  
  37.     private static boolean checkPreparedAll(Map<String, Integer> chocolates) {
  38.         return chocolates.size()==3;
  39.     }
  40.  
  41.     private static String getKind(double cacaoPercentage) {
  42.         if (cacaoPercentage == 0.3) {
  43.             return "Milk Chocolate";
  44.         } else if (cacaoPercentage == 0.5) {
  45.             return "Dark Chocolate";
  46.         } else if (cacaoPercentage == 1) {
  47.             return "Baking Chocolate";
  48.         }
  49.         return "";
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement