AlexRaynor

Untitled

Oct 16th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.56 KB | None | 0 0
  1. class Bag
  2. {
  3.     public List<Item> Items;
  4.     public int MaxWeidth;
  5.  
  6.     public void AddItem(string name, int count)
  7.     {
  8.         int currentWeidth = Items.Sum(item => item.Count);
  9.         Item targetItem = Items.FirstOrDefault(item => item.Name == name);
  10.  
  11.         if (targetItem == null)
  12.             throw new InvalidOperationException();
  13.  
  14.         if (currentWeidth + count > MaxWeidth)
  15.             throw new InvalidOperationException();
  16.  
  17.         targetItem.Count += count;
  18.     }
  19. }
  20.  
  21. class Item
  22. {
  23.     public int Count;
  24.     public string Name;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment