Newer
Older
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package hw1;
/**
* Model of a coin-operated ParkingMeter is constructed so that a specified
* number of minutes is associated with each of three coin types, nickels,
* dimes, or quarters. .
*
* @author Siyu Lin
*/
public class ParkingMeter{
// Instance Varibles
/**
* The remaining coin values, by default in cents
*/
private int coinRemainingValues;
/**
* The remaining coin numbers
*/
private int coinRemainingNumbers;
/**
* The remaining time by default in minutes
*/
private int timeRemaining;
/**
* The max number of coin that can be inserted
*/
private int coinMax;
/**
* The max time by default in minutes
*/
private int timeMax;
/**
* Added time for one nickel inserted, by default in minutes
*/
private int minutesForNickel;
/**
* Added time for one dime inserted, by default in minutes
*/
private int minutesForDime;
/**
* Added time for one quarter inserted, by default in minutes
*/
private int minutesForQuarter;
/**
* Value of nickel coin in cents.
*/
public static final int NICKEL_VALUE = 5;
/**
* Value of dime coin in cents.
*/
public static final int DIME_VALUE = 10;
/**
* Value of quarter coin in cents.
*/
public static final int QUARTER_VALUE = 25;
/**
* Constructors ParkingTime by default is intially expired and contains no
* coins
*
* @param givenMinutesForNickel
* Added time for one nickel inserted, by default in minutes,
* alert error if negative
* @param givenMinutesForDime
* Added time for one dime inserted, by default in minutes, alert
* error if negative
* @param givenMinutesForQuarter
* Added time for one quarter inserted, by default in minutes,
* alert error if negative
* @param givenMaxTime
* The max time by default in minutes, alert error if negative
* @param givenMaxCoins
* The max number of coin that can be inserted, alert error if
* negative
*/
public ParkingMeter(int givenMinutesForNickel, int givenMinutesForDime,
int givenMinutesForQuarter, int givenMaxTime, int givenMaxCoins) {
timeRemaining = 0;
coinMax = givenMaxCoins;
timeMax = givenMaxTime;
minutesForNickel = givenMinutesForNickel;
minutesForDime = givenMinutesForDime;
minutesForQuarter = givenMinutesForQuarter;
if (minutesForNickel > minutesForDime | minutesForDime > minutesForQuarter)
{
// TODO
}
if (!(minutesForNickel > 0 && minutesForDime > 0 && minutesForQuarter > 0))
{
// TODO
}
}
/**
* simulateTimeSimulates the passage of time for the given number of
* minutes. Does nothing if the given value is negative.
*
* @param minutes
* The passage of time, in minutes by default
*/
public void simulateTime(int minutes) {
// check if the minutes has exceeded the timeRemaining
int elapsedMinutes = minutes;
if (minutes > 0)
{
if (timeRemaining - minutes < 0)
{
elapsedMinutes = timeRemaining;
}
else
{
elapsedMinutes = minutes;
;
}
}
else
{
// TODO
}
timeRemaining = timeRemaining - elapsedMinutes;
}
/**
* emptyCoins reduces the coin to zero, without modifying the time
*/
public void emptyCoins() {
coinRemainingNumbers = 0;
coinRemainingValues = 0;
}
/**
* getCents returns the total amount of money, in cents, contained in this
* meter
*
* @return the total amount of mone
*/
public int getCents() {
return coinRemainingValues;
}
/**
* getDollarString returns a String representing the total amount of money,
* in dollars
*
* @return the total amount of money
*/
public String getDollarString() {
// times it by 0.01
String result = String.format("%01.2f", coinRemainingValues * 0.01);
return result;
}
/**
* getMinutesRemaining returns the amount of time, in minutes, remaining on
* this meter
*
* @return the amount of time
*/
public int getMinutesRemaining() {
return timeRemaining;
}
/**
* getHourMinuteString() returns a String representing the time remaining on
* the meter in the form "hh:mm"'
*
* @return the time remaining on the meter in the form "hh:mm"'
*/
public String getHourMinuteString() {
//
int h = timeRemaining / 60;
int m = timeRemaining - h * 60;
String result = String.format("%02d:%02d", h, m);
return result;
}
/**
* getTotalCoins() returns the number of coins currently in this meter
*
* @return the number of coins
*/
public int getTotalCoins() {
return coinRemainingNumbers;
}
/**
* addingCoins will increase the coinRemainingNumber for a given number of
* coins inserted until the coinMax
*
* @param howMany
* The number of coins inserted
*/
private void addingCoins(int howMany) {
int addedCoins = howMany;
if (addedCoins < 0)
{
addedCoins = 0;
}
else
{
coinRemainingNumbers = Math.min(coinRemainingNumbers + addedCoins, coinMax);
}
}
/**
* insertDimes(int howMany) inserts a given number of dimes into this meter,
*
* @param howMany
* The number of coins inserted
*/
public void insertDimes(int howMany) {
//Number of the dimes added
int addedDimesNumber;
addedDimesNumber = howMany;
int currentCoinRemainingNumbers = coinRemainingNumbers;
//Increase the coin numbers after inserting the dimes
this.addingCoins(addedDimesNumber);
//Coins can only be inserted until the coinMax
if (coinRemainingNumbers == coinMax)
{
addedDimesNumber = coinMax - currentCoinRemainingNumbers;
}
//Increase time after inserting coins
//Check the validity of the parameter howMany
if (addedDimesNumber > 0)
{
//It would be igored if the time has exceeded the max time amount
timeRemaining = Math.min(timeRemaining + addedDimesNumber * minutesForDime, timeMax);
//Only when the timeRemaining has exceeded the timeMax and the coinRemainingNumbers has not exceeded the coinMax,
//the coinRemainingValues can be added
if ((timeRemaining <= timeMax) && (coinRemainingNumbers <= coinMax))
{
coinRemainingValues = coinRemainingValues + addedDimesNumber * DIME_VALUE;
}
else
{
coinRemainingValues = coinRemainingValues;
}
}
}
/**
* insertNickels(int howMany) inserts a given number of dimes into this
* meter,
*
* @param howMany
* The number of coins inserted
*/
public void insertNickels(int howMany) {
//Number of the nickels added
int addedNickelsNumber;
addedNickelsNumber = howMany;
int currentCoinRemainingNumbers = coinRemainingNumbers;
this.addingCoins(addedNickelsNumber);
if (coinRemainingNumbers == coinMax)
{
addedNickelsNumber = coinMax - currentCoinRemainingNumbers;
}
if (addedNickelsNumber > 0)
{
//It would be igored if the time has exceeded the max time amount
timeRemaining = Math.min(timeRemaining + addedNickelsNumber * minutesForNickel, timeMax);
//Only when the timeRemaining has exceeded the timeMax and the coinRemainingNumbers has not exceeded the coinMax,
//the coinRemainingValues can be added
if ((timeRemaining <= timeMax) && (coinRemainingNumbers <= coinMax))
{
coinRemainingValues = coinRemainingValues + addedNickelsNumber * NICKEL_VALUE;
}
else
{
coinRemainingValues = coinRemainingValues;
}
}
}
/**
* insertQuarters(int howMany) inserts a given number of dimes into this
* meter,
*
* @param howMany
* The number of coins inserted
*/
public void insertQuarters(int howMany) {
int addedQuartersNumber;
addedQuartersNumber = howMany;
int currentCoinRemainingNumbers = coinRemainingNumbers;
this.addingCoins(addedQuartersNumber);
if (coinRemainingNumbers == coinMax)
{
addedQuartersNumber = coinMax - currentCoinRemainingNumbers;
}
if (addedQuartersNumber > 0)
{
///It would be igored if the time has exceeded the max time amount
timeRemaining = Math.min(timeRemaining + addedQuartersNumber * minutesForQuarter,timeMax);
//Only when the timeRemaining has exceeded the timeMax and the coinRemainingNumbers has not exceeded the coinMax,
//the coinRemainingValues can be added
if ((timeRemaining <= timeMax) && (coinRemainingNumbers <= coinMax))
{
coinRemainingValues = coinRemainingValues + addedQuartersNumber * QUARTER_VALUE;
}
else
{
coinRemainingValues = coinRemainingValues;
}
}
}
/**
* isExpired() returns true if there is no time remaining, false otherwise.
*
* @return returns true if there is no time remaining, false otherwise.
*/
public boolean isExpired() {
if (timeRemaining > 0)
{
return false;
}
else
{
return true;
}
}
/**
* toString returns a String representation of this object in exactly the
* following format: hw1.ParkingMeter
* [" + time + "," + number + "," + value + "]
*
* @return a String representation
*/
public String toString() {
String time = "Time" + timeRemaining + "/" + timeMax;
String number = "Coins" + coinRemainingNumbers + "/" + coinMax;
String value = "Value" + " " + coinRemainingValues;
String result = "hw1.ParkingMeter [" + time + "," + number + "," + value + "]";
return result;
}
}