-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPack.cs
141 lines (125 loc) · 3.33 KB
/
Pack.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using PackingInventory;
namespace PackingInventory
{
internal class Pack
{
public InventoryItem[] Items { get; private set; }
public float MaxWeight { get; private set; }
public float MaxVolume { get; private set; }
public int MaxItems { get; private set; }
private float _currentWeight;
private float _currentVolume;
private int _currentItems;
//Examine array and get total weight
public float CurrentWeight
{
get
{
_currentWeight = 0f;
foreach (var item in Items)
{
if (item is null) continue;
_currentWeight += item.Weight;
}
return _currentWeight;
}
private set { }
}
//Examine array and get total volume
public float CurrentVolume
{
get
{
_currentVolume = 0f;
foreach (var item in Items)
{
if (item is null) continue;
_currentVolume += item.Volume;
}
return _currentVolume;
}
private set { }
}
//Examine array and get total items
public float ItemCount
{
get
{
_currentItems = 0;
foreach (var item in Items)
{
if (item is null) continue;
++_currentItems;
}
return _currentItems;
}
private set { }
}
public Pack(int maxItems, float maxWeight, float maxVolume)
{
Items = new InventoryItem[maxItems];
MaxItems = maxItems;
MaxWeight = maxWeight;
MaxVolume = maxVolume;
}
//Set Pack Sizes
public static Pack CreateSmallPack() => new(5, 3f, 3f);
public static Pack CreateMediumPack() => new(7, 9f, 10f);
public static Pack CreateLargePack() => new(10, 15f, 20f);
//Add a new items if there is space.
public bool Add(InventoryItem item)
{
for (int i = 0; i <= Items.Length-1; i++)
{
if (Items[i] is null && !IsFull())
{
if (CurrentVolume + item.Volume > MaxVolume)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Unable to add {item} - Insufficient Volume");
Console.ResetColor();
return false;
}
if (CurrentWeight + item.Weight > MaxWeight)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Unable to add {item} - Too Heavy");
Console.ResetColor();
return false;
}
Items[i] = item;
Console.ForegroundColor= ConsoleColor.DarkGreen;
Console.WriteLine($"{item} Added to bag");
Console.ResetColor();
return true;
}
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Unable to add {item} - Bag is Full");
Console.ResetColor();
return false;
}
public override string ToString()
{
string[] itemNames = new string[Items.Length];
for (int i = 0; i <= Items.Length - 1; i++)
{
if (Items[i] is null) continue;
itemNames[i] = Items[i].ToString()!;
}
return $"Pack currently contains: {(string.IsNullOrEmpty(itemNames[0]) ? "Nothing" : string.Join(", ", itemNames.OfType<string>()))}";
}
//Is the Pack full on any of our limits
public bool IsFull() => IsMaxItems() || IsMaxWeight() || IsMaxVolume();
public bool IsMaxItems() => ItemCount >= MaxItems;
public bool IsMaxWeight() => CurrentWeight >= MaxWeight;
public bool IsMaxVolume() => CurrentVolume >= MaxVolume;
}
}