Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/OrderCalc/OrderAmount.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;

namespace OrderCalc
Expand All @@ -15,7 +15,10 @@ public static class OrderAmount
public static decimal LineAmount(OrderLine line)
{
Validate(line);
return line.UnitPrice * line.Quantity - (decimal)line.Discount;
// 明細単価(割引後)= UnitPrice × (1 - Discount)、明細金額=それ × Quantity。
// 小数第 3 位を四捨五入して小数第 2 位まで(HLD/Common.md「金額の算出式」)。
decimal discounted = line.UnitPrice * (1m - (decimal)line.Discount);
return Math.Round(discounted * line.Quantity, 2, MidpointRounding.AwayFromZero);
}

/// <summary>小計(同一受注の全明細の明細金額の和)</summary>
Expand All @@ -24,8 +27,7 @@ public static decimal Subtotal(IEnumerable<OrderLine> lines)
decimal sum = 0m;
foreach (OrderLine line in lines)
{
Validate(line);
sum += line.UnitPrice * line.Quantity - (decimal)line.Discount;
sum += LineAmount(line);
}
return sum;
}
Expand Down
40 changes: 39 additions & 1 deletion tests/OrderCalc.Tests/OrderAmountTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NUnit.Framework;
using NUnit.Framework;
using OrderCalc;

namespace OrderCalc.Tests
Expand All @@ -12,6 +12,44 @@ public void LineAmount_割引なし()
Assert.That(OrderAmount.LineAmount(new OrderLine(14.00m, 12, 0f)), Is.EqualTo(168.00m));
}

// 根拠:#9(HLD/Common.md「金額の算出式」:14.00 × (1 − 0.25) × 12 = 126.00)
[Test]
public void LineAmount_割引率を掛ける_Issue9()
{
Assert.That(OrderAmount.LineAmount(new OrderLine(14.00m, 12, 0.25f)), Is.EqualTo(126.00m));
}

// 根拠:#9(同「明細金額は小数第 3 位を四捨五入」:0.50 × 0.05 = 0.025 → 0.03)
[Test]
public void LineAmount_小数第3位を四捨五入_Issue9()
{
Assert.That(OrderAmount.LineAmount(new OrderLine(0.50m, 1, 0.95f)), Is.EqualTo(0.03m));
}

// 根拠:#9(同「小計は丸めた明細金額の合算」。Subtotal にも同じ誤りがあった)
[Test]
public void Subtotal_割引のある明細を含む_Issue9()
{
var lines = new[] { new OrderLine(14.00m, 12, 0.25f), new OrderLine(9.80m, 10, 0f) };
Assert.That(OrderAmount.Subtotal(lines), Is.EqualTo(224.00m));
}

// 根拠:#9(同「その値(丸めた明細金額)を合算して小計」:0.03 + 0.03 = 0.06。合算後に丸めると 0.05)
[Test]
public void Subtotal_丸めた明細金額を合算_Issue9()
{
var lines = new[] { new OrderLine(0.50m, 1, 0.95f), new OrderLine(0.50m, 1, 0.95f) };
Assert.That(OrderAmount.Subtotal(lines), Is.EqualTo(0.06m));
}

// 根拠:#9(同「受注合計 = 小計 + Freight」:126.00 + 32.38)
[Test]
public void Total_割引のある明細と送料_Issue9()
{
var lines = new[] { new OrderLine(14.00m, 12, 0.25f) };
Assert.That(OrderAmount.Total(lines, 32.38m), Is.EqualTo(158.38m));
}

[Test]
public void Subtotal_割引なしの2行()
{
Expand Down