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

solved task #909

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions src/main/java/core/mate/academy/model/Bulldozer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package core.mate.academy.model;

/**
* Add some custom fields that could be only in Bulldozer
* Do not remove no-args constructor
*/
public class Bulldozer extends Machine {
public Bulldozer() {
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/core/mate/academy/model/Excavator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package core.mate.academy.model;

/**
* Add some custom fields that could be only in Excavator
* Do not remove no-args constructor
*/
public class Excavator extends Machine {
public Excavator() {
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/core/mate/academy/model/Truck.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
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 {
public Truck() {
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/core/mate/academy/service/BulldozerProducer.java
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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/core/mate/academy/service/ExcavatorProducer.java
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;
}
}
10 changes: 4 additions & 6 deletions src/main/java/core/mate/academy/service/MachineProducer.java
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();
}
37 changes: 6 additions & 31 deletions src/main/java/core/mate/academy/service/MachineService.java
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);
}
38 changes: 34 additions & 4 deletions src/main/java/core/mate/academy/service/MachineServiceImpl.java
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> {

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

@Override
public 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.

Suggested change
public List<T> getAll(Class<? extends T> type) {
public List<Machine> getAll(Class<? extends Machine> type)

if (type == Bulldozer.class) {

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

Choose a reason for hiding this comment

The 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<>();

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. Directly returning the result of new BulldozerProducer().get() is not recommended, as it does not allow for easy debugging or extension of the method's functionality. Instead, consider creating a local variable to hold the result before returning it.

Choose a reason for hiding this comment

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

Don't return null or specific type List in getAll() when no producer has been found. It's better to return an empty List to avoid potential null pointer exceptions and to provide a consistent API to the users of your method.

}

@Override
public void fill(List<? super T> machines, T value) {

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<? super T> machines, T value) {
public void fill(List<? super Machine> machines, Machine value) {

for (int i = 0; i < machines.size(); i++) {
machines.set(i, value);
}
}

@Override
public void startWorking(List<? extends T> machines) {

Choose a reason for hiding this comment

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

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

for (T machine : machines) {
machine.doWork();
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/core/mate/academy/service/TruckProducer.java
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());

Choose a reason for hiding this comment

The 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;
}
}
Loading