LardaX

Robotics...f*cked!

Jan 21st, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class RoboticsRetry {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String[] robotsLine = scanner.nextLine().split(";");
  7.  
  8.         ArrayList<Robot> robots = new ArrayList<>();
  9.  
  10.         for (int i = 0; i < robotsLine.length; i++) {
  11.             String[] robotInfo = robotsLine[i].split("-");
  12.             String name = robotInfo[0];
  13.             long processingTime = Long.parseLong(robotInfo[1]);
  14.  
  15.             Robot robot = new Robot(name, processingTime);
  16.             robots.add(robot);
  17.         }
  18.  
  19.         String[] startTime = scanner.nextLine().split(":");
  20.         long startSeconds = Integer.parseInt(startTime[0]) * 60 * 60 + Integer.parseInt(startTime[1]) * 60 + Integer.parseInt(startTime[2]);
  21.  
  22.         Deque<String> products = new ArrayDeque<>();
  23.  
  24.         long currentSeconds = startSeconds;
  25.  
  26.         String product = scanner.nextLine();
  27.  
  28.         while (!product.equals("End") || !products.isEmpty()) {
  29.             currentSeconds++;
  30.  
  31.             if (!product.equals("End")) {
  32.                 products.add(product);
  33.             }
  34.             String currentProduct = products.poll();
  35.             boolean productIsProcessed = false;
  36.  
  37.  
  38.             for (Robot robot : robots) {
  39.  
  40.                 if (robot.isProcessingProduct()){
  41.                     robot.setTimeCurrentProductIsProcessed(robot.getTimeCurrentProductIsProcessed() - 1);
  42.                     if (robot.getTimeCurrentProductIsProcessed() == 0) {
  43.  
  44.                         robot.setProcessingProduct(false);
  45.                         robot.setTimeCurrentProductIsProcessed(null);
  46.                     }
  47.                 }
  48.  
  49.                 if (!robot.isProcessingProduct() && !currentProduct.equals("none")) {
  50.                     robot.setProcessingProduct(true);
  51.                     robot.setTimeCurrentProductIsProcessed(robot.getProcessingTime());
  52.                     productIsProcessed = true;
  53.  
  54.                     String currentTime = getCurrentTime(currentSeconds);
  55.  
  56.                     System.out.printf("%s - %s %s", robot.getName(), currentProduct, currentTime);
  57.                 }
  58.  
  59.                 if (productIsProcessed) {
  60.                     currentProduct = "none";
  61.                 }
  62.             }
  63.  
  64.             if (!productIsProcessed) {
  65.                 products.add(currentProduct);
  66.             }
  67.  
  68.             if (!product.equals("End")) {
  69.                 product = scanner.nextLine();
  70.             }
  71.         }
  72.  
  73.     }
  74.  
  75.  
  76.     private static String getCurrentTime(long currentSeconds) {
  77.         long hours = ((int) (currentSeconds / 3600)) % 24;
  78.         long remainder = currentSeconds % 3600;
  79.         long minutes = remainder / 60;
  80.         long seconds = remainder % 60;
  81.  
  82.         String currentTime = String.format("[%02d:%02d:%02d]%n", hours, minutes, seconds);
  83.  
  84.         return currentTime;
  85.     }
  86.  
  87.     public static class Robot {
  88.         String name;
  89.  
  90.         long processingTime;
  91.  
  92.         boolean processingProduct;
  93.  
  94.         Long timeCurrentProductIsProcessed;
  95.  
  96.         public Robot(String name, long proccessingTime) {
  97.             this.name = name;
  98.             this.processingTime = proccessingTime;
  99.             this.processingProduct = false;
  100.             this.timeCurrentProductIsProcessed = null;
  101.         }
  102.  
  103.         public String getName() {
  104.             return name;
  105.         }
  106.  
  107.         public void setName(String name) {
  108.             this.name = name;
  109.         }
  110.  
  111.         public long getProcessingTime() {
  112.             return processingTime;
  113.         }
  114.  
  115.         public void setProcessingTime(long processingTime) {
  116.             this.processingTime = processingTime;
  117.         }
  118.  
  119.         public boolean isProcessingProduct() {
  120.             return processingProduct;
  121.         }
  122.  
  123.         public void setProcessingProduct(boolean processingProduct) {
  124.             this.processingProduct = processingProduct;
  125.         }
  126.  
  127.         public Long getTimeCurrentProductIsProcessed() {
  128.             return timeCurrentProductIsProcessed;
  129.         }
  130.  
  131.         public void setTimeCurrentProductIsProcessed(Long timeCurrentProductIsProcessed) {
  132.             this.timeCurrentProductIsProcessed = timeCurrentProductIsProcessed;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment