Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.FullTask;
- import java.lang.reflect.Array;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.util.ArrayDeque;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- //scanning and creating a list of all the Robots
- String[] input = scan.nextLine().split(";");
- ArrayList<Robot> robotsList = new ArrayList<>();
- for (String entry : input) {
- String[] tokens = entry.split("-");
- String name = tokens[0];
- int timeToProcess = Integer.parseInt(tokens[1]);
- robotsList.add(new Robot(name,timeToProcess));
- }
- //parsing the time
- String inputTime = scan.nextLine();
- LocalTime localTime = LocalTime.parse(inputTime, DateTimeFormatter.ofPattern("H:mm:ss"));
- for (Robot robot : robotsList) {
- robot.setTime2(localTime);
- }
- //adding all the products in a queue
- ArrayDeque<String> products = new ArrayDeque<>();
- String inputProducts = scan.nextLine();
- while (!inputProducts.equals("End")) {
- products.offer(inputProducts);
- inputProducts = scan.nextLine();
- }
- // adding all the robots to a stack
- ArrayDeque<Robot> robotStack = new ArrayDeque<>();
- for (Robot robot : robotsList) {
- robotStack.offer(robot);
- }
- //while the products are not over
- while (products.size() > 0) {
- //products come every second
- localTime = localTime.plusSeconds(1);
- Robot currentRobot = robotStack.poll();
- //if the robot is available at that time
- if (currentRobot.isAvailable2(localTime)) {
- String currentProduct = products.poll();
- System.out.println(String.format("%s - %s [%s]",currentRobot.getName(),currentProduct,localTime));
- currentRobot.setTime2(localTime.plusSeconds(currentRobot.getProcessingTime()));
- robotStack.offer(currentRobot);
- // if is not available
- } else if (!currentRobot.isAvailable2(localTime)) {
- products.offer(products.poll());
- robotStack.offer(currentRobot);
- }
- }
- }
- }
- class Robot {
- private String name;
- private int processingTime;
- private boolean isAvailable2 = true;
- private LocalTime time2 = LocalTime.now();
- public void setTime2(LocalTime time2) {
- this.time2 = time2;
- }
- public LocalTime getTime2() {
- return this.time2;
- }
- public Robot(String name, int processingTime) {
- this.name = name;
- this.processingTime = processingTime;
- }
- public void Robot(String name, int processingTime, boolean isAvailable) {
- this.name = name;
- this.processingTime = processingTime;
- this.isAvailable2 = isAvailable;
- }
- public String getName() {
- return this.name;
- }
- public int getProcessingTime() {
- return this.processingTime;
- }
- public boolean isAvailable2(LocalTime time) {
- if (this.time2.compareTo(time) > 0) {
- this.isAvailable2 = false;
- return false;
- } else {
- this.isAvailable2 = true;
- }
- return true;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setProcessingTime(int processingTime) {
- this.processingTime = processingTime;
- }
- public void setAvailable2(boolean available) {
- this.isAvailable2 = available;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement