2. The RewardCard class, which you will write, represents a customer loyalty card that accumulates points from purchases and allows the customer to claim rewards. RewardCard objects are created by calls to a constructor with two parameters.
RewardCard objects are created by calls to a constructor with two parameters.
The first parameter is a String representing the name of the cardholder. (Precondition: Assume that this value is not null and not empty.)
The second parameter is an int representing the number of points required to claim a single reward. (Precondition: Assume that this value will be greater than 0.)
makePurchase
The RewardCard class contains a makePurchase method, which calculates points earned from a purchase and adds them to the card's total points.
A double representing the purchase amount in dollars. Assume this value is greater than or equal to 0.0.
The card earns 100 points for every full $10.00 spent in the purchase. For example, a purchase of $29.99 earns 200 points, and a purchase of $5.50 earns 0 points.
The calculated points are added to the card's total points.
The method returns the total points on the card after the purchase has been processed.
Returns an int representing the updated total points.
claimReward
The RewardCard class contains a claimReward method, which attempts to redeem points for a reward.
If the card's total points are greater than or equal to the required points threshold (set in the constructor), the threshold amount is subtracted from the total points.
In this case, the method returns a string in the format "[name] claimed a reward! Points remaining: [points]", where [name] is the cardholder's name and [points] is the updated total points.
If the card's total points are less than the required points threshold, the total points remain unchanged.
In this case, the method returns a string in the format "[name] needs [X] more points.", where [X] is the difference between the threshold and the current total points.
Returns a formatted String indicating the result of the claim attempt.
Statement | Return Value (blank if no value) | Explanation |
|---|---|---|
String res; | ||
int pts; | ||
RewardCard card1 = new RewardCard("Alex", 500); | RewardCard card1 is constructed for "Alex" with a threshold of 500 points. The initial total points is 0. | |
pts = card1.makePurchase(25.50); | 200 | A purchase of $25.50 earns 200 points (2 full $10s). The total points is now 200. |
res = card1.claimReward(); | "Alex needs 300 more points." | The total points (200) is less than the threshold (500). The difference is 300. |
pts = card1.makePurchase(40.00); | 600 | A purchase of $40.00 earns 400 points (4 full $10s). The total points is now 600. |
res = card1.claimReward(); | "Alex claimed a reward! Points remaining: 100" | The total points (600) is >= 500. 500 points are subtracted, leaving 100 points. |
res = card1.claimReward(); | "Alex needs 400 more points." | The total points (100) is less than the threshold (500). The difference is 400. |
RewardCard card2 = new RewardCard("Sam", 200); | RewardCard card2 is constructed for "Sam" with a threshold of 200 points. The initial total points is 0. | |
pts = card2.makePurchase(15.00); | 100 | A purchase of $15.00 earns 100 points. The total points is now 100. |
pts = card2.makePurchase(12.50); | 200 | A purchase of $12.50 earns 100 points. The total points is now 200. |
res = card2.claimReward(); | "Sam claimed a reward! Points remaining: 0" | The total points (200) is >= 200. 200 points are subtracted, leaving 0 points. |
Write the complete RewardCard class. Your implementation must meet all specifications and conform to the examples shown in the table.
00:00