Advertisement
Guest User

Robotics

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