Skip to content

Instantly share code, notes, and snippets.

@HolyMonkey
Created August 28, 2019 20:09
Show Gist options
  • Save HolyMonkey/164a6b05eb869781b4562985237f9fa2 to your computer and use it in GitHub Desktop.
Save HolyMonkey/164a6b05eb869781b4562985237f9fa2 to your computer and use it in GitHub Desktop.
class Bag
{
public List<Item> Items;
public int MaxWeidth;
public void AddItem(string name, int count)
{
int currentWeidth = Items.Sum(item => item.Count);
Item targetItem = Items.FirstOrDefault(item => item.Name == name);
if (targetItem == null)
throw new InvalidOperationException();
if (currentWeidth + count > MaxWeidth)
throw new InvalidOperationException();
targetItem.Count += count;
}
}
class Item
{
public int Count;
public string Name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment