using System;
using System.Collections;
using System.Collections.Generic;
///
/// This class defines a basic inventory
///
public class Inventory : IModelObservable- // Borrowing the model observable here. Probably nothing will sub for items removed and such, but transactions are important! Lazy though, should split the observable
{
public int Money { get; private set; } //Getter for the money, the views need it to display the amount of money.
private List
- itemList = new List
- (); //Items in the inventory
private List> observers; // Mostly here for things that want to be up to date on transactions
//Set up the inventory with item count and money
public Inventory(int pItemCount, int pMoney) : this(pMoney)
{
PopulateInventory(pItemCount);
}
public Inventory(int pMoney)
{
Money = pMoney;
observers = new List>();
}
//------------------------------------------------------------------------------------------------------------------------
// GetItems()
//------------------------------------------------------------------------------------------------------------------------
//Returns a list with all current items in the shop.
public List
- GetItems()
{
return new List
- (itemList); //Returns a copy of the list, so the original is kept intact,
//however this is shallow copy of the original list, so changes in
//the original list will likely influence the copy, apply
//creational patterns like prototype to fix this.
}
//------------------------------------------------------------------------------------------------------------------------
// GetItemCount()
//------------------------------------------------------------------------------------------------------------------------
//Returns the number of items
public int GetItemCount()
{
return itemList.Count;
}
//------------------------------------------------------------------------------------------------------------------------
// GetItemByIndex()
//------------------------------------------------------------------------------------------------------------------------
//Attempts to get an item, specified by index, returns null if unsuccessful. Depends on how you set up your shop, it might be
//a good idea to return a copy of the original item.
public Item GetItemByIndex(int index)
{
if (index >= 0 && index < itemList.Count)
{
return itemList[index];
}
else
{
return null;
}
}
//------------------------------------------------------------------------------------------------------------------------
// AddItem()
//------------------------------------------------------------------------------------------------------------------------
//Adds an item to the inventory's item list.
public void AddItem(Item item)
{
itemList.Add(item);//In your setup, what would happen if you add an item that's already existed in the list?
}
//------------------------------------------------------------------------------------------------------------------------
// RemoveItem()
//------------------------------------------------------------------------------------------------------------------------
//Attempts to remove an item, fails silently.
public void Remove(Item item)
{
if (itemList.Contains(item))
{
itemList.Remove(item);
}
}
//------------------------------------------------------------------------------------------------------------------------
// RemoveItemByIndex()
//------------------------------------------------------------------------------------------------------------------------
//Attempts to remove an item, specified by index, fails silently.
public void RemoveItemByIndex(int index)
{
if (index >= 0 && index < itemList.Count)
{
itemList.RemoveAt(index);
}
}
//------------------------------------------------------------------------------------------------------------------------
// PopulateInventory()
//------------------------------------------------------------------------------------------------------------------------
//This is obviously not how you should generate items. For this assignment you need either an abstract item factory, or
//make this into a factory method.
private void PopulateInventory(int itemCount)
{
Random random = new Random();
for (int index = 0; index < itemCount; index++)
{
Item item = new ItemWeapon("Student Sword", "items_" + random.Next(73, 145), 50,0,0); //item name, item icon, cost
itemList.Add(item);
}
}
public void ChangeBalance(int change)
{
if (Money + change < 0)
throw new NotEnoughMoneyException(); // We can't change the balance if it's too expensive
Money += change;
NotifyObservers();
}
//Think of other necessary functions for the inventory based on your design of the shop. Don't forget to unit test all the functions.
public IDisposable RegisterObserver(IShopModelObserver
- observer)
{
// Check whether observer is already registered. If not, add it
if (! observers.Contains(observer)) {
observers.Add(observer);
// Provide observer with existing data.
observer.OnTransaction(Money);
}
return new Unsubscriber
- (observers, observer);
}
public void RemoveObserver(IShopModelObserver
- observer)
{
if (observers.Contains(observer))
observers.Remove(observer);
}
private void NotifyObservers()
{
foreach (var observer in observers)
{
observer.OnTransaction(Money);
}
}
}