|
1 | 1 | # Instructions
|
2 | 2 |
|
3 |
| -In this exercise you'll be modelling a weighing machine. |
| 3 | +In this exercise you'll be modelling a weighing machine with Kilograms as a Unit. |
4 | 4 |
|
5 |
| -The weight can be set and retrieved in pounds or kilograms and cannot be negative. |
| 5 | +You have 6 tasks each of which requires you to implement one or more properties: |
6 | 6 |
|
7 |
| -The weight can be displayed in SI units or US units |
8 |
| -, pounds and ounces. |
| 7 | +## 1. Allow the weighing machine to have a precision |
9 | 8 |
|
10 |
| -A tare adjustment can be applied to the weight (for instance to deduct the |
11 |
| -weight of a container). This can be any value (even negative or a value that makes the display weight negative) |
12 |
| -as there are doubts about the accuracy |
13 |
| -of the weighing machine. For security reasons this value cannot be retrieved. |
14 |
| - |
15 |
| -Note that: |
16 |
| - |
17 |
| -``` |
18 |
| -display-weight = input-weight - tare-adjustment |
19 |
| -``` |
20 |
| - |
21 |
| -Conversion ratios are as follows: |
22 |
| - |
23 |
| -- 16 ounces to a pound |
24 |
| -- 2.20462 kg to a pound |
25 |
| - |
26 |
| -For Example: |
27 |
| - |
28 |
| -- 60 kilograms == 132.2772 ponds |
29 |
| -- 132.2772 pounds == 132 pounds 4 ounces |
30 |
| - |
31 |
| -You have 5 tasks each of which requires you to implement one or |
32 |
| -more properties: |
33 |
| - |
34 |
| -## 1. Allow the weight to be set on the weighing machine |
35 |
| - |
36 |
| -Implement the `WeigingMachine.InputWeight` property to allow the weight to be get and set: |
| 9 | +To cater to different demands, we allow each weighing machine to be customized with a precision (the number of digits after the decimal separator). |
| 10 | +Implement the `WeigingMachine` class to have a get-only `Precision` property set to the constructor's `precision` argument: |
37 | 11 |
|
38 | 12 | ```csharp
|
39 |
| -var wm = new WeighingMachine(); |
40 |
| -wm.InputWeight = 60m; |
| 13 | +var wm = new WeighingMachine(precision: 3); |
41 | 14 |
|
42 |
| -// => wm.InputWeight == 60m |
| 15 | +// => wm.Precision == 3 |
43 | 16 | ```
|
44 | 17 |
|
45 |
| -## 2. Allow specifying the unit used by the weighing machine |
46 |
| - |
47 |
| -Implement the `WeigingMachine.Unit` property to allow the unit to be set. `Unit.Kilograms` should be the default value: |
| 18 | +## 2. Allow the weight to be set on the weighing machine |
48 | 19 |
|
| 20 | +Implement the `WeigingMachine.Weight` property to allow the weight to be get _and_ set: |
49 | 21 |
|
50 | 22 | ```csharp
|
51 |
| -var wm = new WeighingMachine(); |
| 23 | +var wm = new WeighingMachine(precision: 3); |
| 24 | +wm.Weight = 60.5; |
52 | 25 |
|
53 |
| -// => wm.Unit == Kilograms |
| 26 | +// => wm.Weight == 60.5 |
54 | 27 | ```
|
55 | 28 |
|
56 | 29 | ## 3. Ensure that a negative input weight is rejected
|
57 | 30 |
|
58 |
| -Add validation to the `WeighingMachine.InputWeight` property to throw an `ArgumentOutOfRangeException` when trying to set it to a negative weight: |
| 31 | +Clearly, someone cannot have a negative weight. |
| 32 | +Add validation to the `WeighingMachine.Weight` property to throw an `ArgumentOutOfRangeException` when trying to set it to a negative weight: |
59 | 33 |
|
60 | 34 | ```csharp
|
61 |
| -var wm = new WeighingMachine(); |
62 |
| -wm.InputWeight = -10m; // Throws an ArgumentOutOfRangeException |
| 35 | +var wm = new WeighingMachine(precision: 3); |
| 36 | +wm.Weight = -10; // Throws an ArgumentOutOfRangeException |
63 | 37 | ```
|
64 | 38 |
|
65 |
| -## 4. Allow the US weight to be retrieved |
| 39 | +## 4. Allow a tare adjustment to be applied to the weighing machine |
66 | 40 |
|
67 |
| -Implement the `WeighingMachine.USDisplayWeight` property and the `USWeight` class: |
| 41 | +The tare adjustment can be any value (even negative or a value that makes the display weight negative) |
| 42 | +Implement the `WeighingMachine.TareAdjustment` property to allow the tare adjustment to be set: |
68 | 43 |
|
69 | 44 | ```csharp
|
70 |
| -var wm = new WeighingMachine(); |
71 |
| -wm.InputWeight = 60m; |
| 45 | +var wm = new WeighingMachine(precision: 3); |
| 46 | +wm.TareAdjustment = -10.6; |
72 | 47 |
|
73 |
| -var usw = wm.USDisplayWeight; |
74 |
| -// => usw.Pounds == 132 && usw.Ounces == 4 |
| 48 | +// => wm.TareAdjustment == -10.6 |
75 | 49 | ```
|
76 | 50 |
|
77 |
| -## 5. Allow the machine's unit to be set to pounds |
| 51 | +## 5. Ensure that the weighing machine has a default tare adjustment |
78 | 52 |
|
79 |
| -Implement the `WeighingMachine.Unit` property: |
| 53 | +After some thorough testing, it appears that due to a manifacturing issue all weighing machines have a bias towards overestimating the weight by `5`. |
| 54 | +Change the `WeighingMachine.TareAdjustment` property to `5` as its default value. |
80 | 55 |
|
81 | 56 | ```csharp
|
82 |
| -var wm = new WeighingMachine(); |
83 |
| -wm.InputWeight = 175.5m; |
84 |
| -wm.Unit = Unit.Pounds; |
| 57 | +var wm = new WeighingMachine(precision: 3); |
85 | 58 |
|
86 |
| -var usw = wm.USDisplayWeight; |
87 |
| -// => usw.Pounds == 175 && usw.Ounces == 8 |
| 59 | +// => wm.TareAdjustment == 5.0 |
88 | 60 | ```
|
89 | 61 |
|
90 |
| -## 6. Allow a tare adjustment to be applied to the weighing machine |
| 62 | +## 6. Allow the weight to be retrieved |
91 | 63 |
|
92 |
| -Implement the `WeighingMachine.TareAdjustment` and `WeighingMachine.DisplayWeight` properties: |
| 64 | +Implement the `WeighingMachine.DisplayWeight` property which shows weight after tare adjustment and with the correct precision applied: |
| 65 | +Note that: |
| 66 | +``` display-weight = input-weight - tare-adjustment ``` |
93 | 67 |
|
94 | 68 | ```csharp
|
95 |
| -var wm = new WeighingMachine(); |
96 |
| -wm.InputWeight = 100m; |
97 |
| -wm.TareAdjustment = 10m; |
| 69 | +var wm = new WeighingMachine(precision: 3); |
| 70 | +wm.TareAdjustment = 10; |
| 71 | +wm.Weight = 60.567; |
98 | 72 |
|
99 |
| -// => wm.DisplayWeight == 90m |
100 |
| -``` |
| 73 | +// => wm.DisplayWeight == "50.567 kg" |
| 74 | +``` |
0 commit comments