@@ -67,80 +67,80 @@ public void save(Customer<Long> customer) {
67
67
try (Reader reader = createReader (filePath )) {
68
68
customers = gson .fromJson (reader , customerListType );
69
69
} catch (IOException ex ) {
70
- throw new RuntimeException ("Failed to read customer data" , ex );
70
+ throw new CustomException ("Failed to read customer data" , ex );
71
71
}
72
72
}
73
73
customers .add (customer );
74
74
try (Writer writer = createWriter (filePath )) {
75
75
gson .toJson (customers , writer );
76
76
} catch (IOException ex ) {
77
- throw new RuntimeException ("Failed to write customer data" , ex );
77
+ throw new CustomException ("Failed to write customer data" , ex );
78
78
}
79
79
}
80
80
81
81
/** {@inheritDoc} */
82
82
@ Override
83
83
public void update (Customer <Long > customer ) {
84
84
if (!filePath .toFile ().exists ()) {
85
- throw new RuntimeException ("File not found" );
85
+ throw new CustomException ("File not found" );
86
86
}
87
87
List <Customer <Long >> customers ;
88
88
try (Reader reader = createReader (filePath )) {
89
89
customers = gson .fromJson (reader , customerListType );
90
90
} catch (IOException ex ) {
91
- throw new RuntimeException ("Failed to read customer data" , ex );
91
+ throw new CustomException ("Failed to read customer data" , ex );
92
92
}
93
93
customers .stream ()
94
94
.filter (c -> c .getId ().equals (customer .getId ()))
95
95
.findFirst ()
96
96
.ifPresentOrElse (
97
97
c -> c .setName (customer .getName ()),
98
98
() -> {
99
- throw new RuntimeException ("Customer not found with id: " + customer .getId ());
99
+ throw new CustomException ("Customer not found with id: " + customer .getId ());
100
100
});
101
101
try (Writer writer = createWriter (filePath )) {
102
102
gson .toJson (customers , writer );
103
103
} catch (IOException ex ) {
104
- throw new RuntimeException ("Failed to write customer data" , ex );
104
+ throw new CustomException ("Failed to write customer data" , ex );
105
105
}
106
106
}
107
107
108
108
/** {@inheritDoc} */
109
109
@ Override
110
110
public void delete (Long id ) {
111
111
if (!filePath .toFile ().exists ()) {
112
- throw new RuntimeException ("File not found" );
112
+ throw new CustomException ("File not found" );
113
113
}
114
114
List <Customer <Long >> customers ;
115
115
try (Reader reader = createReader (filePath )) {
116
116
customers = gson .fromJson (reader , customerListType );
117
117
} catch (IOException ex ) {
118
- throw new RuntimeException ("Failed to read customer data" , ex );
118
+ throw new CustomException ("Failed to read customer data" , ex );
119
119
}
120
120
Customer <Long > customerToRemove =
121
121
customers .stream ()
122
122
.filter (c -> c .getId ().equals (id ))
123
123
.findFirst ()
124
- .orElseThrow (() -> new RuntimeException ("Customer not found with id: " + id ));
124
+ .orElseThrow (() -> new CustomException ("Customer not found with id: " + id ));
125
125
customers .remove (customerToRemove );
126
126
try (Writer writer = createWriter (filePath )) {
127
127
gson .toJson (customers , writer );
128
128
} catch (IOException ex ) {
129
- throw new RuntimeException ("Failed to write customer data" , ex );
129
+ throw new CustomException ("Failed to write customer data" , ex );
130
130
}
131
131
}
132
132
133
133
/** {@inheritDoc} */
134
134
@ Override
135
135
public List <Customer <Long >> findAll () {
136
136
if (!filePath .toFile ().exists ()) {
137
- throw new RuntimeException ("File not found" );
137
+ throw new CustomException ("File not found" );
138
138
}
139
139
List <Customer <Long >> customers ;
140
140
try (Reader reader = createReader (filePath )) {
141
141
customers = gson .fromJson (reader , customerListType );
142
142
} catch (IOException ex ) {
143
- throw new RuntimeException ("Failed to read customer data" , ex );
143
+ throw new CustomException ("Failed to read customer data" , ex );
144
144
}
145
145
return customers ;
146
146
}
@@ -149,13 +149,13 @@ public List<Customer<Long>> findAll() {
149
149
@ Override
150
150
public Optional <Customer <Long >> findById (Long id ) {
151
151
if (!filePath .toFile ().exists ()) {
152
- throw new RuntimeException ("File not found" );
152
+ throw new CustomException ("File not found" );
153
153
}
154
154
List <Customer <Long >> customers = null ;
155
155
try (Reader reader = createReader (filePath )) {
156
156
customers = gson .fromJson (reader , customerListType );
157
157
} catch (IOException ex ) {
158
- throw new RuntimeException ("Failed to read customer data" , ex );
158
+ throw new CustomException ("Failed to read customer data" , ex );
159
159
}
160
160
return customers .stream ().filter (c -> c .getId ().equals (id )).findFirst ();
161
161
}
@@ -164,12 +164,12 @@ public Optional<Customer<Long>> findById(Long id) {
164
164
@ Override
165
165
public void deleteSchema () {
166
166
if (!filePath .toFile ().exists ()) {
167
- throw new RuntimeException ("File not found" );
167
+ throw new CustomException ("File not found" );
168
168
}
169
169
try {
170
170
Files .delete (filePath );
171
171
} catch (IOException ex ) {
172
- throw new RuntimeException ("Failed to delete customer data" );
172
+ throw new CustomException ("Failed to delete customer data" );
173
173
}
174
174
}
175
175
}
0 commit comments