-
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
Created nessesery classes, changed Objects to PECS. #901
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
private int fuelConsuptionPerHour; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget about access modifiers for your custom fields in |
||
|
||
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget about access modifiers for your custom fields in |
||
|
||
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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(); | ||
} |
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
/** | ||
* 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); | ||
} |
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<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't return null or specific type List in |
||
} | ||
|
||
return (List<Machine>) producer.get(); | ||
} | ||
Comment on lines
+15
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use over-complicated @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(); | ||
} | ||
} | ||
} |
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. Remove the unnecessary empty lines between the package declaration and the class declaration.