Mengenrabatt: Staffel im Rechner und eine Seite im Web

agentd task 4027a3aa01be47e9b677e7c687e4b183
This commit is contained in:
agentd
2026-08-07 13:04:33 +00:00
parent 605a952f7a
commit 5d83ffaa76
11 changed files with 532 additions and 352 deletions

View File

@@ -16,6 +16,57 @@ public static class PriceCalculator
throw new ArgumentOutOfRangeException(nameof(discountPercent), "A discount is between 0 and 100 percent.");
}
return price - discountPercent;
return price * (1m - discountPercent / 100m);
}
/// <summary>Rounds an amount to the nearest 0.05, rounding halves away from zero.</summary>
public static decimal RoundToFiveRappen(decimal amount)
{
return Math.Round(amount * 20m, MidpointRounding.AwayFromZero) / 20m;
}
/// <summary>Returns the quantity discount percent for a quantity: 0, 5 or 10.</summary>
public static int GetQuantityDiscountPercent(int quantity)
{
if (quantity < 1)
{
throw new ArgumentOutOfRangeException(nameof(quantity), "A quantity must be at least 1.");
}
return quantity switch
{
>= 50 => 10,
>= 10 => 5,
_ => 0,
};
}
/// <summary>
/// Calculates the total: percentage discount on the unit price first, then the quantity
/// discount on the total, then rounds the end amount to the nearest 0.05.
/// </summary>
public static decimal CalculateTotal(decimal unitPrice, int quantity, decimal discountPercent)
{
if (unitPrice < 0)
{
throw new ArgumentOutOfRangeException(nameof(unitPrice), "A price cannot be negative.");
}
if (quantity < 1)
{
throw new ArgumentOutOfRangeException(nameof(quantity), "A quantity must be at least 1.");
}
if (discountPercent is < 0 or > 100)
{
throw new ArgumentOutOfRangeException(nameof(discountPercent), "A discount is between 0 and 100 percent.");
}
var discountedUnitPrice = ApplyDiscount(unitPrice, discountPercent);
var totalBeforeQuantityDiscount = discountedUnitPrice * quantity;
var quantityDiscountPercent = GetQuantityDiscountPercent(quantity);
var totalBeforeRounding = totalBeforeQuantityDiscount * (1m - quantityDiscountPercent / 100m);
return RoundToFiveRappen(totalBeforeRounding);
}
}