44
55namespace 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 )
0 commit comments