Skip to content

Instantly share code, notes, and snippets.

@matthiasnoback
Last active April 17, 2018 18:06
Show Gist options
  • Save matthiasnoback/1ace8021efa7a6548ab15cfcc12b603a to your computer and use it in GitHub Desktop.
Save matthiasnoback/1ace8021efa7a6548ab15cfcc12b603a to your computer and use it in GitHub Desktop.
Modeling exercise: Quantities
<?php
// provided as method arguments:
$quantityInCurrentDelivery = 10.0;
// product-specific:
$quantityPrecision = 5;
// these quantities are all floats:
$quantityInitiallyOrdered = 12.5;
$quantitySoFarDelivered = 3.5;
$quantityOpen = 9.0;
// when we accept a delivery:
$quantitySoFarDelivered = round($quantitySoFarDelivered + $quantityInCurrentDelivery, $quantityPrecision);
$quantityOpen = $quantityInitiallyOrdered - $quantityInCurrentDelivery;
if ($quantityOpen < 0) {
// over-delivering is fine, but we'll set quantity open to 0
$quantityOpen = 0.0;
}
// when we have to undo a delivery:
$quantitySoFarDelivered = round($quantitySoFarDelivered - $quantityInCurrentDelivery, $quantityPrecision);
if ($quantitySoFarDelivered < 0) {
$quantitySoFarDelivered = 0.0;
}
$quantityOpen = round($quantityOpen + $quantityInCurrentDelivery, $quantityPrecision);
if ($quantityOpen > $quantityInitiallyOrdered) {
// maybe we have been over-delivered, but we shouldn't expect more than we ordered
$quantityOpen = $quantityInitiallyOrdered;
}
@matthiasnoback
Copy link
Author

matthiasnoback commented Apr 17, 2018

Design issues that should be fixed by introducing value objects:

  • Dealing with "quantity precision" (i.e. number of decimals taken into account).
  • Preventing "quantity so far delivered" to end up being less than 0.
  • Preventing "quantity open" to end up being more than "quantity initially ordered".

Question: what is the relation between these different quantities? Which ones do we need to "remember" (i.e. store in a database, etc.), which ones can we derive?

@matthiasnoback
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment