Advertisement
LardaX

RoboticsBetter

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