@@ -78,6 +78,10 @@ class Machine{
7878 private final int id ;
7979 private List <Task > tasks = new ArrayList <>();
8080
81+ public Machine (){
82+ this .id = countMachines ++;
83+ }
84+
8185 public Machine (Task task ){
8286 this .id = countMachines ++;
8387 this .tasks .add (task );
@@ -108,11 +112,22 @@ public int getTotalTimeAllocated(){
108112 }
109113
110114 public int getStartTimeOfFistTask (){
111- return this .tasks .get (0 ).getStart ();
115+ int startTime =99 ;
116+ for (Task t : this .tasks ){
117+ if (t .getStart ()<startTime )
118+ startTime = t .getStart ();
119+ }
120+
121+ return startTime ;
112122 }
113123
114124 public int getEndTimeOfLastTask (){
115- return this .tasks .get (countMachines ).getEnd ();
125+ int endTime =0 ;
126+ for (Task t : this .tasks ){
127+ if (t .getEnd ()>endTime )
128+ endTime = t .getEnd ();
129+ }
130+ return endTime ;
116131 }
117132
118133 public String toString (){
@@ -129,49 +144,33 @@ public String toString(){
129144class ResultResourceAllocation {
130145
131146 public static List <Machine > allocationStrategy (List <Machine > cluster , Task task ){
132- int idOfMachineWithLessLoad = 0 ;
133- int idOfNextMachineUnoccupied = 0 ;
134- Machine server = cluster .get (idOfMachineWithLessLoad );
135- System .out .println (cluster .size ());
136- int lessLoad = server .getTotalTimeAllocated ();
137- int lessEndTime = server .getEndTimeOfLastTask ();
138- int machineID =-99 ;
139- System .out .println ("lessLoad: " +lessLoad );
140- System .out .println ("lessEndTime: " +lessEndTime );
141-
147+ int machineID = 99 ;
148+ System .out .println (task );
142149 for (Machine machine : cluster ) {
143- int startTimeOfFistTask = machine .getStartTimeOfFistTask ();
144- int endTimeOfLastTask = machine .getEndTimeOfLastTask ();
145- int loadMachine = machine .getTotalTimeAllocated ();machineID = machine .getId ();
146-
147- if (loadMachine < lessLoad ){
148- lessLoad = loadMachine ;
149- idOfMachineWithLessLoad = machine .getId ();
150- }
150+ int loadMachine = machine .getTotalTimeAllocated (); //0
151+ machineID = machine .getId ();
151152
152- if (lessEndTime < endTimeOfLastTask ) {
153- lessEndTime = endTimeOfLastTask ;
154- idOfNextMachineUnoccupied = machine .getId ();
155- }
156-
157- if (task .getEnd ()< startTimeOfFistTask ){
153+ if (loadMachine ==0 ){
154+ machine .addTask (task );
155+ machineID = machine .getId ();
156+ task .setAllocationStatus (true );
157+ } else if (task .getEnd () < machine .getStartTimeOfFistTask ()){
158158 machine .addTask (task );
159159 machineID = machine .getId ();
160160 task .setAllocationStatus (true );
161- } else if (task .getStart () > endTimeOfLastTask ){
161+ } else if (task .getStart () > machine . getEndTimeOfLastTask () ){
162162 machine .addTask (task );
163163 task .setAllocationStatus (true );
164164 machineID = machine .getId ();
165165 }
166166 }
167167
168168 if (!task .isAllocated ()){
169- System .out .println ("Nova máquina criada" );
170169 cluster .add (new Machine (task ));
171170 machineID = cluster .get (cluster .size ()-1 ).getId ();
172171 }
173172
174- System .out .println ("Task " +task .getId ()+"alocada na maquina " +machineID );
173+ System .out .println ("Task " +task .getId ()+" alocada na maquina " +machineID );
175174 return cluster ;
176175 }
177176
@@ -183,18 +182,19 @@ public static int getMinMatchines(List<Integer> start, List<Integer> end) {
183182 for (int i = 0 ; i < start .size (); i ++) {
184183 queue .add (new Task (start .get (i ), end .get (i )));
185184 }
186-
187- //Machine server = new Machine(queue.get(0));
188185
189186 for (Task t : queue ) {
190- System .out .println (t .toString ());
191187
192188 if (cluster .isEmpty ()){
193- cluster .add (new Machine (t ));
194- } else {
195- cluster = allocationStrategy (cluster ,t );
196- }
189+ cluster .add (new Machine ());
190+ System .out .println ("Cluster is empity. Add new machine" );
191+ }
192+
193+ allocationStrategy (cluster ,t );
194+
197195 }
196+ System .out .println ("" );
197+ System .out .println ("Minimum number of machines in the cluster is: " + cluster .size ());
198198 return cluster .size ();
199199 }
200200}
@@ -222,12 +222,9 @@ public static void main(String[] args) throws IOException {
222222
223223 bufferedReader .close ();
224224
225- System .out .println (Arrays .toString (start .toArray ()));
226- System .out .println (Arrays .toString (end .toArray ()));
227-
228225 int result = ResultResourceAllocation .getMinMatchines (start ,end );
229226
230- bufferedWriter .write ("->" + result );
227+ bufferedWriter .write (result );
231228 bufferedWriter .newLine ();
232229 bufferedWriter .flush ();
233230 bufferedWriter .close ();
0 commit comments