Advertisement
Guest User

Robotics

a guest
Apr 21st, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. package com.company.FullTask;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.time.LocalTime;
  5. import java.time.format.DateTimeFormatter;
  6. import java.util.ArrayDeque;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Scanner;
  10.  
  11.  
  12.  
  13. class Main {
  14.     public static void main(String[] args) {
  15.         Scanner scan = new Scanner(System.in);
  16.  
  17.         //scanning and creating a list of all the Robots
  18.         String[] input = scan.nextLine().split(";");
  19.         ArrayList<Robot> robotsList = new ArrayList<>();
  20.         for (String entry : input) {
  21.             String[] tokens = entry.split("-");
  22.             String name = tokens[0];
  23.             int timeToProcess = Integer.parseInt(tokens[1]);
  24.             robotsList.add(new Robot(name,timeToProcess));
  25.         }
  26.         //parsing the time
  27.         String inputTime = scan.nextLine();
  28.         LocalTime localTime = LocalTime.parse(inputTime, DateTimeFormatter.ofPattern("H:mm:ss"));
  29.  
  30.         for (Robot robot : robotsList) {
  31.             robot.setTime2(localTime);
  32.         }
  33.  
  34.         //adding all the products in a queue
  35.         ArrayDeque<String> products = new ArrayDeque<>();
  36.         String inputProducts = scan.nextLine();
  37.         while (!inputProducts.equals("End")) {
  38.             products.offer(inputProducts);
  39.             inputProducts = scan.nextLine();
  40.         }
  41.         // adding all the robots to a stack
  42.         ArrayDeque<Robot> robotStack = new ArrayDeque<>();
  43.         for (Robot robot : robotsList) {
  44.             robotStack.offer(robot);
  45.         }
  46.         //while the products are not over
  47.         while (products.size() > 0) {
  48.             //products come every second
  49.             localTime = localTime.plusSeconds(1);
  50.             Robot currentRobot = robotStack.poll();
  51.  
  52.             //if the robot is available at that time
  53.             if (currentRobot.isAvailable2(localTime)) {
  54.                 String currentProduct = products.poll();
  55.                 System.out.println(String.format("%s - %s [%s]",currentRobot.getName(),currentProduct,localTime));
  56.                 currentRobot.setTime2(localTime.plusSeconds(currentRobot.getProcessingTime()));
  57.                 robotStack.offer(currentRobot);
  58.             // if is not available
  59.             } else if (!currentRobot.isAvailable2(localTime)) {
  60.                 products.offer(products.poll());
  61.                 robotStack.offer(currentRobot);
  62.             }
  63.         }
  64.  
  65.     }
  66. }
  67. class Robot {
  68.  
  69.     private String name;
  70.     private int processingTime;
  71.     private boolean isAvailable2 = true;
  72.     private LocalTime time2 = LocalTime.now();
  73.  
  74.     public void setTime2(LocalTime time2) {
  75.         this.time2 = time2;
  76.     }
  77.  
  78.     public LocalTime getTime2() {
  79.         return this.time2;
  80.     }
  81.  
  82.     public Robot(String name, int processingTime) {
  83.         this.name = name;
  84.         this.processingTime = processingTime;
  85.     }
  86.  
  87.     public void Robot(String name, int processingTime, boolean isAvailable) {
  88.         this.name = name;
  89.         this.processingTime = processingTime;
  90.         this.isAvailable2 = isAvailable;
  91.     }
  92.  
  93.     public String getName() {
  94.         return this.name;
  95.     }
  96.  
  97.     public int getProcessingTime() {
  98.         return this.processingTime;
  99.     }
  100.  
  101.  
  102.  
  103.     public boolean isAvailable2(LocalTime time) {
  104.         if (this.time2.compareTo(time) > 0) {
  105.             this.isAvailable2 = false;
  106.             return false;
  107.  
  108.         } else {
  109.             this.isAvailable2 = true;
  110.         }
  111.         return true;
  112.     }
  113.  
  114.     public void setName(String name) {
  115.         this.name = name;
  116.     }
  117.  
  118.     public void setProcessingTime(int processingTime) {
  119.         this.processingTime = processingTime;
  120.     }
  121.  
  122.     public void setAvailable2(boolean available) {
  123.         this.isAvailable2 = available;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement