-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCouponValidator.java
More file actions
38 lines (36 loc) · 1.72 KB
/
CouponValidator.java
File metadata and controls
38 lines (36 loc) · 1.72 KB
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
package org.codedifferently;
import java.util.Random;
public class CouponValidator {
// Checks the coupon code to see if a discount should be applied.
public static double validateCoupon(String coupon, Random random) {
System.out.println("\nChecking for coupon code...");
if ((coupon).equalsIgnoreCase("friend")) {
// Applies a 15% discount if the "friend" coupon code is used.
System.out.println("Coupon code " + "\"" + coupon + "\" accepted!");
return 0.15;
} else if ((coupon).equalsIgnoreCase("employee")) {
// Applies a 30% discount if the coupon code "employee" is used.
System.out.println("Coupon code " + "\"" + coupon + "\" accepted!");
return 0.30;
} else if (coupon.equalsIgnoreCase("chance")) {
/* Starts "chance time" which will roll a die. If the result is even,
a 40% discount will be applied. If it is odd, all items will become 5%
more expensive.
*/
System.out.println("Coupon code " + "\"" + coupon + "\" accepted!\nWelcome to Chance Time!\nRolling the dice (1-6)...");
int diceRoll = (random.nextInt(1, 7));
System.out.println("Rolled " + diceRoll + ".");
if (diceRoll % 2 == 0) {
System.out.println("YOU'VE GOTTEN LUCKY!!!");
return 0.40;
} else {
System.out.println("Better luck next time...");
return -0.05;
}
} else {
// The coupon code provided does not exist. The discount rate will be 0%.
System.out.println("Coupon code " + "\"" + coupon + "\" not found!");
return 0.0;
}
}
}