Joreto

Robotics

Sep 18th, 2023
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import java.time.LocalTime;
  2. import java.util.ArrayDeque;
  3. import java.util.Scanner;
  4.  
  5. public class Robotics {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] input = scanner.nextLine().split(";");
  10.         String[] robots = new String[input.length];
  11.         int[] processTime = new int[input.length];
  12.         int[] worktime = new int[input.length];
  13.  
  14.         for (int i = 0; i < input.length; i++) {
  15.             String[] data = input[i].split("-");
  16.             String name = data[0];
  17.             int time = Integer.parseInt(data[1]);
  18.             robots[i] = name;
  19.             processTime[i] = time;
  20.         }
  21.  
  22.         String startTime = scanner.nextLine();
  23.  
  24.         ArrayDeque<String> products = new ArrayDeque<>();
  25.  
  26.         String inputProduct = scanner.nextLine();
  27.  
  28.         while (!inputProduct.equals("End")) {
  29.             products.offer(inputProduct);
  30.  
  31.             inputProduct = scanner.nextLine();
  32.         }
  33.  
  34.         String[] timeData = startTime.split(":");
  35.         int hours = Integer.parseInt(timeData[0]);
  36.         int minutes = Integer.parseInt(timeData[1]);
  37.         int seconds = Integer.parseInt(timeData[2]);
  38.  
  39.         int beginSeconds = hours * 3600 + minutes * 60 + seconds;
  40.  
  41.         while (!products.isEmpty()) {
  42.             beginSeconds++;
  43.  
  44.             String product = products.poll();
  45.  
  46.             boolean isWorking = false;
  47.             for (int i = 0; i < robots.length; i++) {
  48.                 if (worktime[i] == 0 && !isWorking) {
  49.                     worktime[i] = processTime[i];
  50.                     isWorking = true;
  51.                     printRobotData(robots[i], product, beginSeconds);
  52.                 }
  53.                 if (worktime[i] > 0) {
  54.                     worktime[i]--;
  55.                 }
  56.             }
  57.  
  58.             if (!isWorking) { //ako ima svoboden
  59.                 products.offer(product);
  60.             }
  61.         }
  62.  
  63.     }
  64.  
  65.     private static void printRobotData(String robot, String product, int beginSeconds) {
  66.         long s = beginSeconds % 60;
  67.         long m = (beginSeconds / 60) % 60;
  68.         long h = (beginSeconds / (60 * 60)) % 24;
  69.         System.out.println(robot + " - " + product +
  70.                 String.format(" [%02d:%02d:%02d] ", h, m, s));
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment