This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhonePlan.java
535 lines (488 loc) · 17.4 KB
/
PhonePlan.java
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
public class PhonePlan {
private String phoneNumber;
private double monthlyCharge;
private int minuteLimit;
private int messageLimit;
private int megabyteLimit;
private double minuteRate;
private double messageRate;
private double downloadRate;
private double roamingRate;
private double internationalRate;
private double roamingMessageRate;
private double roamingDownloadRate;
private boolean active;
private boolean allowRoaming;
private boolean allowInternational;
private double taxRate;
private double balance;
private int minuteBalance;
private int textMessageBalance;
private int downloadBalance;
private int anniversaryDate;
/* Returns the phone number for this account
*/
public String getPhoneNumber() {
return phoneNumber;
}
/* Returns the monthly charge for this phone plan
*/
public double getMonthlyCharge() {
return monthlyCharge;
}
/* Sets the monthly charge for this phone plan
*/
public void setMonthlyCharge(double monthlyCharge) {
this.monthlyCharge = monthlyCharge;
}
/* Returns the amount of minutes available per month as part of the plan
*/
public int getMonthlyMinutes() {
return minuteLimit;
}
/* Sets the amount of minutes available per month as part of the plan
*/
public void setMonthlyMinutes(int minuteLimit) {
this.minuteLimit = minuteLimit;
}
/* Returns the number of text messages per month that may be sent on this plan
*/
public int getMonthlyMessages() {
return messageLimit;
}
/* Sets the number of text messages per month that may be sent on this plan
*/
public void setMonthlyMessages(int messageLimit) {
this.messageLimit = messageLimit;
}
/* Returns the number of megabytes that may be downloaded per month on this plan
*/
public int getMonthlyDownloads() {
return megabyteLimit;
}
/* Sets the number of megabytes that may be downloaded per month on this plan
*/
public void setMonthlyDownloads(int megabyteLimit) {
this.megabyteLimit = megabyteLimit;
}
/* Returns the amount charged per minute for a phone call beyond the monthly allotment
*/
public double getMinuteRate() {
return minuteRate;
}
/* Sets the amount charged per minute for a phone call beyond the monthly allotment
*/
public void setMinuteRate(double minuteRate) {
this.minuteRate = minuteRate;
}
/* Returns the amount charged per text message if sending more messages than the plan allows per month
*/
public double getMessageRate() {
return messageRate;
}
/* Sets the amount charged per text message that exceeds the number allowed per month
*/
public void setMessageRate(double messageRate) {
this.messageRate = messageRate;
}
/* Returns the amount charged per megabyte of download when the amount
* downloaded exceeds the plan limit for the month
*/
public double getDownloadRate() {
return downloadRate;
}
/* Sets the amount charged per megabyte of download that exceeds the amount allowed per month
*/
public void setDownloadRate(double downloadRate) {
this.downloadRate = downloadRate;
}
/* Returns the amount charged per minute for a roaming call
*/
public double getRoamingRate() {
return roamingRate;
}
/* Sets the amount charged per minute for a roaming call
*/
public void setRoamingRate(double roamingRate) {
this.roamingRate = roamingRate;
}
/* Returns the amount per minute for an outgoing international call
*/
public double getInternationalRate() {
return internationalRate;
}
/* Sets the amount per minute for an outgoing international call
*/
public void setInternationalRate(double internationalRate) {
this.internationalRate = internationalRate;
}
/* Gets the amount charged for a text message while roaming
*/
public double getRoamingMessageRate() {
return roamingMessageRate;
}
/* Sets the amount charged for a text message while roaming
*/
public void setRoamingMessageRate(double roamingMessageRate) {
this.roamingMessageRate = roamingMessageRate;
}
/* Gets the amount charged per megabyte of download while roaming
*/
public double getRoamingDownloadRate() {
return roamingDownloadRate;
}
/* Sets the amount charged per megabyte of download while roaming
*/
public void setRoamingDownloadRate(double roamingDownloadRate) {
this.roamingDownloadRate = roamingDownloadRate;
}
/* Returns true if the phone is active
*/
public boolean isActivated() {
return active;
}
/* Sets whether the phone is active or not
*/
public void setActivated(boolean active) {
this.active = active;
}
/* Returns true if the phone may make or recieve calls while roaming
*/
public boolean allowRoamingCalls() {
return allowRoaming;
}
/* Sets whether the phone may make or recieve calls while roaming
*/
public void setAllowRoamingCalls(boolean allowRoaming) {
this.allowRoaming = allowRoaming;
}
/* Returns true if the phone is allowed to make international calls
*/
public boolean allowInternationalCalls() {
return allowInternational;
}
/* Sets whether the phone may make international calls or not
*/
public void setAllowInternationalCalls(boolean allowInternational) {
this.allowInternational = allowInternational;
}
/* Returns the tax rate for the plan
*/
public double getTaxRate() {
return taxRate;
}
/* Sets the tax rate for the plan
*/
public void setTaxRate(double taxRate) {
this.taxRate = taxRate;
}
/* Returns the current cash balance for the the account
*/
public double getBalance() {
return balance;
}
/* Adds amount to account cash balance
*/
public void pay(double amount) {
balance = (balance + amount);
}
/* Returns the current minute balance for the account
*/
public int getMinuteBalance() {
return minuteBalance;
}
/* Returns the current message balance for the account
*/
public int getTextMessageBalance() {
return textMessageBalance;
}
/* Returns the current download balance for the account
*/
public int getDownloadBalance() {
return downloadBalance;
}
/* This method simulates sending or receiving a text messge. Text messages are paid for
* by the message balance and/or the cash balance. The method returns true if the text
* message was sent (or received) and the balances reduced appropriately, and false if
* the message was not sent because there is not enough remaining in the balances to pay for the message.
*/
public boolean sendTextMessage(boolean isRoaming) {
if (isRoaming == true) {
if (allowRoamingCalls() != true) {
return false;
}
else if (getBalance() >= (getRoamingMessageRate() + getMessageRate()) && getTextMessageBalance() == 0) {
balance = (getBalance() - (getRoamingMessageRate() + getMessageRate()));
return true;
}
else if (getBalance() >= getRoamingMessageRate() && getTextMessageBalance() > 0) {
balance = (getBalance() - getRoamingMessageRate());
textMessageBalance--;
return true;
}
else {
return false;
}
}
else {
if (getTextMessageBalance() >= 1) {
textMessageBalance--;
return true;
}
else if (getBalance() >= getMessageRate()) {
balance = (getBalance() - getMessageRate());
return true;
}
else {
return false;
}
}
}
/* This method simulates downloading data to the phone. Data downloads are paid
* for by the download balance and/or the cash balance. The method returns true
* if the desired megabytes were downloaded and the balances reduced appropriately,
* and false if the megabytes were not downloaded because there is not enough remaining
* in the balances to pay for the message.
*/
public boolean downloadFile(int megabytes, boolean isRoaming) {
if (isRoaming == true) {
if (allowRoamingCalls() != true) {
return false;
}
if (getDownloadBalance() >= megabytes) {
balance = (getBalance() - (getRoamingDownloadRate() * megabytes));
downloadBalance = getDownloadBalance() - megabytes;
return true;
}
if (getDownloadBalance() < megabytes && getBalance() > 0) {
balance = ((getBalance() - ((megabytes * getRoamingRate()) +
((megabytes - getDownloadRate()) * getDownloadRate()))));
downloadBalance = 0;
return true;
}
else {
return false;
}
}
else {
if (getDownloadBalance() >= megabytes) {
downloadBalance = (getDownloadBalance() - megabytes);
return true;
}
if (getDownloadBalance() < megabytes && getBalance() > 0) {
balance = (getBalance() - (getDownloadRate() * (Math.abs(getDownloadBalance() - megabytes))));
downloadBalance = 0;
return true;
}
else {
return false;
}
}
}
/* Returns the length of the longest possible call, given the current minutes balance and cash balance
*/
public int maxCallLength(boolean isRoaming, boolean isInternational) {
if (isActivated() != true) {
return 0;
}
else if (isRoaming == true && allowRoamingCalls() != true) {
return 0;
}
else if (isInternational == true && allowInternationalCalls() != true) {
return 0;
}
else if (isRoaming == true && isInternational != true) {
if((getBalance() / getRoamingRate()) < getMinuteBalance()) {
return ((int)(getBalance() / getRoamingRate()));
}
else {
return ((int)(((getBalance() - (getMinuteBalance() * getRoamingRate()))/ (getRoamingRate() + getMinuteRate()))
+ getMinuteBalance()));
}
}
else if (isRoaming == true && isInternational == true) {
if((balance / (roamingRate + internationalRate)) < minuteBalance) {
return ((int)(getBalance() / (getRoamingRate() + getInternationalRate())));
}
else {
return ((int)(((getBalance() - (getMinuteBalance() * (getRoamingRate() + getInternationalRate())))/ (getRoamingRate() + getInternationalRate() + getMinuteRate()))
+ getMinuteBalance()));
}
}
else if (isRoaming == false && isInternational == true) {
return 0;
}
else {
return ((int)(getBalance() / getMinuteRate()) + getMinuteBalance());
}
}
/* This method returns true if a call of length minutes was successfully made, and the
* various balances must be adjusted accordingly. If a call of length minutes can not be
* made (minutes is greater than the value returned by maxCallLength), there is no adjustment
* to the balances, and the method returns false.
*/
public boolean makeCall(int minutes, boolean isRoaming, boolean isInternational) {
int callLength = this.maxCallLength(isRoaming, isInternational);
if (minutes > callLength) {
return false;
}
else if (isRoaming == true && isInternational != true) {
if((getMinuteBalance() - minutes) >= 0) {
balance = (getBalance() - (minutes * getRoamingRate()));
minuteBalance = (getMinuteBalance() - minutes);
return true;
}
else {
balance = (getBalance() - ((minutes * getRoamingRate()) + ((Math.abs(getMinuteBalance() - minutes)) *
getMinuteRate())));
minuteBalance = 0;
return true;
}
}
else if (isRoaming == true && isInternational == true) {
if((minuteBalance - minutes) >= 0) {
balance = (getBalance() - ((minutes * getRoamingRate()) + (minutes * getInternationalRate())));
minuteBalance = (getMinuteBalance() - minutes);
return true;
}
else {
balance = (getBalance() - ((minutes * getRoamingRate()) + (minutes * getInternationalRate()) +
((Math.abs(getMinuteBalance() - minutes)) * getMinuteRate())));
minuteBalance = 0;
return true;
}
}
else if ((minuteBalance - minutes) >= 0) {
minuteBalance = (getMinuteBalance() - minutes);
return true;
}
else {
balance = (getBalance() - ((Math.abs(getMinuteBalance() - minutes)) * getMinuteRate()));
minuteBalance = 0;
return true;
}
}
/* This method initializes the month for the plan
*/
public boolean startMonth(int date) {
if(balance >= (getMonthlyCharge() + (getMonthlyCharge() * getTaxRate()))) {
balance = (getBalance() - (getMonthlyCharge() + (getMonthlyCharge() * getTaxRate())));
minuteBalance = getMonthlyMinutes();
textMessageBalance = getMonthlyMessages();
downloadBalance = getMonthlyDownloads();
if(isActivated() == false) {
active = true;
anniversaryDate = date;
};
return true;
}
else {
minuteBalance = 0;
textMessageBalance = 0;
downloadBalance = 0;
active = false;
return false;
}
}
/* This method checks to see if the day is the aniversary date
*/
public void processDay(int day, int month) {
if ((month == 1 && day == 31 && getAnniversaryDate() <
day && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 2 && day == 28 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 3 && day == 31 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 4 && day == 30 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 5 && day == 31 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 6 && day == 30 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 7 && day == 31 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 8 && day == 31 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 9 && day == 30 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 10 && day == 31 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 11 && day == 30 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
else if ((month == 12 && day == 31 && getAnniversaryDate() <
day && isActivated() == true) || (day == getAnniversaryDate() && isActivated() == true)) {
startMonth(anniversaryDate);
}
}
/* Returns the anniversary date of the plan
*/
public int getAnniversaryDate() {
return anniversaryDate;
}
/* A constructor that sets the phone number, monthly charge, monthly minute limit,
* monthly message limit, and monthly download limit for a phone plan
*/
public PhonePlan(String phoneNumber, double monthlyCharge, int monthlyMinutes,
int monthlyMessages, int monthlyDownloads) {
this.phoneNumber = phoneNumber;
this.monthlyCharge = monthlyCharge;
this.minuteLimit = monthlyMinutes;
this.messageLimit = monthlyMessages;
this.megabyteLimit = monthlyDownloads;
minuteRate = .1;
messageRate = .5;
downloadRate = .01;
roamingRate = .1;
roamingMessageRate = .1;
roamingDownloadRate = .01;
internationalRate = .1;
taxRate = .06;
active = false;
allowRoaming = true;
allowInternational = true;
balance = 0;
minuteBalance = getMonthlyMinutes();
downloadBalance = getMonthlyDownloads();
textMessageBalance = getMonthlyMessages();
anniversaryDate = 16;
}
/* A constructor that sets the minute rate, roaming rate, international rate,
* message rate, download rate, roaming message rate, roaming download rate, and tax rate)
*/
public PhonePlan(String phoneNumber, double monthlyCharge, int monthlyMinutes, int monthlyMessages,
int monthlyDownloads, double minuteRate, double roamingRate, double internationalRate,
double messageRate, double downloadRate, double roamingMessageRate,
double roamingDownloadRate, double taxRate) {
this(phoneNumber, monthlyCharge, monthlyMinutes, monthlyMessages, monthlyDownloads);
this.minuteRate = minuteRate;
this.roamingRate = roamingRate;
this.internationalRate = internationalRate;
this.messageRate = messageRate;
this.downloadRate = downloadRate;
this.roamingMessageRate = roamingMessageRate;
this.roamingDownloadRate = roamingDownloadRate;
this.taxRate = taxRate;
}
}