Valik98

Robotics

Dec 18th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. package StacksAndQueues_0.more_exercises;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class Robotics {
  9.     static class Robot {
  10.         String name;
  11.         int workTime;
  12.         boolean isWorking;
  13.  
  14.         Robot(String name, int workTime, boolean isWorking) {
  15.             this.name = name;
  16.             this.workTime = workTime;
  17.             this.isWorking = isWorking;
  18.         }
  19.     }
  20.  
  21.     private static List<Robot> groupRobots(String[] input) {
  22.         List<Robot> robots = new ArrayList<>();
  23.  
  24.         for (String s : input) {
  25.             String[] currentRobotInfo = s.split("-");
  26.  
  27.             Robot robot = new Robot(currentRobotInfo[0], Integer.parseInt(currentRobotInfo[1]), false);
  28.  
  29.             robots.add(robot);
  30.         }
  31.  
  32.         return robots;
  33.     }
  34.  
  35.     private static int[] initialWorkTime(List<Robot> robots) {
  36.         int[] temp = new int[robots.size()];
  37.  
  38.         for (int i = 0; i < robots.size(); i++) {
  39.             temp[i] = robots.get(i).workTime;
  40.         }
  41.  
  42.         return temp;
  43.     }
  44.  
  45.     private static void showMessage(String robotName, String product, int time) {
  46.         long s = time % 60;
  47.         long m = (time / 60) % 60;
  48.         long h = (time / (60 * 60)) % 24;
  49.  
  50.         System.out.printf("%s - %s [%02d:%02d:%02d]%n", robotName, product, h, m, s);
  51.     }
  52.  
  53.     public static void main(String[] args) {
  54.         Scanner console = new Scanner(System.in);
  55.  
  56.         String[] input = console.nextLine().split(";");
  57.  
  58.         List<Robot> robots = groupRobots(input);
  59.         int[] initialWorkTime = initialWorkTime(robots);
  60.  
  61.         String startTime = console.nextLine();
  62.         String[] timeData = startTime.split(":");
  63.         int hours = Integer.parseInt(timeData[0]);
  64.         int minutes = Integer.parseInt(timeData[1]);
  65.         int seconds = Integer.parseInt(timeData[2]);
  66.  
  67.         int time = hours * 3600 + minutes * 60 + seconds;
  68.  
  69.         ArrayDeque<String> products = new ArrayDeque<>();
  70.  
  71.         String inputProduct = console.nextLine();
  72.  
  73.         while (!inputProduct.equals("End")) {
  74.             products.add(inputProduct);
  75.  
  76.             inputProduct = console.nextLine();
  77.         }
  78.  
  79.  
  80.         while (!products.isEmpty()) {
  81.             time++;
  82.  
  83.             boolean working = false;
  84.  
  85.             for (int i = 0; i < robots.size(); i++) {
  86.                 if (!robots.get(i).isWorking) {
  87.                     String currentRobot = robots.get(i).name;
  88.                     String currentProduct = products.remove();
  89.                     robots.get(i).isWorking = true;
  90.                     robots.get(i).workTime--;
  91.                     showMessage(currentRobot, currentProduct, time);
  92.  
  93.                     working = true;
  94.                     break;
  95.                 }else {
  96.                     robots.get(i).workTime--;
  97.  
  98.                     if (robots.get(i).workTime == 0) {
  99.                         robots.get(i).workTime = initialWorkTime[i];
  100.                         robots.get(i).isWorking = false;
  101.                     }
  102.                 }
  103.             }
  104.             if (!working) {
  105.                 products.offer(products.poll());
  106.             }
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment