-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
solved task #909
base: master
Are you sure you want to change the base?
solved task #909
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.mate.academy.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import core.mate.academy.model.Bulldozer; | ||
|
||
public class BulldozerProducer implements MachineProducer<Bulldozer> { | ||
@Override | ||
public List<Bulldozer> get() { | ||
List<Bulldozer> bulldozers = new ArrayList<>(); | ||
bulldozers.add(new Bulldozer()); | ||
bulldozers.add(new Bulldozer()); | ||
return bulldozers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.mate.academy.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import core.mate.academy.model.Excavator; | ||
|
||
public class ExcavatorProducer implements MachineProducer<Excavator> { | ||
@Override | ||
public List<Excavator> get() { | ||
List<Excavator> excavators = new ArrayList<>(); | ||
excavators.add(new Excavator()); | ||
excavators.add(new Excavator()); | ||
return excavators; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
package core.mate.academy.service; | ||
|
||
import core.mate.academy.model.Machine; | ||
|
||
import java.util.List; | ||
|
||
public interface MachineProducer { | ||
/** | ||
* In your implementations - create 2-3 models, add them to the list and return | ||
* @return - the list of models | ||
*/ | ||
List<Object> get(); | ||
public interface MachineProducer<T extends Machine> { | ||
List<T> get(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,13 @@ | ||
package core.mate.academy.service; | ||
|
||
import java.util.List; | ||
import core.mate.academy.model.Machine; | ||
|
||
/** | ||
* Parameterize this service and add its implementation in a separate class. | ||
*/ | ||
public interface MachineService { | ||
import java.util.List; | ||
|
||
/** | ||
* Return the list of machines. | ||
* In the implementation of this method please use your MachineProducer implementations | ||
* See that 'Class type' is not parametrized. | ||
* Consider to parametrize this Class< PARAMETRIZE_ME > with specific wildcard bounding | ||
* | ||
* @param type - any class of Machine sub class. For example: Truck.class or Bulldozer.class | ||
* @return the list of machines | ||
*/ | ||
List<Object> getAll(Class type); | ||
public interface MachineService<T extends Machine> { | ||
List<T> getAll(Class<? extends T> type); | ||
|
||
/** | ||
* Fill the machines list with passed value | ||
* Replace the Object with parametrized value. | ||
* This method should be able to work well with any type of machine passed as 'value' | ||
* | ||
* @param machines - list of machines to be filled with value | ||
* @param value - any object of machine sub class | ||
*/ | ||
void fill(List<Object> machines, Object value); | ||
void fill(List<? super T> machines, T value); | ||
|
||
/** | ||
* Call the method doWork() from each machine. | ||
* This method should be able to accept a list of bulldozers as well as list of trucks. | ||
* | ||
* @param machines - the list of machines | ||
*/ | ||
void startWorking(List<Object> machines); | ||
void startWorking(List<? extends T> machines); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,37 @@ | ||||||
package core.mate.academy.service; | ||||||
|
||||||
/** | ||||||
* Your implementation of MachineService. | ||||||
*/ | ||||||
public class MachineServiceImpl { | ||||||
import core.mate.academy.model.Bulldozer; | ||||||
import core.mate.academy.model.Excavator; | ||||||
import core.mate.academy.model.Machine; | ||||||
import core.mate.academy.model.Truck; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
|
||||||
public class MachineServiceImpl<T extends Machine> implements MachineService<T> { | ||||||
@Override | ||||||
public List<T> getAll(Class<? extends T> type) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (type == Bulldozer.class) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep if else constructions simple. There is no need for else after return in the if clause. The code can be made more concise by removing the else keyword when it follows a return statement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better use method equals (the same for other blocks) |
||||||
return (List<T>) new BulldozerProducer().get(); | ||||||
} else if (type == Excavator.class) { | ||||||
return (List<T>) new ExcavatorProducer().get(); | ||||||
} else if (type == Truck.class) { | ||||||
return (List<T>) new TruckProducer().get(); | ||||||
} | ||||||
return new ArrayList<>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider creating a local variable in your implementation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't return null or specific type List in |
||||||
} | ||||||
|
||||||
@Override | ||||||
public void fill(List<? super T> machines, T value) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for (int i = 0; i < machines.size(); i++) { | ||||||
machines.set(i, value); | ||||||
} | ||||||
} | ||||||
|
||||||
@Override | ||||||
public void startWorking(List<? extends T> machines) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for (T machine : machines) { | ||||||
machine.doWork(); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.mate.academy.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import core.mate.academy.model.Truck; | ||
|
||
public class TruckProducer implements MachineProducer<Truck> { | ||
@Override | ||
public List<Truck> get() { | ||
List<Truck> trucks = new ArrayList<>(); | ||
trucks.add(new Truck()); | ||
trucks.add(new Truck()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better use loop for creating a list of objects (the same for other classes) |
||
return trucks; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.