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

Created nessesery classes, changed Objects to PECS. #901

Open
wants to merge 3 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
3 changes: 3 additions & 0 deletions src/main/java/core/mate/academy/model/Bulldozer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* Do not remove no-args constructor
*/
public class Bulldozer extends Machine {

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. Remove the unnecessary empty lines between the package declaration and the class declaration.

private int fuelConsuptionPerHour;

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. The field fuelConsuptionPerHour should be private and accessed through getters and setters.


public Bulldozer() {
this.fuelConsuptionPerHour = 40;
}

@Override
Expand Down
19 changes: 19 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,19 @@
package core.mate.academy.model;

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

public class BulldozerProducer implements MachineProducer<Bulldozer> {

@Override
public List<Bulldozer> get() {
List<Bulldozer> bulldozer = new ArrayList<>();

bulldozer.add(new Bulldozer());
bulldozer.add(new Bulldozer());
bulldozer.add(new Bulldozer());

return bulldozer;
}
}
3 changes: 3 additions & 0 deletions src/main/java/core/mate/academy/model/Excavator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* Do not remove no-args constructor
*/
public class Excavator extends Machine {
private int shovelSize;

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 Excavator class. You should never want to expose the object fields directly. They should be accessed through special methods (getters and setters).


public Excavator() {
this.shovelSize = 10;
}

@Override
Expand Down
19 changes: 19 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,19 @@
package core.mate.academy.model;

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

public class ExcavatorProducer implements MachineProducer<Excavator> {

@Override
public List<Excavator> get() {
List<Excavator> excavator = new ArrayList<>();

excavator.add(new Excavator());
excavator.add(new Excavator());
excavator.add(new Excavator());

return excavator;
}
}
3 changes: 3 additions & 0 deletions src/main/java/core/mate/academy/model/Truck.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 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.

According to the checklist, you should never want to expose the object fields directly. They should be accessed through special methods (getters and setters). Please add getter and setter methods for the maxCapacity field.

private int maxCapacity;

public Truck() {
this.maxCapacity = 5000;
}

@Override
Expand Down
19 changes: 19 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,19 @@
package core.mate.academy.model;

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

public class TruckProducer implements MachineProducer<Truck> {

@Override
public List<Truck> get() {
List<Truck> truck = new ArrayList<>();

truck.add(new Truck());
truck.add(new Truck());
truck.add(new Truck());

return truck;
}
}
6 changes: 4 additions & 2 deletions src/main/java/core/mate/academy/service/MachineProducer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package core.mate.academy.service;

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

public interface MachineProducer {
public interface MachineProducer<T extends Machine> {
/**

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.

* 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,11 +1,12 @@
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 extends Machine> {

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. Remove the unnecessary empty lines before the interface declaration.


/**
* Return the list of machines.
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<Machine> getAll(Class<? extends Machine> type);

Choose a reason for hiding this comment

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

Do not use raw types. You should parameterize the return type of getAll method to match the type parameter of the MachineService interface. This will ensure type safety.


/**
* 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<? 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.

The fill method should use the generic type T instead of Machine for the List parameter to ensure that the list and the value are of the same type.


/**
* 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);
}
45 changes: 44 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,50 @@
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 implements MachineService<Machine> {

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. There are unnecessary empty lines at the start of the class implementation.


@Override
public List<Machine> getAll(Class<? extends Machine> type) {

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. It's good practice to create a local variable for the result list, which will be returned at the end of the method. This makes the code more readable and maintainable.

MachineProducer<? extends Machine> producer;

if (type == Bulldozer.class) {
producer = new BulldozerProducer();
} else if (type == Excavator.class) {
producer = new ExcavatorProducer();
} else if (type == Truck.class) {
producer = new TruckProducer();
} else {
return new ArrayList<>();

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. Instead of returning a new ArrayList<>(); directly, it's better to assign it to a local variable and return that variable. This ensures consistency and readability in the method's return points.

}

List<? extends Machine> specifyMachines = producer.get();

Choose a reason for hiding this comment

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

There is no need to have a separate variable specifyMachines. You can directly assign the result of producer.get() to allMachines and return it, simplifying the code.

List<Machine> allMachines = new ArrayList<>(specifyMachines);
return allMachines;

Choose a reason for hiding this comment

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

The variable allMachines is unnecessary since you can return the new ArrayList<>(specifyMachines) directly. This will reduce the amount of code and improve readability.

}

@Override
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 Machine> machines) {
for (Machine element : machines) {
element.doWork();
}
}
}
Loading