Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Last active April 7, 2024 05:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DhavalDalal/958a50307d214206813836bf1d8d0d97 to your computer and use it in GitHub Desktop.
Save DhavalDalal/958a50307d214206813836bf1d8d0d97 to your computer and use it in GitHub Desktop.
Loyalty Points - C#

Loyalty Points (C#)

  • Create a C# Library Project - you can name it LoyaltyPoints.
  • Copy PointsCalculatorTest.cs under project PrivilegeServiceSpecs. Add reference to NUnit in this project.
  • Copy Tier.cs, PrivilegeService.cs, PointsCalculator.cs, CustomerRepository.cs and Customer.cs under project PrivilegeService.
  • 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.
namespace LoyaltyPoints
{
public class Customer
{
public Tier Tier { get; set; }
public int Points { get; set; }
}
}
namespace LoyaltyPoints
{
class CustomerRepository
{
public Customer FindById(string customerId)
{
return new Customer { Points = 120, Tier = Tier.Gold };
}
}
}
using System;
namespace LoyaltyPoints
{
public class PointsCalculator
{
private static float AMOUNT_PER_POINT = 100.00f;
public int CalculatePoints(Tier tier, float amountSpent) {
float earnedPoints = amountSpent / AMOUNT_PER_POINT;
var points = Math.Floor(earnedPoints + Bonus(tier, earnedPoints));
return Convert.ToInt32(points);
}
private float Bonus(Tier tier, float amountSpent)
{
if (tier == Tier.Silver) {
return 0.15f * amountSpent;
}
if (tier == Tier.Gold) {
return 0.3f * amountSpent;
}
if (tier == Tier.Platinum) {
return 0.5f * amountSpent;
}
return 0f;
}
}
}
using NUnit.Framework;
namespace LoyaltyPoints
{
[TestFixture]
public class PointsCalculatorTest
{
private PointsCalculator calculator = new PointsCalculator();
[Test]
public void BlueCustomerEarnsNoBonusPoints()
{
Assert.AreEqual(0, calculator.CalculatePoints(Tier.Blue, 99.99f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Blue, 100.00f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Blue, 199.99f));
Assert.AreEqual(2, calculator.CalculatePoints(Tier.Blue, 200.00f));
}
[Test]
public void SilverCustomerEarns15PercentBonusPointsAboveBlueCustomer()
{
Assert.AreEqual(0, calculator.CalculatePoints(Tier.Silver, 86.95f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Silver, 86.96f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Silver, 173.91f));
Assert.AreEqual(2, calculator.CalculatePoints(Tier.Silver, 173.92f));
}
[Test]
public void GoldCustomerEarns30PercentBonusPointsAboveBlueCustomer()
{
Assert.AreEqual(0, calculator.CalculatePoints(Tier.Gold, 76.92f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Gold, 76.93f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Gold, 153.84f));
Assert.AreEqual(2, calculator.CalculatePoints(Tier.Gold, 153.85f));
}
[Test]
public void PlatinumCustomerEarns50PercentBonusPointsOverBlueCustomer()
{
Assert.AreEqual(0, calculator.CalculatePoints(Tier.Platinum, 66.66f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Platinum, 66.67f));
Assert.AreEqual(1, calculator.CalculatePoints(Tier.Platinum, 133.33f));
}
}
}
namespace LoyaltyPoints
{
public class PrivilegeService
{
private readonly PointsCalculator pointsCalculator = new PointsCalculator();
private CustomerRepository customerRepository = new CustomerRepository();
public int LoyaltyPoints(string customerId, float amountSpent)
{
var customer = customerRepository.FindById(customerId);
Tier tier = customer.Tier;
int newPoints = pointsCalculator.CalculatePoints(tier, amountSpent);
int oldPoints = customer.Points;
customer.Points = oldPoints + newPoints;
return customer.Points;
}
// Other public methods
// Tier updateTier(Customer customer);
// ...
}
}
namespace LoyaltyPoints
{
public enum Tier
{
Platinum = 0,
Gold = 1,
Silver = 2,
Blue = 3
}
}
@shaeelabbas96
Copy link

How do you run this program?

@dvnsh-shah
Copy link

please provide the main method as well

@DhavalDalal
Copy link
Author

Tests are good enough, you don't really need a Main method. If you still need it, it will not take much time to hand-toss one on your own

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