Forked from 18-F-cali/WeighingMachinePossibleSolution.cs
Last active
September 28, 2021 09:50
-
-
Save ErikSchierboom/f0a453cf9e8f2a2c03311b6796e9f31b to your computer and use it in GitHub Desktop.
Weighing machine possible solution
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
using System; | |
class WeighingMachine | |
{ | |
private int _weight; | |
public WeighingMachine(int precision) | |
{ | |
Precision = precision; | |
} | |
// Goal: getter-only property initialized in constructor | |
public int Precision { get; } | |
// Goal: auto-implemented get/set property with initial value | |
public int TareAdjustment { get; set; } = 5; | |
// Goal: explicit get/set property | |
public int Weight | |
{ | |
get | |
{ | |
return _weight; | |
} | |
set | |
{ | |
if(value < 0) throw new ArgumentOutOfRangeException(); | |
_weight = value; | |
} | |
} | |
// Goal: getter-only property with logic | |
public int DisplayWeight | |
{ | |
get | |
{ | |
return Weight - TareAdjustment; | |
} | |
} | |
} |
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
using System; | |
using Xunit; | |
using Exercism.Tests; | |
public class WeighingMachineTests | |
{ | |
[Fact] | |
public void Set_weight_and_get_weight() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.InputWeight = 60.001m; | |
Assert.Equal(60.001m, wm.InputWeight, precision: 3); | |
} | |
[Fact] | |
public void Negative_weight_is_invalid() | |
{ | |
var wm = new WeighingMachine(3); | |
Assert.Throws<ArgumentOutOfRangeException>(() => wm.InputWeight = -10); | |
} | |
[Fact] | |
public void Default_Unit_Is_Kilogram() | |
{ | |
var wm = new WeighingMachine(3); | |
Assert.Equal(Unit.Kilograms, wm.Unit); | |
} | |
[Fact] | |
public void Get_us_display_weight_pounds() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.InputWeight = 60m; | |
Assert.Equal(132.277m, wm.Pounds, precision: 3); | |
} | |
[Fact] | |
public void Get_us_display_weight_ounces() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.InputWeight = 60m; | |
Assert.Equal(2116.440m, wm.Ounces, precision: 3); | |
} | |
[Fact] | |
public void Input_pounds_and_get_us_display_weight_pounds() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Pounds; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(175.500m, wm.Pounds, precision: 3); | |
} | |
[Fact] | |
public void Input_pounds_and_get_is_display_weight_ounces() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Pounds; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(2808.000m, wm.Ounces, precision: 3); | |
} | |
[Fact] | |
public void Input_pounds_and_get_is_display_weight_Kilograms() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Pounds; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(79.605m, wm.Kilograms, precision: 3); | |
} | |
[Fact] | |
public void Input_ounces_and_get_is_display_weight_ounces() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Ounces; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(175.500m, wm.Ounces, precision: 3); | |
} | |
[Fact] | |
public void Input_ounces_and_get_us_display_weight_pounds() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Ounces; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(10.969m, wm.Pounds, precision: 3); | |
} | |
[Fact] | |
public void Input_ounces_and_get_is_display_weight_Kilograms() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Ounces; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(4.975m, wm.Kilograms, precision: 3); | |
} | |
[Fact] | |
public void Input_Kilograms_and_get_is_display_weight_Kilograms() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Kilograms; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(175.500m, wm.Kilograms, precision: 3); | |
} | |
[Fact] | |
public void Input_Kilograms_and_get_us_display_weight_pounds() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Kilograms; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(386.911m, wm.Pounds, precision: 3); | |
} | |
[Fact] | |
public void Input_Kilograms_and_get_is_display_weight_ounces() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.Unit = Unit.Kilograms; | |
wm.InputWeight = 175.500m; | |
Assert.Equal(6190.587m, wm.Ounces, precision: 3); | |
} | |
[Fact] | |
public void Apply_tare_adjustment_and_get_display_weight() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.InputWeight = 100m; | |
wm.TareAdjustment = 10.123m; | |
Assert.Equal(89.877m, wm.DisplayWeight, precision: 3); | |
} | |
[Fact] | |
public void Apply_negative_tare_adjustment() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.InputWeight = 100m; | |
wm.TareAdjustment = -10.123m; | |
Assert.Equal(89.877m, wm.DisplayWeight, precision: 3); | |
} | |
[Fact] | |
public void Apply_large_tare_adjustment_to_allow_negative_display_weight() | |
{ | |
var wm = new WeighingMachine(3); | |
wm.InputWeight = 100m; | |
wm.TareAdjustment = 110m; | |
Assert.Equal(-10.000m, wm.DisplayWeight, precision: 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment