Skip to content

Instantly share code, notes, and snippets.

@18-F-cali
Last active September 28, 2021 09:05
Show Gist options
  • Save 18-F-cali/0cd6d13c531d5191b4a473f74a94791d to your computer and use it in GitHub Desktop.
Save 18-F-cali/0cd6d13c531d5191b4a473f74a94791d to your computer and use it in GitHub Desktop.
Weighing machine possible solution
using System;
enum Unit
{
Pounds,
Kilograms,
Ounces
}
class WeighingMachine
{
private int Precision { get; }
public WeighingMachine(int _precision)
{
Precision = _precision;
}
// TODO: define the 'Unit' property
public Unit Unit { get; set; } = Unit.Kilograms;
// TODO: define the 'Pounds' property
public decimal Pounds { get; set; }
// TODO: define the 'Ounces' property
public decimal Ounces { get; set; }
// TODO: define the 'Kilograms' property
public decimal Kilograms { get; set; }
void SetOtherUnitWeights(decimal weight, Unit unit)
{
switch (unit)
{
case Unit.Kilograms :
this.Kilograms = weight;
this.Pounds = weight * 2.20462m;
this.Ounces = weight * 35.274m;
break;
case Unit.Pounds :
this.Pounds = weight;
this.Kilograms = weight * 0.453592m;
this.Ounces = weight * 16m;
break;
case Unit.Ounces :
this.Ounces = weight;
this.Pounds = weight * 0.0625m;
this.Kilograms = weight * 0.0283495m;
break;
}
}
// TODO: define the 'InputWeight' property
public decimal InputWeight {
get
{
return this.Unit switch
{
Unit.Kilograms => Kilograms,
Unit.Pounds => Pounds,
Unit.Ounces => Ounces
};
}
set
{
if(value < 0) throw new ArgumentOutOfRangeException();
SetOtherUnitWeights(value, this.Unit);
}
}
// TODO: define the 'DisplayWeight' property
public decimal DisplayWeight => Math.Round(InputWeight - Math.Abs(TareAdjustment), 3);
// TODO: define the 'USDisplayWeight' property
public decimal USDisplayWeight
{
get
{
return this.Unit switch
{
Unit.Pounds => this.Pounds,
Unit.Ounces => this.Ounces,
Unit.Kilograms => this.InputWeight
};
}
}
// TODO: define the 'TareAdjustment' property
public decimal TareAdjustment { get; set; }
}
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