Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Task {
- protected String name;
- public Task(final String name) {
- this.name = name;
- }
- public abstract boolean execute(double time);
- }
- class SimpleTask extends Task {
- private double necTime;
- public SimpleTask(final String name, double necTime) {
- super(name);
- this.necTime = necTime;
- }
- @Override
- public boolean execute(double time) {
- if (time >= necTime) {
- necTime = 0;
- return true;
- }
- necTime -= time;
- return false;
- }
- @Override
- public String toString() {
- return "Name: " + name + ", Time: " + necTime + ";";
- }
- }
- class ComposedTask extends Task {
- private SimpleTask[] simpleTasks;
- public ComposedTask(final String name, SimpleTask[] simpleTasks) {
- super(name);
- this.simpleTasks = simpleTasks;
- }
- @Override
- public boolean execute(double time) {
- double q = time / simpleTasks.length;
- boolean finished = true;
- for (int i = 0; i < simpleTasks.length; i++) {
- finished &= simpleTasks[i].execute(q);
- }
- return finished;
- }
- @Override
- public String toString() {
- String s = "Name: " + name + ", COntent: (";
- for (int i = 0; i < simpleTasks.length; i++) {
- s += simpleTasks[i] + " ";
- }
- return s + ")";
- }
- }
- class Processor {
- private Task[] tasks;
- public Processor(Task[] tasks) {
- this.tasks = tasks;
- }
- public void finishAllTasks() {
- boolean finished = false;
- while (!finished) {
- finished = true;
- for (int i = 0; i < tasks.length; i++) {
- finished &= tasks[i].execute(5);
- }
- }
- }
- @Override
- public String toString() {
- String s = "Proc: ";
- for (int i = 0; i < tasks.length; i++) {
- s += tasks[i] + "; ";
- }
- return s;
- }
- }
- public class Main {
- public static void main(String[] args) {
- var st1 = new SimpleTask("st1", 5);
- var st2 = new SimpleTask("st2", 10);
- var ct = new ComposedTask("ct", new SimpleTask[]{st1, st2});
- var p = new Processor(new Task[]{ct});
- System.out.println(p);
- p.finishAllTasks();
- System.out.println(p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement