Last active
April 17, 2018 18:06
-
-
Save matthiasnoback/1ace8021efa7a6548ab15cfcc12b603a to your computer and use it in GitHub Desktop.
Modeling exercise: Quantities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Design issues that should be fixed by introducing value objects:
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?