-
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
implemented pecs #919
base: master
Are you sure you want to change the base?
implemented pecs #919
Conversation
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.
Hey there! It looks like there are a few critical issues that need to be addressed in your code. The use of generics needs to be tightened up to ensure type safety, and the logic in methods like 'getAll' and 'startWorking' needs to be corrected to match the task's requirements. Also, remember to avoid using raw types and unnecessary class fields. Keep up the good work, and with a bit of tweaking, your code will be in great shape! 👍💻
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
@@ -6,10 +6,13 @@ | |||
*/ | |||
public class Bulldozer extends Machine { |
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.
Don't begin class or method implementation with empty lines (lines 3-6). The class declaration should follow directly after the package statement.
@@ -6,10 +6,13 @@ | |||
*/ | |||
public class Bulldozer extends Machine { | |||
public Bulldozer() { | |||
setName(getName()); |
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.
The no-args constructor is setting the name of the Bulldozer to its current name which is not initialized yet. This logic doesn't seem to serve any purpose and could potentially be removed unless there's a specific reason for this implementation.
@@ -6,10 +6,13 @@ | |||
*/ | |||
public class Bulldozer extends Machine { | |||
public Bulldozer() { |
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.
Don't remove the no-args constructor, but also make sure it doesn't contain unnecessary logic as currently seen in line 9.
|
||
public class BulldozerProducer implements MachineProducer<Bulldozer> { | ||
@Override | ||
public List<? extends Machine> get() { |
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.
The return type of the get
method should be parametrized with Bulldozer
, not the raw Machine
type to avoid using raw types and to ensure type safety.
public class BulldozerProducer implements MachineProducer<Bulldozer> { | ||
@Override | ||
public List<? extends Machine> get() { | ||
Machine machine = new Bulldozer(); |
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.
There's no need to declare a Machine
type variable; you can directly create a list of Bulldozer
objects. Consider creating a local variable of type List<Bulldozer>
to store the bulldozers and return it.
} | ||
|
||
@Override | ||
public void fill(List<Object> machines, Machine value) { |
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.
The 'fill' method should use List as a parameter instead of List to ensure type safety and avoid potential class cast issues at runtime.
public void startWorking(List<? extends Machine> machines) { | ||
if (machines.contains(truck)) { | ||
truck.doWork(); | ||
} else if (machines.contains(excavator)) { | ||
excavator.doWork(); | ||
} else if (machines.contains(bulldozer)) { | ||
bulldozer.doWork(); | ||
} |
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.
The 'startWorking' method logic is incorrect. It should iterate over the list of machines and call 'doWork()' on each machine. The current implementation checks if the list contains one of the specific machines created as fields, which is not the intended behavior.
public void startWorking(List<? extends Machine> machines) { | ||
if (machines.contains(truck)) { | ||
truck.doWork(); | ||
} else if (machines.contains(excavator)) { | ||
excavator.doWork(); | ||
} else if (machines.contains(bulldozer)) { | ||
bulldozer.doWork(); | ||
} |
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.
The fields 'truck', 'excavator', and 'bulldozer' are not used correctly. They are not needed as fields and should be removed. The 'startWorking' method should work with the passed list of machines.
private final Machine bulldozer = new Bulldozer(); | ||
|
||
@Override | ||
public List<Machine> getAll(Class<? extends Machine> type) { |
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.
The 'getAll' method should return a list of the specified type 'T', not the raw 'Machine' type. Adjust the return type and the logic to comply with the generic type 'T'.
} else if (Excavator.class.isAssignableFrom(type)) { | ||
machines.addAll(excavatorProducer); | ||
} | ||
return machines; |
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.
The 'getAll' method should return an empty list instead of null when no producer has been found for the given type. Make sure to handle this case correctly.
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.
Hey there! You've done a great job with the functionality, but we need to ensure our code is safely typed. Let's make sure to use generics properly where indicated, especially with the fill()
method and MachineService
interface, to maintain type safety. Also, consider adding specific fields to the Truck
class to represent its unique attributes. Keep up the good work, and with these small tweaks, your code will be even more robust! 👍🛠️
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
import core.mate.academy.service.MachineProducer; | ||
import java.util.List; | ||
|
||
public class BulldozerProducer implements MachineProducer<Bulldozer> { |
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.
Don't begin class or method implementation with empty line. Remove the empty line at the start of the class definition.
import core.mate.academy.service.MachineProducer; | ||
import java.util.List; | ||
|
||
public class ExcavatorProducer implements MachineProducer<Excavator> { |
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.
Don't begin class or method implementation with an empty line. Remove the empty line at the beginning of the class definition.
package core.mate.academy.model; | ||
|
||
/** | ||
* Add some custom fields that could be only in Truck | ||
* Do not remove no-field constructor | ||
*/ | ||
|
||
public class Truck extends Machine { |
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.
Don't forget about access modifiers for your custom fields in Truck
class. You should define fields with private access modifiers and provide public getters and setters for accessing them.
package core.mate.academy.model; | ||
|
||
/** | ||
* Add some custom fields that could be only in Truck | ||
* Do not remove no-field constructor | ||
*/ | ||
|
||
public class Truck extends Machine { |
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.
The class Truck
should contain custom fields that represent the state of a truck. For example, you might have fields such as loadCapacity
, engineType
, etc. Make sure to add these fields with appropriate access modifiers and methods (getters/setters).
import core.mate.academy.service.MachineProducer; | ||
import java.util.List; | ||
|
||
public class TruckProducer implements MachineProducer<Truck> { |
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.
Don't begin class or method implementation with an empty line. Remove the empty line before the class declaration.
@@ -16,7 +17,7 @@ public interface MachineService { | |||
* @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); | |||
List<T> getAll(Class<? extends T> type); |
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.
Consider parameterizing the MachineService
interface to ensure that the T
type is always a subtype of Machine
. For example, MachineService<T extends Machine>
.
/** | ||
* Your implementation of MachineService. | ||
*/ | ||
public class MachineServiceImpl { | ||
public class MachineServiceImpl<T extends Machine> implements MachineService<T> { |
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.
Don't begin class or method implementation with empty lines (lines 13-15).
public List<T> getAll(Class<? extends T> type) { | ||
List<T> machines = new ArrayList<>(); | ||
|
||
if (Truck.class.isAssignableFrom(type)) { | ||
machines.addAll((List<T>) new TruckProducer().get()); | ||
} else if (Bulldozer.class.isAssignableFrom(type)) { | ||
machines.addAll((List<T>) new BulldozerProducer().get()); | ||
} else if (Excavator.class.isAssignableFrom(type)) { | ||
machines.addAll((List<T>) new ExcavatorProducer().get()); | ||
} | ||
|
||
return machines; |
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.
Consider creating a local variable in your implementation of getAll(Class type)
method to hold the result from the producer's get()
method before adding it to the machines
list.
public void fill(List<Object> machines, Machine value) { | ||
machines.replaceAll(ignored -> value); |
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.
The fill()
method should use generics for its machines
parameter to ensure type safety. Please change the parameter type from List<Object>
to List<T>
where T
is a bounded type parameter extending Machine
.
public void fill(List<Object> machines, Machine value) { | ||
machines.replaceAll(ignored -> value); |
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.
The size of a list passed to fill()
method should remain the same. Just replace the old values with the new ones. This implementation is correct, but please ensure the generic type is used for the method parameter.
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.
Great job on implementing the BulldozerProducer! 👍 Remember to address the mentioned issues to improve code quality and maintainability. Keep learning and coding!
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
/** | ||
* Add some custom fields that could be only in Bulldozer | ||
* Do not remove no-args constructor | ||
*/ | ||
public class Bulldozer extends Machine { |
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.
Don't begin class or method implementation with an empty line. Please remove the empty line before the class declaration.
/** | ||
* Add some custom fields that could be only in Bulldozer | ||
* Do not remove no-args constructor | ||
*/ | ||
public class Bulldozer extends Machine { |
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.
Don't forget about access modifiers for your custom fields in Bulldozer
class. It's good practice to use private access modifiers and provide getters and setters for accessing these fields.
@@ -4,6 +4,7 @@ | |||
* Add some custom fields that could be only in Excavator | |||
* Do not remove no-args constructor | |||
*/ | |||
|
|||
public class Excavator extends Machine { |
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.
Don't begin class implementation with an empty line. According to the checklist, the class definition should not be preceded by an empty line.
import core.mate.academy.service.MachineProducer; | ||
import java.util.List; | ||
|
||
public class ExcavatorProducer implements MachineProducer<Excavator> { |
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.
Don't begin class or method implementation with an empty line.
@@ -4,6 +4,7 @@ | |||
* Add some custom fields that could be only in Truck | |||
* Do not remove no-field constructor | |||
*/ | |||
|
|||
public class Truck extends Machine { |
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.
Don't begin class or method implementation with an empty line. It's good practice to start the class body immediately after the class declaration line to avoid unnecessary whitespace.
|
||
void fill(List<Object> machines, Machine value); |
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.
The fill
method should have a bounded wildcard for the machines
list to ensure type safety. Use List<? super T>
to allow lists of any supertype of T
.
/** | ||
* Your implementation of MachineService. | ||
*/ | ||
public class MachineServiceImpl { | ||
|
||
public class MachineServiceImpl<T extends Machine> implements MachineService<T> { |
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.
The class 'MachineServiceImpl' should not start with an empty line according to the common mistakes checklist.
public class MachineServiceImpl<T extends Machine> implements MachineService<T> { | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public List<T> getAll(Class<? extends Machine> type) { |
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.
Consider creating a local variable for the result of the 'get()' call to avoid casting the list multiple times. This would improve readability and maintainability of the code.
} | ||
|
||
@Override | ||
public void fill(List<Object> machines, Machine value) { |
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.
The 'fill()' method should use a List of type T, not Object, to ensure type safety. Please correct the method signature to 'List machines' and adjust the implementation accordingly.
machines.addAll((List<T>) new ExcavatorProducer().get()); | ||
} | ||
|
||
return machines.isEmpty() ? List.of() : machines; |
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.
Keep if-else constructions simple. There is no need for an else clause after a return statement. You can remove the else to flatten the structure of the code.
|
||
List<T> getAll(Class<? extends Machine> type); |
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.
List<T> getAll(Class<? extends Machine> type); | |
List<T> getAll(Class<? extends T> type); |
|
||
void fill(List<Object> machines, Machine value); |
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.
void fill(List<Object> machines, Machine 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 Machine> machines); |
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.
void startWorking(List<? extends Machine> machines); | |
void startWorking(List<? extends T> machines); |
/** | ||
* Your implementation of MachineService. | ||
*/ | ||
public class MachineServiceImpl { | ||
|
||
public class MachineServiceImpl<T extends Machine> implements MachineService<T> { |
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.
public class MachineServiceImpl<T extends Machine> implements MachineService<T> { | |
public class MachineServiceImpl implements MachineService<Machine> { |
@SuppressWarnings("unchecked") | ||
@Override | ||
public List<T> getAll(Class<? extends Machine> type) { | ||
List<T> machines = new ArrayList<>(); |
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.
List<T> machines = new ArrayList<>(); | |
List<? extends Machine> machines = null; | |
if (type == Bulldozer.class) { | |
machines = bulldozerProducer.get(); | |
}...... |
} | ||
|
||
@Override | ||
public void fill(List<Object> machines, Machine value) { |
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.
public void fill(List<Object> machines, Machine value) { | |
public void fill(List<? super Machine> machines, Machine value) { | |
for (int i = 0; i < machines.size(); i++) { | |
machines.set(i, value); | |
} | |
} |
No description provided.