-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.cs
147 lines (134 loc) · 3.14 KB
/
Inventory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Xml;
namespace Game
{
public class Inventory :IXml
{
/// <summary>
/// Number of items, which can be stored in an inventory.
/// </summary>
public readonly int maxsize;
/// <summary>
/// The inventory itself - list of items.
/// </summary>
public List<Item> bag;
/// <summary>
/// Initializes a new instance of the <see cref="Game.Inventory"/> class.
/// </summary>
/// <param name='maxsize'>
/// Maxsize.
/// </param>
public Inventory (int maxsize)
{
this.maxsize = maxsize;
this.bag = new List<Item>();
}
/// <summary>
/// Add Item to inventory, if there is space.
/// </summary>
/// <param name='i'>
/// I.
/// </param>
public string Add(Item i)
{
string msg = "Adding item to bag";
if(this.bag.Count <= this.maxsize)
this.bag.Add(i);
else
msg += "\nInventory is full";
return msg;
}
/// <summary>
/// Remove the specified Item from inventory, unless it is missing - in that case give warning message.
/// </summary>
/// <param name='i'>
/// I.
/// </param>
public string Remove(Item i)
{
string msg = ("Attempt to remove " + i.Name);
bool removed = false;
foreach(Item item in this.bag)
if (item.Name == i.Name)
{
this.bag.Remove(item);
removed = true;
msg += "\nRemoved";
break;
}
if (!removed)
msg += ("\nItem is not in the Inventory and will not be removed");
return msg;
}
public void RemoveAll()
{
this.bag = new List<Item>();
}
/// <summary>
/// Returns one item with given name from the Inventory.
/// </summary>
/// <returns>
/// The item.
/// </returns>
/// <param name='name'>
/// Name.
/// </param>
/// <exception cref='ArgumentException'>
/// Is thrown when an argument passed to a method is invalid.
/// </exception>
public Item GetItem(string s)
{
foreach (Item i in this.bag)
if (i.Name == s)
return i;
throw new ArgumentException("This item is not in the inventory.");
}
/// <summary>
/// Is Item with given name inside inventory?.
/// </summary>
/// <param name='name'>
/// If set to <c>true</c> name.
/// </param>
//
public bool Inside(string name)
{
foreach (Item item in this.bag)
if(item.Name == name)
return true;
return false;
}
public int Count()
{
return this.bag.Count;
}
/// <summary>
/// Write names of all items in inventory.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents the current <see cref="Game.Inventory"/>.
/// </returns>
public override string ToString ()
{
string s = "";
int position = 1;
foreach (Item i in this.bag)
{
s += (position + " " + i.Name + "\n");
position++;
}
return s;
}
public XmlElement ToXml(XmlDocument doc, string elementName)
{
XmlElement inventory = doc.CreateElement(elementName);
inventory.SetAttribute("maxsize", this.maxsize.ToString());
foreach (IXml i in this.bag)
{
XmlElement xmli = i.ToXml(doc, i.GetType().ToString().Split('.')[1]);
inventory.AppendChild(xmli);
}
return inventory;
}
}
}