Last active
May 22, 2022 20:58
-
-
Save es-repo/fe17787db63f51cf426a1b0495cf03f9 to your computer and use it in GitHub Desktop.
better-unit-test-in-c#-article-Box-v1.cs
This file contains hidden or 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
public sealed record Box | |
{ | |
private readonly Dictionary<string, Thing> thingsInside; | |
public int Size { get; init; } | |
public bool IsOpen { get; private set; } | |
public Box() | |
{ | |
thingsInside = new(); | |
} | |
public void Open() | |
{ | |
IsOpen = true; | |
} | |
public void Close() | |
{ | |
IsOpen = false; | |
} | |
public int GetAvailableSpace() | |
{ | |
return Size - thingsInside.Values.Sum(thing => thing.Size); | |
} | |
public bool CanPutInside(Thing thing, string label) | |
{ | |
return IsOpen && GetAvailableSpace() - thing.Size >= 0 && !thingsInside.ContainsKey(label); | |
} | |
public bool PutInside( | |
Thing thing, | |
string label) | |
{ | |
if (!CanPutInside(thing, label)) | |
{ | |
return false; | |
} | |
thingsInside.Add(label, thing); | |
return true; | |
} | |
} | |
public sealed record Thing | |
{ | |
public int Size { get; init; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment