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 all 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
19 changes: 15 additions & 4 deletions src/main/java/core/mate/academy/model/Bulldozer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
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 {

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() {
}

public Bulldozer(String name, String color, Integer fuelConsuptionPerHour) {
super(name, color);
this.fuelConsuptionPerHour = fuelConsuptionPerHour;
}

@Override
public void doWork() {
System.out.println("Bulldozer started to work");
}

public int getFuelConsuptionPerHour() {
return fuelConsuptionPerHour;
}

public void setFuelConsuptionPerHour(int fuelConsuptionPerHour) {
this.fuelConsuptionPerHour = fuelConsuptionPerHour;
}
}
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("TitanForce 900", "Black", 52));
bulldozer.add(new Bulldozer("EarthMover X5", "Red", 55));
bulldozer.add(new Bulldozer("TerraMaster 5000", "Orange", 70));

return bulldozer;
}
}
20 changes: 16 additions & 4 deletions src/main/java/core/mate/academy/model/Excavator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
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 {
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() {

}

public Excavator(String name, String color, Integer shovelSize) {
super(name, color);
this.shovelSize = shovelSize;
}

@Override
public void doWork() {
System.out.println("Excavator started to work");
}

public int getShovelSize() {
return shovelSize;
}

public void setShovelSize(int shovelSize) {
this.shovelSize = shovelSize;
}
}
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("DigMaster 300", "Purple", 5));
excavator.add(new Excavator("Excavator ZX7", "Cyan", 4));
excavator.add(new Excavator("GroundBreaker X1", "Lime", 6));

return excavator;
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/mate/academy/model/Machine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ public abstract class Machine implements Workable {
private String name;
private String color;

public Machine() {

}

public Machine(String name, String color) {
this.name = name;
this.color = color;
}

public String getName() {
return name;
}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/core/mate/academy/model/Truck.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
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.

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() {
}

public Truck(String name, String color, Integer maxCapacity) {
super(name, color);
this.maxCapacity = maxCapacity;
}

@Override
public void doWork() {
System.out.println("Truck started to work");
}

public int getMaxCapacity() {
return maxCapacity;
}

public void setMaxCapacity(int maxCapacity) {
this.maxCapacity = maxCapacity;
}
}
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("HeavyHaul Pro XT", "Pink", 5000));
truck.add(new Truck("MaxLoad 750", "Gray", 7000));
truck.add(new Truck("GigaTransporter S4", "Yellow", 6500));

return truck;
}
}
9 changes: 3 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,8 @@
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();
}
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 T> type);

/**
* 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 T> machines);
}
46 changes: 42 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,45 @@
package core.mate.academy.service;

/**
* Your implementation of MachineService.
*/
public class MachineServiceImpl {
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;

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

@Override
public List<Machine> getAll(Class<? extends T> type) {
MachineProducer<? extends T> producer;

if (type == Bulldozer.class) {
producer = (MachineProducer<? extends T>) new BulldozerProducer();
} else if (type == Excavator.class) {
producer = (MachineProducer<? extends T>) new ExcavatorProducer();
} else if (type == Truck.class) {
producer = (MachineProducer<? extends T>) 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.

}

return (List<Machine>) producer.get();
}
Comment on lines +15 to +30

Choose a reason for hiding this comment

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

Do not use over-complicated if-else constuction, bette to use switch here, for example:

    @Override
    public List<Machine> getAll(Class<? extends Machine> type) {
        List<? extends Machine> machines;
        switch (type.getSimpleName()) {
            case "Bulldozer" -> machines = new BulldozerProducerImpl().get();
            case "Excavator" -> machines = new ExcavatorProducerImpl().get();
            case "Truck" -> machines = new TruckProducerImpl().get();
            default -> {
                return Collections.emptyList();
            }
        }
        return new ArrayList<>(machines);
    }


@Override
public void fill(List<? super T> machines, T value) {
for (int i = 0; i < machines.size(); i++) {
machines.set(i, value);
}
}

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