Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Last active February 12, 2024 12:09
Show Gist options
  • Save DhavalDalal/277465e43c01460d6ff3030ae6462d8e to your computer and use it in GitHub Desktop.
Save DhavalDalal/277465e43c01460d6ff3030ae6462d8e to your computer and use it in GitHub Desktop.
Loyalty Points - Java

Loyalty Points (Java)

  • You can use Gradle to create a Java Project - you can name it LoyaltyPoints.
  • Copy PointsCalculatorTest.java under package loyaltyPoints within src/test/java
  • Copy Tier.java, PrivilegeService.java, PointsCalculator.java and Customer.java under package loyaltyPoints within src/main/java
  • Add JUnit4 or JUnit5 Jar or latest JUnit as a test dependency.
  • Run the tests to get a green bar.

An airline has following rules for calculating bonus amount for loyalty points for a customer based on which tier they are in:

  • If a customer is in Platinum tier, add 50% more points above regular points.
  • If a customer is in Gold tier, add 30% more points above regular points.
  • If a customer is in Silver tier, add 15% more points above regular points.
  • If a customer is in Blue tier, then there are no bonus points, just regular points.
  • Spending INR 100 earns a regular point.

I want to add Titanium Tier, that gives 75% more points above regular points, can you help me?

NOTE

If you are behind proxy, and use gradle as a build tool, you can add the following lines to gradle.properties:

systemProp.http.proxyHost=Proxy IP Address
SystemProp.http.proxyPort=83
systemProp.https.proxyHost=Proxy IP Address
SystemProp.https.proxyPort=83
package loyaltyPoints;
public class Customer {
private Tier tier;
private int points;
public Tier getTier() {
return tier;
}
public void setTier(Tier tier) {
this.tier = tier;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}
package loyaltyPoints;
public class PointsCalculator {
private static float AMOUNT_PER_POINT = 100.00f;
public int calculateTotalPoints(Tier tier, float amountSpent) {
final float earnedPoints = earnedPoints(amountSpent);
return (int)(earnedPoints + bonus(tier, earnedPoints));
}
private float earnedPoints(float amountSpent) {
return amountSpent / AMOUNT_PER_POINT;
}
private float bonus(Tier tier, float earnedPoints) {
if (tier == Tier.SILVER) {
return 0.15f * earnedPoints;
}
if (tier == Tier.GOLD) {
return 0.3f * earnedPoints;
}
if (tier == Tier.PLATINUM) {
return 0.5f * earnedPoints;
}
return 0f;
}
}
package loyaltyPoints;
import static org.junit.Assert.*;
import org.junit.Test;
public class PointsCalculatorTest {
private PointsCalculator calculator = new PointsCalculator();
@Test
public void blueCustomerEarnsNoBonusPoints() {
assertEquals(0, calculator.calculateTotalPoints(Tier.BLUE, 99.99f));
assertEquals(1, calculator.calculateTotalPoints(Tier.BLUE, 100.00f));
assertEquals(1, calculator.calculateTotalPoints(Tier.BLUE, 199.99f));
assertEquals(2, calculator.calculateTotalPoints(Tier.BLUE, 200.00f));
}
@Test
public void silverCustomerEarns15PercentBonusPointsAboveBlueCustomer() {
assertEquals(0, calculator.calculateTotalPoints(Tier.SILVER, 86.95f));
assertEquals(1, calculator.calculateTotalPoints(Tier.SILVER, 86.96f));
assertEquals(1, calculator.calculateTotalPoints(Tier.SILVER, 173.91f));
assertEquals(2, calculator.calculateTotalPoints(Tier.SILVER, 173.92f));
}
@Test
public void goldCustomerEarns30PercentBonusPointsAboveBlueCustomer() {
assertEquals(0, calculator.calculateTotalPoints(Tier.GOLD, 76.92f));
assertEquals(1, calculator.calculateTotalPoints(Tier.GOLD, 76.93f));
assertEquals(1, calculator.calculateTotalPoints(Tier.GOLD, 153.84f));
assertEquals(2, calculator.calculateTotalPoints(Tier.GOLD, 153.85f));
}
@Test
public void platinumCustomerEarns50PercentBonusPointsOverBlueCustomer() {
assertEquals(0, calculator.calculateTotalPoints(Tier.PLATINUM, 66.66f));
assertEquals(1, calculator.calculateTotalPoints(Tier.PLATINUM, 66.67f));
assertEquals(1, calculator.calculateTotalPoints(Tier.PLATINUM, 133.33f));
assertEquals(2, calculator.calculateTotalPoints(Tier.PLATINUM, 133.34f));
}
}
package loyaltyPoints;
public class PrivilegeService {
private PointsCalculator pointsCalculator = new PointsCalculator();
public int loyaltyPoints(Customer customer, float amountSpent) {
Tier tier = customer.getTier();
int newPoints = pointsCalculator.calculateTotalPoints(tier, amountSpent);
int oldPoints = customer.getPoints();
customer.setPoints(oldPoints + newPoints);
return customer.getPoints();
}
// Other public methods
// Tier updateTier(Customer customer) {
// ...
}
package loyaltyPoints;
enum Tier {
PLATINUM, GOLD, SILVER, BLUE;
}
@DhavalDalal
Copy link
Author

DhavalDalal commented Sep 16, 2021

If you are behind proxy, and use gradle as a build tool, you can add the following lines to gradle.properties:

systemProp.http.proxyHost=Proxy IP Address
SystemProp.http.proxyPort=83
systemProp.https.proxyHost=Proxy IP Address
SystemProp.https.proxyPort=83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment