desislava_topuzakova

05. Robotics

May 21st, 2021
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. package StacksAndQueues;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class Robotics_05 {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         String[] inputRobots = scanner.nextLine().split(";");
  13.         String time = scanner.nextLine(); //начално време
  14.         String[] splittedTime = time.split(":");
  15.  
  16.         int hours = Integer.parseInt(splittedTime[0]);
  17.         int minutes = Integer.parseInt(splittedTime[1]);
  18.         int seconds = Integer.parseInt(splittedTime[2]);
  19.  
  20.         long totalSeconds = hours * 3600 + minutes * 60 + seconds;
  21.  
  22.         Map<String, Integer> robots = splitDataIntoLinkedHashMap(inputRobots);
  23.         int[] processTimes = new int[robots.size()];
  24.  
  25.         ArrayDeque<String> products = new ArrayDeque<>();
  26.         String product = scanner.nextLine();
  27.  
  28.         while (!product.equals("End")) {
  29.             products.offer(product);
  30.             product = scanner.nextLine();
  31.         }
  32.  
  33.  
  34.         while (!products.isEmpty()) {
  35.             totalSeconds++;
  36.             String currentProduct = products.poll();
  37.             boolean isAssigned = false;
  38.             decreaseProcessTime(processTimes);
  39.             for (int i = 0; i < processTimes.length; i++) {
  40.                 if (processTimes[i] == 0) {
  41.                     int count = 0;
  42.                     for (Map.Entry<String, Integer> robot : robots.entrySet()) {
  43.                         if (count == i) {
  44.                             processTimes[i] = robot.getValue();
  45.                             isAssigned = true;
  46.                             printJob(robot.getKey(), currentProduct, totalSeconds);
  47.                             break;
  48.                         }
  49.                         count++;
  50.                     }
  51.  
  52.                     break;
  53.                 }
  54.  
  55.             }
  56.             if (!isAssigned) {
  57.                 products.offer(currentProduct);
  58.             }
  59.  
  60.         }
  61.  
  62.     }
  63.  
  64.  
  65.     private static void decreaseProcessTime(int[] processTimes) {
  66.         for (int i = 0; i < processTimes.length; i++) {
  67.             if(processTimes[i] > 0){
  68.                 --processTimes[i];
  69.             }
  70.  
  71.         }
  72.     }
  73.  
  74.     private static void printJob(String name, String product, long totalSeconds) {
  75.         long hours = (totalSeconds / 3600) % 24;
  76.         long minutes = totalSeconds % 3600 / 60;
  77.         long seconds = totalSeconds % 60;
  78.         if (hours >= 24) {
  79.             hours -= 24;
  80.         }
  81.         System.out.printf("%s - %s [%02d:%02d:%02d]%n", name, product, hours, minutes, seconds);
  82.     }
  83.  
  84.     private static Map<String, Integer> getStartProcessTimes(String[] inputRobots) {
  85.         Map<String, Integer> processTimes = new LinkedHashMap<>();
  86.         for (String input : inputRobots) {
  87.             String name = input.split("-")[0];
  88.             processTimes.put(name, 0);
  89.         }
  90.         return processTimes;
  91.     }
  92.  
  93.     private static Map<String, Integer> splitDataIntoLinkedHashMap(String[] inputRobots) {
  94.         Map<String, Integer> robots = new LinkedHashMap<>();
  95.         for (String input : inputRobots) {
  96.             String name = input.split("-")[0];
  97.             int time = Integer.parseInt(input.split("-")[1]);
  98.             if (!robots.containsKey(name)) {
  99.                 robots.put(name, time);
  100.             }
  101.         }
  102.         return robots;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment