Skip to content

Instantly share code, notes, and snippets.

@pawelpluta

pawelpluta/WashingService.java Secret

Last active Oct 8, 2020
Embed
What would you like to do?
Article: The OOP has been explained wrongly to you
class WashingService {
private static final BigDecimal MAX_LAUNDRY_WEIGHT = BigDecimal.valueOf(7);
Collection<LaundryElement> washWool(WashingMachine washingMachine,
Collection<LaundryElement> laundry,
Integer spinSpeed,
Integer temperature) {
List<LaundryElement> laundryToBeDone = maximumLoadFrom(laundry, WOOL);
washingMachine.setLaundry(laundryToBeDone);
if (spinSpeed >= 0 && spinSpeed <= 400) {
washingMachine.setSpinSpeed(spinSpeed);
} else {
washingMachine.setSpinSpeed(400);
}
if (temperature >= 0 && temperature <= 40) {
washingMachine.setTemperature(temperature);
} else {
washingMachine.setTemperature(40);
}
washingMachine.wash();
return washingMachine.getLaundry();
}
Collection<LaundryElement> washCotton(WashingMachine washingMachine, Collection<LaundryElement> laundry, Integer spinSpeed, Integer temperature) {
// similar implementation as in washWool
}
Collection<LaundryElement> washSilk(WashingMachine washingMachine, Collection<LaundryElement> laundry, Integer spinSpeed, Integer temperature) {
// similar implementation as in washWool
}
private List<LaundryElement> maximumLoadFrom(Collection<LaundryElement> laundry,
Fabric fabricType) {
List<LaundryElement> laundryToBeDone = new ArrayList<>();
laundry.stream()
.filter(laundryElement -> laundryElement.getType().equals(fabricType))
.forEach(nextLaundryElement -> {
if (canFitAnotherLaundry(laundryToBeDone, nextLaundryElement)) {
laundryToBeDone.add(nextLaundryElement);
}
});
return laundryToBeDone;
}
private boolean canFitAnotherLaundry(List<LaundryElement> laundryToBeDone,
LaundryElement nextLaundryElement) {
return totalWeightOf(laundryToBeDone).add(nextLaundryElement.getWeight())
.compareTo(MAX_LAUNDRY_WEIGHT) <= 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment