@@ -96,8 +96,9 @@ class CoffeeMachine {
96
96
_waterAmount = 0 ;
97
97
98
98
set waterAmount (value ) {
99
- // "throw new Error" generates an error, we'll cover it later in the tutorial
100
- if (value < 0 ) throw new Error (" Negative water" );
99
+ if (value < 0 ) {
100
+ value = 0 ;
101
+ }
101
102
this ._waterAmount = value;
102
103
}
103
104
@@ -118,7 +119,7 @@ let coffeeMachine = new CoffeeMachine(100);
118
119
coffeeMachine .waterAmount = - 10 ; // Error: Negative water
119
120
```
120
121
121
- Now the access is under control, so setting the water below zero fails .
122
+ Now the access is under control, so setting the water amount below zero becomes impossible .
122
123
123
124
## Read-only "power"
124
125
@@ -160,7 +161,7 @@ class CoffeeMachine {
160
161
_waterAmount = 0;
161
162
162
163
*!*setWaterAmount(value)*/!* {
163
- if (value < 0) throw new Error("Negative water") ;
164
+ if (value < 0) value = 0 ;
164
165
this._waterAmount = value;
165
166
}
166
167
@@ -200,19 +201,23 @@ class CoffeeMachine {
200
201
*/ ! *
201
202
202
203
* ! *
203
- #checkWater (value ) {
204
- if (value < 0 ) throw new Error ( " Negative water " ) ;
205
- if (value > this .#waterLimit) throw new Error ( " Too much water " ) ;
204
+ #fixWaterAmount (value ) {
205
+ if (value < 0 ) return 0 ;
206
+ if (value > this .#waterLimit) return this .#waterLimit ;
206
207
}
207
208
*/ ! *
208
209
210
+ setWaterAmount (value ) {
211
+ this .#waterLimit = this .#fixWaterAmount (value);
212
+ }
213
+
209
214
}
210
215
211
216
let coffeeMachine = new CoffeeMachine ();
212
217
213
218
* ! *
214
219
// can't access privates from outside of the class
215
- coffeeMachine.#checkWater ( ); // Error
220
+ coffeeMachine.#fixWaterAmount ( 123 ); // Error
216
221
coffeeMachine.#waterLimit = 1000 ; // Error
217
222
*/ ! *
218
223
```
@@ -233,7 +238,7 @@ class CoffeeMachine {
233
238
}
234
239
235
240
set waterAmount (value ) {
236
- if (value < 0 ) throw new Error ( " Negative water " ) ;
241
+ if (value < 0 ) value = 0 ;
237
242
this .#waterAmount = value;
238
243
}
239
244
}
0 commit comments