Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     private static void Main()
  8.     {
  9.         Bag bag = new Bag(100);
  10.     }
  11. }
  12.  
  13. class Bag
  14. {
  15.     private List<Cell> _cells;
  16.    
  17.     public int MaxWeidth { get; private set; }
  18.     public int Weidth => _cells.Sum(cell => cell.Count);
  19.     public IReadOnlyCollection<Cell> Cells => _cells;
  20.  
  21.  
  22.     public Bag(int maxWeidth, IEnumerable<Item> items)
  23.     {
  24.  
  25.         foreach (var item in items)
  26.             _cells.Add(new Cell(item));
  27.  
  28.         MaxWeidth = maxWeidth;
  29.     }
  30.  
  31.     public void AddItem(Item item, int count)
  32.     {
  33.         Cell cell = _cells.FirstOrDefault(x => x.Item == item);
  34.  
  35.         if (cell.Item == null)
  36.             throw new InvalidOperationException();
  37.  
  38.         if (Weidth + count > MaxWeidth)
  39.             throw new InvalidOperationException();
  40.  
  41.         cell.Count += count;
  42.     }
  43.  
  44.     public class Cell
  45.     {
  46.         public readonly Item Item;
  47.         public int Count;
  48.  
  49.         public Cell(Item item)
  50.         {
  51.             Item = item;
  52.             Count = 0;
  53.         }
  54.     }
  55. }
  56.  
  57. class Item
  58. {
  59.     public string Name;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement