Advertisement
LardaX

RoboticsRetryAgain

Jan 23rd, 2017
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 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.         addRobots(robotsLine, robots);
  11.  
  12.         String[] startTime = scanner.nextLine().split(":");
  13.         long startSeconds = Integer.parseInt(startTime[0]) * 60 * 60 + Integer.parseInt(startTime[1]) * 60 + Integer.parseInt(startTime[2]);
  14.  
  15.         Deque<String> products = new ArrayDeque<>();
  16.  
  17.         long currentSeconds = startSeconds;
  18.  
  19.         String product = scanner.nextLine();
  20.  
  21.         while (!product.equals("End")) {
  22.             currentSeconds++;
  23.             products.add(product);
  24.  
  25.             String currentProduct = products.poll();
  26.             boolean productIsProcessed = false;
  27.  
  28.             for (Robot robot : robots) {
  29.  
  30.                 if (robot.isProcessingProduct()) {
  31.                     robot.setTimeCurrentProductIsProcessed(robot.getTimeCurrentProductIsProcessed() - 1);
  32.                     if (robot.getTimeCurrentProductIsProcessed() <= 0) {
  33.  
  34.                         robot.setProcessingProduct(false);
  35.                         robot.setTimeCurrentProductIsProcessed(null);
  36.                     }
  37.                 }
  38.  
  39.                 if (!robot.isProcessingProduct() && !currentProduct.equals("none")) {
  40.                     robot.setProcessingProduct(true);
  41.                     robot.setTimeCurrentProductIsProcessed(robot.getProcessingTime());
  42.                     productIsProcessed = true;
  43.  
  44.                     String currentTime = getCurrentTime(currentSeconds);
  45.  
  46.                     System.out.printf("%s - %s %s", robot.getName(), currentProduct, currentTime);
  47.                 }
  48.  
  49.                 if (productIsProcessed) {
  50.                     currentProduct = "none";
  51.                 }
  52.             }
  53.  
  54.             product = scanner.nextLine();
  55.  
  56.             if (!productIsProcessed) {
  57.                 products.add(currentProduct);
  58.             }
  59.         }
  60.  
  61.         while (products.size() > 0) {
  62.             currentSeconds++;
  63.  
  64.             String currentProduct = products.poll();
  65.             boolean productIsProcessed = false;
  66.  
  67.             for (Robot robot : robots) {
  68.  
  69.                 if (robot.isProcessingProduct()) {
  70.                     robot.setTimeCurrentProductIsProcessed(robot.getTimeCurrentProductIsProcessed() - 1);
  71.                     if (robot.getTimeCurrentProductIsProcessed() <= 0) {
  72.  
  73.                         robot.setProcessingProduct(false);
  74.                         robot.setTimeCurrentProductIsProcessed(null);
  75.                     }
  76.                 }
  77.  
  78.                 if (!robot.isProcessingProduct() && !currentProduct.equals("none")) {
  79.                     robot.setProcessingProduct(true);
  80.                     robot.setTimeCurrentProductIsProcessed(robot.getProcessingTime());
  81.                     productIsProcessed = true;
  82.  
  83.                     String currentTime = getCurrentTime(currentSeconds);
  84.  
  85.                     System.out.printf("%s - %s %s", robot.getName(), currentProduct, currentTime);
  86.                 }
  87.  
  88.                 if (productIsProcessed) {
  89.                     currentProduct = "none";
  90.                 }
  91.             }
  92.  
  93.             if (!productIsProcessed) {
  94.                 products.add(currentProduct);
  95.             }
  96.         }
  97.  
  98.     }
  99.  
  100.     private static void addRobots(String[] robotsLine, ArrayList<Robot> robots) {
  101.         for (int i = 0; i < robotsLine.length; i++) {
  102.             String[] robotInfo = robotsLine[i].split("-");
  103.             String name = robotInfo[0];
  104.             long processingTime = Long.parseLong(robotInfo[1]);
  105.  
  106.             Robot robot = new Robot(name, processingTime);
  107.             robots.add(robot);
  108.         }
  109.     }
  110.  
  111.  
  112.     private static String getCurrentTime(long currentSeconds) {
  113.         long hours = ((int) (currentSeconds / 3600)) % 24;
  114.         long remainder = currentSeconds % 3600;
  115.         long minutes = remainder / 60;
  116.         long seconds = remainder % 60;
  117.  
  118.         String currentTime = String.format("[%02d:%02d:%02d]%n", hours, minutes, seconds);
  119.  
  120.         return currentTime;
  121.     }
  122.  
  123.     public static class Robot {
  124.         String name;
  125.  
  126.         long processingTime;
  127.  
  128.         boolean processingProduct;
  129.  
  130.         Long timeCurrentProductIsProcessed;
  131.  
  132.         public Robot(String name, long proccessingTime) {
  133.             this.name = name;
  134.             this.processingTime = proccessingTime;
  135.             this.processingProduct = false;
  136.             this.timeCurrentProductIsProcessed = null;
  137.         }
  138.  
  139.         public String getName() {
  140.             return name;
  141.         }
  142.  
  143.         public void setName(String name) {
  144.             this.name = name;
  145.         }
  146.  
  147.         public long getProcessingTime() {
  148.             return processingTime;
  149.         }
  150.  
  151.         public void setProcessingTime(long processingTime) {
  152.             this.processingTime = processingTime;
  153.         }
  154.  
  155.         public boolean isProcessingProduct() {
  156.             return processingProduct;
  157.         }
  158.  
  159.         public void setProcessingProduct(boolean processingProduct) {
  160.             this.processingProduct = processingProduct;
  161.         }
  162.  
  163.         public Long getTimeCurrentProductIsProcessed() {
  164.             return timeCurrentProductIsProcessed;
  165.         }
  166.  
  167.         public void setTimeCurrentProductIsProcessed(Long timeCurrentProductIsProcessed) {
  168.             this.timeCurrentProductIsProcessed = timeCurrentProductIsProcessed;
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement