-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathShallowAndDeepCopy.cs
More file actions
146 lines (122 loc) · 5.05 KB
/
ShallowAndDeepCopy.cs
File metadata and controls
146 lines (122 loc) · 5.05 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
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.Linq;
using System.Text;
namespace ShallowDeepCopy
{
class Program
{
static void Main(string[] args)
{
// Random Social Security Number Generator, which returns a tuple (int threeDigits, int twoDigits,int fourDigits)
// xxx-xx-xxxx format
SSNGenerator ssnGenerator = new(new Random());
Staff hrStaff = new(new QuickInfo("Jessy", "Herolt"),
new DetailedInfo(Guid.NewGuid(),
"0001-89938749928-388293",
"Budgie Str. Cathood Avn. 33242 / 233",
new DateTime(1990, 03, 1), ssnGenerator.Ssn),
"dummyData");
Staff shallowCopy = hrStaff.ShallowCopy();
Staff deepCopy = hrStaff.DeepCopy();
Print(hrStaff,"Original");
// alter the values in hrStaff , DummyData is the only primitive type in Staff class
hrStaff.QuickInformation.FirstName = "Dalton";
hrStaff.QuickInformation.LastName = "Mayer";
hrStaff.DummyData = "DummyData changed";
hrStaff.DetailedInformation.Id = Guid.NewGuid();
hrStaff.DetailedInformation.IBAN = "0001-19958649928-088293";
hrStaff.DetailedInformation.Address = "Nowhere";
hrStaff.DetailedInformation.BirthDate = new DateTime(1956, 1, 2);
hrStaff.DetailedInformation.SSN = ssnGenerator.Ssn;
Print(shallowCopy, "Shallow Copy");
Print(deepCopy, "Deep Copy");
static void Print(Staff hrStaff,string copyType)
{
Console.WriteLine($"{copyType}\n");
Console.WriteLine($"FirstName:{hrStaff.QuickInformation.FirstName}\nLastName:{hrStaff.QuickInformation.LastName}\nDummyData:{hrStaff.DummyData}\n" +
$"Detailed Information:\n" +
$" Id:{hrStaff.DetailedInformation.Id}\n IBAN:{hrStaff.DetailedInformation.IBAN}\n" +
$" Address:{hrStaff.DetailedInformation.Address}\n BirthDate:{hrStaff.DetailedInformation.BirthDate}\n" +
$" SSN:{hrStaff.DetailedInformation.SSN.treeDigits}-" +
$"{hrStaff.DetailedInformation.SSN.twoDigits}-" +
$"{hrStaff.DetailedInformation.SSN.fourDigits}\n");
}
}
}
public class SSNGenerator
{
public (int threeDigits, int twoDigits, int fourDigits) Ssn => GenerateSSN();
private readonly Random _random;
// struct doesn't accept a parameterless constructor
// this is why I'm passing a random parameter
public SSNGenerator(Random random)
{
_random = random;
}
private (int threeDigits, int twoDigits, int fourDigits) GenerateSSN()
{
var threeDigits = _random.Next(100, 1000);
var twoDigits = _random.Next(10, 100);
var fourDigits = _random.Next(1000, 10_000);
return (threeDigits, twoDigits, fourDigits);
}
}
public class QuickInfo
{
public QuickInfo(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public QuickInfo ShallowCopy()
{
return this.MemberwiseClone() as QuickInfo;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class DetailedInfo
{
public DetailedInfo(Guid id, string iban, string address, DateTime birthDate, (int treeDigits, int twoDigits, int fourDigits) ssn)
{
Id = id;
IBAN = iban;
Address = address;
BirthDate = birthDate;
SSN = ssn;
}
public DetailedInfo ShallowCopy()
{
return this.MemberwiseClone() as DetailedInfo;
}
public Guid Id { get; set; }
public string IBAN { get; set; }
public string Address { get; set; }
public DateTime BirthDate { get; set; }
public (int treeDigits, int twoDigits, int fourDigits) SSN { get; set; } // 123-42-6476 format
}
public class Staff
{
public Staff(QuickInfo quicInfornamtion, DetailedInfo detailedInformation,string dummyData)
{
QuickInformation = quicInfornamtion;
DetailedInformation = detailedInformation;
DummyData = dummyData;
}
public QuickInfo QuickInformation{ get; set; }
public DetailedInfo DetailedInformation { get; set; }
public string DummyData { get; set; }
public Staff ShallowCopy()
{
return MemberwiseClone() as Staff;
}
public Staff DeepCopy()
{
var deepCopy = this.MemberwiseClone() as Staff;
deepCopy.DetailedInformation = this.DetailedInformation.ShallowCopy();
deepCopy.QuickInformation = this.QuickInformation.ShallowCopy();
return deepCopy;
}
}
}