Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Last active October 8, 2020 19:37
Show Gist options
  • Save pawelpluta/2f49227541fc2d3ba7d804e5204bd163 to your computer and use it in GitHub Desktop.
Save pawelpluta/2f49227541fc2d3ba7d804e5204bd163 to your computer and use it in GitHub Desktop.
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