Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- private static void Main()
- {
- Bag bag = new Bag(100);
- }
- }
- class Bag
- {
- private List<Cell> _cells;
- public int MaxWeidth { get; private set; }
- public int Weidth => _cells.Sum(cell => cell.Count);
- public IReadOnlyCollection<Cell> Cells => _cells;
- public Bag(int maxWeidth, IEnumerable<Item> items)
- {
- foreach (var item in items)
- _cells.Add(new Cell(item));
- MaxWeidth = maxWeidth;
- }
- public void AddItem(Item item, int count)
- {
- Cell cell = _cells.FirstOrDefault(x => x.Item == item);
- if (cell.Item == null)
- throw new InvalidOperationException();
- if (Weidth + count > MaxWeidth)
- throw new InvalidOperationException();
- cell.Count += count;
- }
- public class Cell
- {
- public readonly Item Item;
- public int Count;
- public Cell(Item item)
- {
- Item = item;
- Count = 0;
- }
- }
- }
- class Item
- {
- public string Name;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement