-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.cs
201 lines (169 loc) · 5.32 KB
/
Ship.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace OOP_Project
{
[Serializable][DataContract (Name = "Navio")]
public class Ship //---- Classe para objetos tipo Navio
{
[DataMember (Name = "Nome")]
private string _name;
[DataMember (Name = "Número")]
private int _number;
[DataMember (Name = "MaxContentores")]
private int _maxContainers;
[DataMember (Name = "MaxExplosivos")]
private int _maxExplosive;
[DataMember (Name = "MaxQuimicos")]
private int _maxChemical;
[DataMember (Name = "Bandeira")]
private string _flag;
[DataMember (Name = "NoPorto")]
private bool _isAtPort;
[DataMember (Name = "Contentores")]
private List<Container> _containers;
public Ship(){}
public Ship(string name, int maxContainers, int maxExplosive, int maxChemical, string flag)
{
_name = name;
_number = ShipNumber();
_maxContainers = maxContainers;
_maxExplosive = maxExplosive;
_maxChemical = maxChemical;
_flag = flag;
_isAtPort = true;
_containers = new List<Container>();
}
public string GetName()
{
return _name;
}
public void SetName(string name)
{
_name = name;
}
public int GetNumber()
{
return _number;
}
public void SetNumber(int number)
{
_number = number;
}
public int GetMaxContainers()
{
return _maxContainers;
}
public void SetMaxContainers(int maxContainers)
{
_maxContainers = maxContainers;
}
public int GetMaxExplosive()
{
return _maxExplosive;
}
public void SetMaxExplosive(int maxExplosive)
{
_maxExplosive = maxExplosive;
}
public int GetMaxChemical()
{
return _maxChemical;
}
public void SetMaxChemical(int maxChemical)
{
_maxChemical = maxChemical;
}
public string GetFlag()
{
return _flag;
}
public void SetFlag(string flag)
{
_flag = flag;
}
public bool GetIsAtPort()
{
return _isAtPort;
}
public void SetIsAtPort(bool isAtPort)
{
_isAtPort = isAtPort;
}
public List<Container> GetContainers()
{
return _containers;
}
public void SetContainers(List<Container> containers)
{
_containers = containers;
}
public void AddContainer(Container c)
{
if (_containers.Count == _maxContainers)
{
throw new MaxContainersException(
"\t\tEste navio já tem o máximo de contentores! Contentor não adicionado!");
}
switch (c)
{
case Explosive _ when _containers.OfType<Explosive>().Count() == _maxExplosive:
throw new MaxExplosiveException(
"\t\tEste navio já tem o máximo de contentores do tipo Explosivo! Contentor não adicionado!");
case Chemical _ when _containers.OfType<Chemical>().Count() == _maxChemical:
throw new MaxChemicalException(
"\t\tEste navio já tem o máximo de contentores do tipo Químico!" +
" Contentor não adicionado!"
);
default:
_containers.Add(c);
break;
}
}
public int ShipNumber()
{
var random = new Random();
var rand = random.Next(1000000, 9999999);
return rand;
}
public int ListContainers()
{
if (_containers.Count == 0)
{
Console.WriteLine("\n\t\tEste navio não tem contentores atribuídos!");
Console.Write("\n\t\tPrima uma tecla para continuar...");
Console.ReadLine();
}
else
{
_containers.ForEach(Console.WriteLine);
}
return _containers.Count;
}
public override string ToString()
{
string s = "\t\tNome: " + _name + "\tNúmero: " + _number + "\tBandeira: " + _flag;
s += "\n\t\tMax. contentores: " + _maxContainers + "\t Max Explosivo: " + _maxExplosive + "\t Max Químicos: " + _maxChemical;
if (_isAtPort == true)
{
s += "\n\t\tAtracado no porto: Sim";
}
else
{
s += "\n\t\tAtracado no porto: Não";
}
s += "\n\t\t---------------------------------------------------------------------------------------------";
return s;
}
public override int GetHashCode()
{
return _number.GetHashCode();
}
public override bool Equals(object obj)
{
if (!(obj is Ship s)) return false;
return _number == s._number;
}
}
}