forked from 1kevgriff/ProjectDover
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInventory.cs
83 lines (68 loc) · 2.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectDover
{
public class Inventory
{
public string Name { get; set; }
public List<Item> Items { get; set; }
public Inventory(){
Name = "RoomItems";
Items = new List<Item>();
}
public Inventory(string name){
Name = name;
Items = new List<Item>();
}
public Item RemoveItem(string itemName)
{
Item item = Items.First(i => i.Name.Equals(itemName, StringComparison.OrdinalIgnoreCase));
return RemoveItem(item);
}
public Item RemoveItem(Item item)
{
Items.Remove(item);
Console.WriteLine($"The {item.Name} was removed from {Name}.");
return item;
}
public void AddItem(Item item)
{
Items.Add(item);
Console.WriteLine($"The {item.Name} was added to {Name}.");
}
public void ListItems()
{
StringBuilder itemlist = new StringBuilder();
foreach (Item i in Items) {
itemlist.Append(i.Name + ", ");
}
if (itemlist.Length > 0)
{
itemlist.Remove(itemlist.Length - 2, 2);
Console.WriteLine($"{Name} contains {itemlist.ToString()}.");
}
else {
Console.WriteLine($"{Name} is empty.");
}
}
public void LookAt(string itemName)
{
var result = (from item in Items
where item.Name.ToLower() == itemName.ToLower()
select item).FirstOrDefault<Item>();
if (result != null)
{
Console.WriteLine($"{result.Description}.");
}
else
{
Console.WriteLine($"The {itemName} was not found into {Name}.");
}
}
public bool Contains(string itemName){
return Items.Any(i => i.Name.Equals(itemName, StringComparison.OrdinalIgnoreCase));
}
}
}