Skip to content

Commit d291cc5

Browse files
committed
Second Task
1 parent e7a89c8 commit d291cc5

File tree

6 files changed

+41
-26
lines changed

6 files changed

+41
-26
lines changed

ConsoleApp2/ICollectionItem.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace ConsoleApp2;
2+
3+
public interface ICollectionItem
4+
{
5+
int _id
6+
{
7+
get;
8+
set;
9+
}
10+
void validateWholeClass();
11+
}

ConsoleApp2/Menu.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace ConsoleApp2;
88

99
public class Menu
1010
{
11-
private REGISTRATION_CERTIFICATE_COLLECTION _collection;
11+
private REGISTRATION_CERTIFICATE_COLLECTION<REGISTRATION_CERTIFICATE> _collection;
1212
public Menu()
1313
{
14-
_collection = new REGISTRATION_CERTIFICATE_COLLECTION();
14+
_collection = new REGISTRATION_CERTIFICATE_COLLECTION<REGISTRATION_CERTIFICATE>();
1515

1616
_collection.deserealizeFromFile();
1717
var dictionary = new Dictionary<int, Delegate>()
@@ -88,8 +88,17 @@ private void Find()
8888
private void Add()
8989
{
9090
var certificate = REGISTRATION_CERTIFICATE.readFromConsole();
91-
_collection.Add(certificate);
92-
Console.Write(_collection);
91+
try
92+
{
93+
_collection.Add(certificate);
94+
Console.Write(_collection);
95+
}
96+
catch (Exception e)
97+
{
98+
Console.WriteLine(e.Message);
99+
}
100+
101+
93102
}
94103

95104
private void Remove()

ConsoleApp2/Program.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ public class Registration
77
// This is the main entry point for the application.
88
public static void Main(string[] args)
99
{
10-
var d = new REGISTRATION_CERTIFICATE("1", "ВС9909АВ","5/1/2008","4Y1SL65848Z411439","test","2007");
11-
var dq = new REGISTRATION_CERTIFICATE("2", "ВС9908АВ","5/1/2008","4Y1SL65848Z411439","TEst","2008");
12-
var dw = new REGISTRATION_CERTIFICATE("3", "ВС9907АВ","5/1/2008","4Y1SL65848Z411439","Audi","2002");
13-
var a = new REGISTRATION_CERTIFICATE_COLLECTION();
1410
var menu = new Menu();
1511
}
1612
}

ConsoleApp2/REGISTRATION_CERTIFICATE.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ConsoleApp2;
44

5-
public class REGISTRATION_CERTIFICATE
5+
public class REGISTRATION_CERTIFICATE: ICollectionItem
66
{
77
public int _id{ get; set;}
88
public string _registration_number{ get; set; }
@@ -40,12 +40,12 @@ public REGISTRATION_CERTIFICATE(string id, string registration_number,string dat
4040
Set_year_of_manufacture(yearOfCar);
4141
}
4242

43+
4344
public void Set_id(string idStr)
4445
{
4546
var id = Convert.ToInt32(idStr);
46-
_id = Validation.ValidateId(id);
47+
_id = Validation.ValidateId(id);
4748
}
48-
4949
public void Set_registration_number(string number)
5050
{
5151

ConsoleApp2/REGISTRATION_CERTIFICATE_COLLECTION.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace ConsoleApp2;
66

7-
public class REGISTRATION_CERTIFICATE_COLLECTION
7+
public class REGISTRATION_CERTIFICATE_COLLECTION<T> where T : ICollectionItem
88
{
9-
private List<REGISTRATION_CERTIFICATE> _list;
9+
private List<T> _list;
1010

1111
public void serilizeToFile()
1212
{
@@ -17,9 +17,9 @@ public void serilizeToFile()
1717

1818
public void deserealizeFromFile()
1919
{
20-
List<REGISTRATION_CERTIFICATE> result = new List<REGISTRATION_CERTIFICATE>();
20+
List<T> result = new List<T>();
2121
string json = new StreamReader("../../../data.json").ReadToEnd();
22-
_list = JsonSerializer.Deserialize<List<REGISTRATION_CERTIFICATE>>(json);
22+
_list = JsonSerializer.Deserialize<List<T>>(json);
2323
var indexToDelete = new List<int>();
2424
for (int i =0; i<_list.Count-1;++i)
2525
{
@@ -40,26 +40,26 @@ public void deserealizeFromFile()
4040
}
4141
}
4242

43-
private REGISTRATION_CERTIFICATE certificateByID(int id)
43+
private T ItemById(int id)
4444
{
4545
foreach (var certificate in _list)
4646
{
4747
if (certificate._id == id)
4848
return certificate;
4949
}
5050

51-
return null;
51+
return default(T);
5252
}
5353

5454
public REGISTRATION_CERTIFICATE_COLLECTION()
5555
{
56-
_list = new List<REGISTRATION_CERTIFICATE>();
56+
_list = new List<T>();
5757
}
5858

5959

60-
public List<REGISTRATION_CERTIFICATE> searchByValue(string value)
60+
public List<T> searchByValue(string value)
6161
{
62-
var result = new List<REGISTRATION_CERTIFICATE>();
62+
var result = new List<T>();
6363
foreach (var item in _list)
6464
{
6565
foreach (var field in item.GetType().GetProperties())
@@ -109,26 +109,25 @@ public bool checkIfIdIsUnique(int id)
109109

110110
return true;
111111
}
112-
public void Add(REGISTRATION_CERTIFICATE certificate)
112+
public void Add(T certificate)
113113
{
114114
if (!checkIfIdIsUnique(certificate._id))
115115
{
116-
Console.WriteLine("You have registration certificate with the same ID");
117-
return;
116+
throw new Exception("You have item with the same id");
118117
}
119118
_list.Add(certificate);
120119
serilizeToFile();
121120
}
122121

123122
public void Remove(int index)
124123
{
125-
_list.Remove(certificateByID(index));
124+
_list.Remove(ItemById(index));
126125
serilizeToFile();
127126
}
128127

129128
public void Edit(int index)
130129
{
131-
REGISTRATION_CERTIFICATE foundCertificate = null;
130+
T foundCertificate = default(T);
132131
foreach (var _certificate in _list)
133132
{
134133
if (_certificate._id == index)

ConsoleApp2/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"_id":1,"_registration_number":"\u0412\u042199609\u0410\u0412","_date_of_registration":"2008-05-01T00:00:00","_VIN_code":"4Y1SL65848Z411439","_car":"test","_year_of_manufacture":2007},{"_id":2,"_registration_number":"\u0412\u04219908\u0410\u0412","_date_of_registration":"2008-05-01T00:00:00","_VIN_code":"4Y1SL65848Z411439","_car":"TEst","_year_of_manufacture":2008},{"_id":3,"_registration_number":"\u0412\u04219907\u0410\u0412","_date_of_registration":"2008-05-01T00:00:00","_VIN_code":"4Y1SL65848Z411439","_car":"Audi","_year_of_manufacture":2002}]
1+
[{"_id":2,"_registration_number":"\u0412\u04219908\u0410\u0412","_date_of_registration":"2008-05-01T00:00:00","_VIN_code":"4Y1SL65848Z411439","_car":"TEst","_year_of_manufacture":2008},{"_id":3,"_registration_number":"\u0412\u04219907\u0410\u0412","_date_of_registration":"2008-05-01T00:00:00","_VIN_code":"4Y1SL65848Z411439","_car":"Audi","_year_of_manufacture":2002},{"_id":43,"_registration_number":"\u0412\u04219909\u0410\u041F","_date_of_registration":"2008-05-01T00:00:00","_VIN_code":"4Y1SL65848Z411439","_car":"\u0430\u0432","_year_of_manufacture":2007}]

0 commit comments

Comments
 (0)