Advertisement
damesova

Robotics [Mimi] [JA]

Apr 28th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package _06_linearDataStructures_Exercises;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Scanner;
  5.  
  6. public class _11_Robotics {
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         String[] input = sc.nextLine().split(";");
  11.         String[] robots = new String[input.length]; //  robots names
  12.         int[] needTime = new int[input.length];     //  single robot time
  13.         int[] counter = new int[input.length];      //  counter
  14.  
  15.         for (int i = 0; i < input.length; i++) {
  16.             String[] robotData = input[i].split("-");
  17.             robots[i] = robotData[0];                       //  fill robots collection
  18.             needTime[i] = Integer.parseInt(robotData[1]);   //  fill single robot's time collection with the same index
  19.         }
  20.  
  21.         //  manage the time variables:
  22.         String[] startTime = sc.nextLine().split(":");
  23.         int h = Integer.parseInt(startTime[0]) * 60 * 60;
  24.         int m = Integer.parseInt(startTime[1]) * 60;
  25.         int s = Integer.parseInt(startTime[2]);
  26.         long totalS = h + m + s;
  27.  
  28.         ArrayDeque<String> products = new ArrayDeque<>();
  29.         String product = "";
  30.         while (!"End".equals(product = sc.nextLine())) {
  31.             products.add(product);  //  fill products in Queue
  32.         }
  33.  
  34.         //  main logic here:
  35.         while (!products.isEmpty()) {
  36.             totalS++;
  37.             for (int i = 0; i < counter.length; i++) {
  38.                 if (counter[i] > 0) {
  39.                     counter[i]--;  
  40.                 }
  41.             }
  42.             String currProd = products.poll();
  43.             boolean isTaken = false;
  44.             for (int i = 0; i < robots.length; i++) {
  45.                 if (counter[i] == 0) {
  46.                     // robot is idle
  47.                     counter[i] = needTime[i];
  48.                     isTaken = true;
  49.                     System.out.printf("%s - %s [%s]%n", robots[i], currProd, convertTime(totalS));
  50.                     break;
  51.                 }
  52.             }
  53.             if (!isTaken) {
  54.                 //  robot is not idle
  55.                 products.add(currProd);
  56.             }
  57.         }
  58.     }
  59.  
  60.     public static String convertTime(long totalS) {
  61.         long s = totalS % 60;
  62.         long m = (totalS / 60) % 60;
  63.         long h = (totalS / (60 * 60)) % 24;
  64.  
  65.         return String.format("%02d:%02d:%02d", h, m, s);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement