public static void Main(string[] args) { Toy car = new Toy("Car", 3, 2014, "BiH"); Toy plane = new Toy("Plane", 2, 2013); Toy ball = new Toy("Ball", 0, 2013); ToyStore store = new ToyStore("Camp"); store.BuyToy(car); store.BuyToy(car); store.BuyToy(plane); store.BuyToy(plane); store.BuyToy(plane); store.BuyToy(ball); store.BuyToy(ball); store.BuyToy(ball); store.BuyToy(ball); store.PrintInventory(); store.SellToy(car); store.PrintInventory(); store.SellToy(car); store.PrintInventory(); }
private int IndexOf(Toy toy) { for (int i = 0; i < size; i++) { if (array[i].Equals(toy)) { return i; } } return -1; }
public void RemoveToy(Toy toy) { int index = IndexOf(toy); if (index == -1) throw new ArgumentException("Can't remove a toy we don't have"); if (array[index].Amount == 1) { RemoveElement(index); } else { array[index].DecereaseAmount(); } }
public void AddToy(Toy toy) { if (array.Length >= size) { Resize(); } int index = IndexOf(toy); if (index >= 0) { array[index].IncreaseAmount(); } else { array[size++] = toy; } }
public bool Contains(Toy toy) { return IndexOf(toy) >= 0; }
public void SellToy(Toy toy) { if (inventory.Contains(toy)) inventory.RemoveToy(toy); }
public void BuyToy(Toy toy) { inventory.AddToy(toy); }
private void Resize() { Toy[] temp = new Toy[array.Length * 2]; for (int i = 0; i < array.Length; i++) { temp[i] = array[i]; } array = temp; }