desislava_topuzakova

05. Robotics

May 20th, 2021
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String inputRobots = scanner.nextLine(); //ROB-15;SS2-10;NX8000-3
  7.         String [] robotsData = inputRobots.split(";"); //["ROB-15", "SS2-10", "NX8000-3"]
  8.         LinkedHashMap<String, Integer> robots = getRobotsMap(robotsData); //име -> за колко време приключва даден продукт
  9.         LinkedHashMap<String, Integer> robotsWorkingTime = getRobotsWorkingTimeMap(robotsData);// име -> колко време работи върху даден продукт
  10.  
  11.         String startTime = scanner.nextLine(); //8:00:00 -> split(":") -> ["8", "00", "00"]
  12.         int startTimeInSeconds = getStartTimeInSeconds(startTime);
  13.  
  14.  
  15.         ArrayDeque<String> products = new ArrayDeque<>();
  16.         String product = scanner.nextLine(); //End или продукт
  17.         while(!product.equals("End")) {
  18.             products.offer(product);
  19.             product = scanner.nextLine();
  20.         }
  21.  
  22.         while(!products.isEmpty()) {
  23.             String currentProduct = products.poll();
  24.             startTimeInSeconds++;
  25.             //намалим времето за работа на всички роботи, при които има продукт
  26.             decreaseWorkingTime(robotsWorkingTime);
  27.             boolean isTaken = false;
  28.             //проверка кой робот е свободен -> working time = 0
  29.             for (Map.Entry<String, Integer> robot : robotsWorkingTime.entrySet()) { //име -> време в което работи
  30.                 if(robot.getValue() == 0) { //роботът е свободен  -> роботът започва да обработва продукта
  31.                     System.out.println(robot.getKey() + " - " + currentProduct + " ["  + getStartTime(startTimeInSeconds) + "]");
  32.                     robotsWorkingTime.put(robot.getKey(), robots.get(robot.getKey()));
  33.                     isTaken = true;
  34.                     break;
  35.                 }
  36.             }
  37.  
  38.             if(!isTaken) {
  39.                 products.offer(currentProduct);
  40.             }
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.     }
  47.  
  48.     private static void decreaseWorkingTime(LinkedHashMap<String, Integer> robotsWorkingTime) {
  49.         for (Map.Entry<String, Integer> robot : robotsWorkingTime.entrySet()){
  50.             if(robot.getValue() > 0) { //не е свободен
  51.                 //работили са 1 секунда по продукта -> времето за работа по продукта - 1
  52.                 robotsWorkingTime.put(robot.getKey(), robot.getValue() - 1);
  53.             }
  54.         }
  55.     }
  56.  
  57.     private static LinkedHashMap<String, Integer> getRobotsWorkingTimeMap(String[] robotsData) {
  58.         LinkedHashMap<String, Integer> robots = new LinkedHashMap<>();
  59.         for (String robotData : robotsData) {
  60.             //robotData = "ROB-15" -> split("-") -> ["ROB", "15"]
  61.             String robotName = robotData.split("-")[0];
  62.             robots.put(robotName, 0);
  63.         }
  64.         return robots;
  65.     }
  66.  
  67.     private static int getStartTimeInSeconds(String startTime) {
  68.         int hours = Integer.parseInt(startTime.split(":")[0]);
  69.         int minutes = Integer.parseInt(startTime.split(":")[1]);
  70.         int seconds = Integer.parseInt(startTime.split(":")[2]);
  71.         return hours * 3600 + minutes * 60 + seconds;
  72.     }
  73.  
  74.     private static String getStartTime(int startTimeInSeconds) {
  75.         //секунди -> "h:mm:s" -> hours + ":" + minutes + ":" + seconds
  76.         int hours = startTimeInSeconds / 3600;
  77.         int minutes = startTimeInSeconds % 3600 / 60;
  78.         int seconds = startTimeInSeconds % 60;
  79.         return String.format("%02d:%02d:%02d",hours, minutes, seconds);
  80.     }
  81.  
  82.     private static LinkedHashMap<String, Integer> getRobotsMap(String[] robotsData) {
  83.         LinkedHashMap<String, Integer> robots = new LinkedHashMap<>();
  84.         for (String robotData : robotsData) {
  85.                 //robotData = "ROB-15" -> split("-") -> ["ROB", "15"]
  86.             String robotName = robotData.split("-")[0];
  87.             int processingTime = Integer.parseInt(robotData.split("-")[1]);
  88.             robots.put(robotName, processingTime);
  89.         }
  90.         return robots;
  91.     }
  92.  
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment