Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StacksAndQueues_0.more_exercises;
- import java.util.ArrayDeque;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Robotics {
- static class Robot {
- String name;
- int workTime;
- boolean isWorking;
- Robot(String name, int workTime, boolean isWorking) {
- this.name = name;
- this.workTime = workTime;
- this.isWorking = isWorking;
- }
- }
- private static List<Robot> groupRobots(String[] input) {
- List<Robot> robots = new ArrayList<>();
- for (String s : input) {
- String[] currentRobotInfo = s.split("-");
- Robot robot = new Robot(currentRobotInfo[0], Integer.parseInt(currentRobotInfo[1]), false);
- robots.add(robot);
- }
- return robots;
- }
- private static int[] initialWorkTime(List<Robot> robots) {
- int[] temp = new int[robots.size()];
- for (int i = 0; i < robots.size(); i++) {
- temp[i] = robots.get(i).workTime;
- }
- return temp;
- }
- private static void showMessage(String robotName, String product, int time) {
- long s = time % 60;
- long m = (time / 60) % 60;
- long h = (time / (60 * 60)) % 24;
- System.out.printf("%s - %s [%02d:%02d:%02d]%n", robotName, product, h, m, s);
- }
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- String[] input = console.nextLine().split(";");
- List<Robot> robots = groupRobots(input);
- int[] initialWorkTime = initialWorkTime(robots);
- String startTime = console.nextLine();
- String[] timeData = startTime.split(":");
- int hours = Integer.parseInt(timeData[0]);
- int minutes = Integer.parseInt(timeData[1]);
- int seconds = Integer.parseInt(timeData[2]);
- int time = hours * 3600 + minutes * 60 + seconds;
- ArrayDeque<String> products = new ArrayDeque<>();
- String inputProduct = console.nextLine();
- while (!inputProduct.equals("End")) {
- products.add(inputProduct);
- inputProduct = console.nextLine();
- }
- while (!products.isEmpty()) {
- time++;
- boolean working = false;
- for (int i = 0; i < robots.size(); i++) {
- if (!robots.get(i).isWorking) {
- String currentRobot = robots.get(i).name;
- String currentProduct = products.remove();
- robots.get(i).isWorking = true;
- robots.get(i).workTime--;
- showMessage(currentRobot, currentProduct, time);
- working = true;
- break;
- }else {
- robots.get(i).workTime--;
- if (robots.get(i).workTime == 0) {
- robots.get(i).workTime = initialWorkTime[i];
- robots.get(i).isWorking = false;
- }
- }
- }
- if (!working) {
- products.offer(products.poll());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment