-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrototype.cs
More file actions
55 lines (46 loc) · 1.59 KB
/
Copy pathPrototype.cs
File metadata and controls
55 lines (46 loc) · 1.59 KB
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
namespace Lab2.Patterns.Prototype;
public sealed class Virus : ICloneable
{
public Virus(string name, string species, double weight, int age, IReadOnlyList<Virus>? children = null)
{
Name = name;
Species = species;
Weight = weight;
Age = age;
Children = children?.ToList() ?? [];
}
public string Name { get; }
public string Species { get; }
public double Weight { get; }
public int Age { get; }
public List<Virus> Children { get; }
public object Clone()
{
var clonedChildren = Children.Select(child => (Virus)child.Clone()).ToList();
return new Virus(Name, Species, Weight, Age, clonedChildren);
}
public string ToTreeString(string indent = "")
{
var lines = new List<string>
{
$"{indent}- {Name} | вид: {Species}, вага: {Weight:F1}, вік: {Age}"
};
foreach (var child in Children)
{
lines.Add(child.ToTreeString($"{indent} "));
}
return string.Join(Environment.NewLine, lines);
}
}
public static class VirusSamples
{
public static Virus CreateFamily()
{
var grandChildOne = new Virus("Neo.A1", "Mutant", 0.3, 1);
var grandChildTwo = new Virus("Neo.A2", "Mutant", 0.4, 1);
var grandChildThree = new Virus("Neo.B1", "Hybrid", 0.5, 1);
var childOne = new Virus("Alpha", "Mutant", 0.8, 2, [grandChildOne, grandChildTwo]);
var childTwo = new Virus("Beta", "Hybrid", 0.9, 2, [grandChildThree]);
return new Virus("Omega", "Prime", 1.4, 4, [childOne, childTwo]);
}
}