Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/core/mate/academy/model/BulldozerProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.mate.academy.model;

import core.mate.academy.service.MachineProducer;
import java.util.List;

public class BulldozerProducer implements MachineProducer<Bulldozer> {

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.

@Override
public List<Bulldozer> get() {
return List.of(new Bulldozer());
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/mate/academy/model/ExcavatorProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.mate.academy.model;

import core.mate.academy.service.MachineProducer;
import java.util.List;

public class ExcavatorProducer implements MachineProducer<Excavator> {

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.

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.

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.

@Override
public List<Excavator> get() {
return List.of(new Excavator());
}
}
2 changes: 2 additions & 0 deletions src/main/java/core/mate/academy/model/Truck.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

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 {

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.

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).

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.

public Truck() {
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/core/mate/academy/model/TruckProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.mate.academy.model;

import core.mate.academy.service.MachineProducer;
import java.util.List;

public class TruckProducer implements MachineProducer<Truck> {

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. The empty line after the class declaration is unnecessary and should be removed.

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.

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 start of the class definition.

@Override
public List<Truck> get() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't begin method implementation with an empty line. Remove the empty line before the method declaration.

return List.of(new Truck());

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 list of machines before returning it. This can make debugging easier if you need to inspect the list before it is returned or if additional processing is required in the future.

}
}
4 changes: 2 additions & 2 deletions src/main/java/core/mate/academy/service/MachineProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.List;

public interface MachineProducer {
public interface MachineProducer<T> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameterize MachineProducer in such a way that any implementation NOT using Machine or its subtype could NOT be created. You should restrict the type parameter T to extend Machine or its subclasses.

/**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't begin interface or method implementation with an empty line. It is unnecessary and does not follow Java code style conventions.

* In your implementations - create 2-3 models, add them to the list and return
* @return - the list of models
*/
List<Object> get();
List<T> get();
}
9 changes: 5 additions & 4 deletions src/main/java/core/mate/academy/service/MachineService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package core.mate.academy.service;

import core.mate.academy.model.Machine;
import java.util.List;

/**
* Parameterize this service and add its implementation in a separate class.
*/
public interface MachineService {

public interface MachineService<T> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't begin interface or method implementation with empty lines.

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. Please remove the excess empty lines to improve code readability.

/**
* Return the list of machines.
* In the implementation of this method please use your MachineProducer implementations
Expand All @@ -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);

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>.


/**
* Fill the machines list with passed value
Expand All @@ -26,13 +27,13 @@ public interface MachineService {
* @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<Object> machines, Machine value);

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 the generic type 'T' for the 'machines' list instead of 'Object'. This ensures type safety.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fill method should not use raw types. Consider parameterizing the machines list to be List<T> instead of List<Object>, ensuring type safety.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'startWorking' method is correctly using a bounded wildcard, but consider if it could be better to use the generic type 'T' as the parameter to ensure that the method works with the specific machine type that the service is intended for.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void startWorking(List<? extends Machine> machines);
void startWorking(List<? extends T> machines);

}
39 changes: 38 additions & 1 deletion src/main/java/core/mate/academy/service/MachineServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
package core.mate.academy.service;

import core.mate.academy.model.Bulldozer;
import core.mate.academy.model.BulldozerProducer;
import core.mate.academy.model.Excavator;
import core.mate.academy.model.ExcavatorProducer;
import core.mate.academy.model.Machine;
import core.mate.academy.model.Truck;
import core.mate.academy.model.TruckProducer;
import java.util.ArrayList;
import java.util.List;

/**
* Your implementation of MachineService.
*/
public class MachineServiceImpl {
public class MachineServiceImpl<T extends Machine> implements MachineService<T> {

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-16).

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).

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class MachineServiceImpl<T extends Machine> implements MachineService<T> {
public class MachineServiceImpl implements MachineService<Machine> {

@SuppressWarnings("unchecked")
@Override
public List<T> getAll(Class<? extends T> type) {
List<T> machines = new ArrayList<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List<T> machines = new ArrayList<>();
List<? extends Machine> machines = null;
if (type == Bulldozer.class) {
machines = bulldozerProducer.get();
}......


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;

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.

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.

}

@Override
public void fill(List<Object> machines, Machine value) {

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.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
}
}

machines.replaceAll(ignored -> value);
Comment on lines +37 to +38

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.

Comment on lines +37 to +38

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.

}

@Override
public void startWorking(List<? extends Machine> machines) {
for (Machine machine : machines) {
machine.doWork();
}
}
}
Loading