Advertisement
STANAANDREY

poo test2

Dec 17th, 2023
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1.  
  2. abstract class Task {
  3.     protected String name;
  4.  
  5.     public Task(final String name) {
  6.         this.name = name;
  7.     }
  8.  
  9.     public abstract boolean execute(double time);
  10. }
  11.  
  12. class SimpleTask extends Task {
  13.     private double necTime;
  14.  
  15.     public SimpleTask(final String name, double necTime) {
  16.         super(name);
  17.         this.necTime = necTime;
  18.     }
  19.  
  20.     @Override
  21.     public boolean execute(double time) {
  22.         if (time >= necTime) {
  23.             necTime = 0;
  24.             return true;
  25.         }
  26.         necTime -= time;
  27.         return false;
  28.     }
  29.  
  30.     @Override
  31.     public String toString() {
  32.         return "Name: " + name + ", Time: " + necTime + ";";
  33.     }
  34. }
  35.  
  36. class ComposedTask extends Task {
  37.     private SimpleTask[] simpleTasks;
  38.  
  39.     public ComposedTask(final String name, SimpleTask[] simpleTasks) {
  40.         super(name);
  41.         this.simpleTasks = simpleTasks;
  42.     }
  43.  
  44.     @Override
  45.     public boolean execute(double time) {
  46.         double q = time / simpleTasks.length;
  47.         boolean finished = true;
  48.         for (int i = 0; i < simpleTasks.length; i++) {
  49.             finished &= simpleTasks[i].execute(q);
  50.         }
  51.         return finished;
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.         String s = "Name: " + name + ", COntent: (";
  57.         for (int i = 0; i < simpleTasks.length; i++) {
  58.             s += simpleTasks[i] + " ";
  59.         }
  60.         return s + ")";
  61.     }
  62. }
  63.  
  64. class Processor {
  65.     private Task[] tasks;
  66.  
  67.     public Processor(Task[] tasks) {
  68.         this.tasks = tasks;
  69.     }
  70.  
  71.     public void finishAllTasks() {
  72.         boolean finished = false;
  73.         while (!finished) {
  74.             finished = true;
  75.             for (int i = 0; i < tasks.length; i++) {
  76.                 finished &= tasks[i].execute(5);
  77.             }
  78.         }
  79.     }
  80.  
  81.     @Override
  82.     public String toString() {
  83.         String s = "Proc: ";
  84.         for (int i = 0; i < tasks.length; i++) {
  85.             s += tasks[i] + "; ";
  86.         }
  87.         return s;
  88.     }
  89. }
  90.  
  91. public class Main {
  92.     public static void main(String[] args) {
  93.         var st1 = new SimpleTask("st1", 5);
  94.         var st2 = new SimpleTask("st2", 10);
  95.         var ct = new ComposedTask("ct", new SimpleTask[]{st1, st2});
  96.         var p = new Processor(new Task[]{ct});
  97.         System.out.println(p);
  98.         p.finishAllTasks();
  99.         System.out.println(p);
  100.     }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement