Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

734 use decimal for money #737

Merged
merged 8 commits into from
Dec 17, 2018
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
4 changes: 2 additions & 2 deletions exercises/bank-account/BankAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public void Close()
throw new NotImplementedException("You need to implement this function.");
}

public float Balance
public decimal Balance
AndrewYHuang marked this conversation as resolved.
Show resolved Hide resolved
{
get
{
throw new NotImplementedException("You need to implement this property.");
}
}

public void UpdateBalance(float change)
public void UpdateBalance(decimal change)
{
throw new NotImplementedException("You need to implement this function.");
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/bank-account/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class BankAccount
{
private readonly object _lock = new object();

private float balance;
private decimal balance;
private bool isOpen;

public void Open()
Expand All @@ -23,7 +23,7 @@ public void Close()
}
}

public float Balance
public decimal Balance
{
get
{
Expand All @@ -39,7 +39,7 @@ public float Balance
}
}

public void UpdateBalance(float change)
public void UpdateBalance(decimal change)
{
lock(_lock)
{
Expand Down
2 changes: 1 addition & 1 deletion exercises/book-store/BookStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

public static class BookStore
{
public static double Total(IEnumerable<int> books)
public static decimal Total(IEnumerable<int> books)
AndrewYHuang marked this conversation as resolved.
Show resolved Hide resolved
{
throw new NotImplementedException("You need to implement this function.");
}
Expand Down
30 changes: 15 additions & 15 deletions exercises/book-store/BookStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,104 +9,104 @@ public class BookStoreTest
public void Only_a_single_book()
{
var basket = new[] { 1 };
Assert.Equal(8, BookStore.Total(basket));
Assert.Equal(8m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_of_the_same_book()
{
var basket = new[] { 2, 2 };
Assert.Equal(16, BookStore.Total(basket));
Assert.Equal(16m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Empty_basket()
{
var basket = Array.Empty<int>();
Assert.Equal(0, BookStore.Total(basket));
Assert.Equal(0m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_different_books()
{
var basket = new[] { 1, 2 };
Assert.Equal(15.2, BookStore.Total(basket));
Assert.Equal(15.2m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Three_different_books()
{
var basket = new[] { 1, 2, 3 };
Assert.Equal(21.6, BookStore.Total(basket));
Assert.Equal(21.6m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Four_different_books()
{
var basket = new[] { 1, 2, 3, 4 };
Assert.Equal(25.6, BookStore.Total(basket));
Assert.Equal(25.6m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Five_different_books()
{
var basket = new[] { 1, 2, 3, 4, 5 };
Assert.Equal(30, BookStore.Total(basket));
Assert.Equal(30m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three()
{
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 5 };
Assert.Equal(51.2, BookStore.Total(basket));
Assert.Equal(51.2m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_groups_of_four_is_cheaper_than_groups_of_five_and_three()
{
var basket = new[] { 1, 1, 2, 3, 4, 4, 5, 5 };
Assert.Equal(51.2, BookStore.Total(basket));
Assert.Equal(51.2m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three()
{
var basket = new[] { 1, 1, 2, 2, 3, 4 };
Assert.Equal(40.8, BookStore.Total(basket));
Assert.Equal(40.8m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_each_of_first_4_books_and_1_copy_each_of_rest()
{
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5 };
Assert.Equal(55.6, BookStore.Total(basket));
Assert.Equal(55.6m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_copies_of_each_book()
{
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
Assert.Equal(60, BookStore.Total(basket));
Assert.Equal(60m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Three_copies_of_first_book_and_2_each_of_remaining()
{
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1 };
Assert.Equal(68, BookStore.Total(basket));
Assert.Equal(68m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Three_each_of_first_2_books_and_2_each_of_remaining_books()
{
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2 };
Assert.Equal(75.2, BookStore.Total(basket));
Assert.Equal(75.2m, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three()
{
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5 };
Assert.Equal(102.4, BookStore.Total(basket));
Assert.Equal(102.4m, BookStore.Total(basket));
}
}
30 changes: 15 additions & 15 deletions exercises/book-store/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

public static class BookStore
{
private const double BookPrice = 8.0;
private const decimal BookPrice = 8.0m;

public static double Total(int[] books)
public static decimal Total(int[] books)
{
if (books.Length == 0)
return 0.0;
return 0.0m;

var bookGroups = BookGroupsWithCount(books);

return Enumerable.Range(1, bookGroups.Length)
.Min(size => CalculateTotalCost(bookGroups, size, 0.0));
.Min(size => CalculateTotalCost(bookGroups, size, 0.0m));
}

private static int[] BookGroupsWithCount(int[] books)
Expand All @@ -24,7 +24,7 @@ private static int[] BookGroupsWithCount(int[] books)
.OrderByDescending(book => book)
.ToArray();

private static double CalculateTotalCost(int[] bookGroups, int numberOfBooksToRemove, double totalCost)
private static decimal CalculateTotalCost(int[] bookGroups, int numberOfBooksToRemove, decimal totalCost)
{
var numberOfBooks = Math.Min(numberOfBooksToRemove, bookGroups.Length);
if (numberOfBooks == 0)
Expand All @@ -48,23 +48,23 @@ private static int[] RemoveBooks(int[] bookGroups, int numberOfBooks)

private static int RemoveBook(int books) => books - 1;

private static double BooksPrice(int differentBooks)
private static decimal BooksPrice(int differentBooks)
=> ApplyDiscount(RegularPrice(differentBooks), DiscountPercentage(differentBooks));

private static double RegularPrice(int books) => books * BookPrice;
private static decimal RegularPrice(int books) => books * BookPrice;

private static double DiscountPercentage(int differentBooks)
private static decimal DiscountPercentage(int differentBooks)
{
switch (differentBooks)
{
case 5: return 25.0;
case 4: return 20.0;
case 3: return 10.0;
case 2: return 5.0;
default: return 0.0;
case 5: return 25.0m;
case 4: return 20.0m;
case 3: return 10.0m;
case 2: return 5.0m;
default: return 0.0m;
}
}

private static double ApplyDiscount(double price, double discountPercentage)
=> Math.Round(price * (100.0f - discountPercentage) / 100.0f, 2);
private static decimal ApplyDiscount(decimal price, decimal discountPercentage)
=> Math.Round(price * (100.0m - discountPercentage) / 100.0m, 2);
}
10 changes: 5 additions & 5 deletions exercises/ledger/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class LedgerEntry
{
public LedgerEntry(DateTime date, string description, float change)
public LedgerEntry(DateTime date, string description, decimal change)
{
Date = date;
Description = description;
Expand All @@ -14,7 +14,7 @@ public LedgerEntry(DateTime date, string description, float change)

public DateTime Date { get; }
public string Description { get; }
public float Change { get; }
public decimal Change { get; }
}

public static class Ledger
Expand All @@ -27,7 +27,7 @@ public static LedgerEntry CreateEntry(string date, string description, int chang

private static DateTime ParseDate(string date) => DateTime.Parse(date, System.Globalization.CultureInfo.InvariantCulture);

private static float ParseChange(int change) => change / 100.0f;
private static decimal ParseChange(int change) => change / 100.0m;

private static CultureInfo CultureInfo(string locale)
{
Expand Down Expand Up @@ -93,8 +93,8 @@ private static string FormatHeader(CultureInfo culture)
private static string FormatDescription(string description) =>
description.Length <= TruncateLength ? description : description.Substring(0, TruncateLength - TruncateSuffix.Length) + TruncateSuffix;

private static string FormatChange(IFormatProvider culture, float change) =>
change < 0.0 ? change.ToString("C", culture) : change.ToString("C", culture) + " ";
private static string FormatChange(IFormatProvider culture, decimal change) =>
change < 0.0m ? change.ToString("C", culture) : change.ToString("C", culture) + " ";

private static string FormatEntry(IFormatProvider culture, LedgerEntry entry) =>
string.Format("{0} | {1,-25} | {2,13}", FormatDate(culture, entry.Date), FormatDescription(entry.Description), FormatChange(culture, entry.Change));
Expand Down
10 changes: 5 additions & 5 deletions exercises/ledger/Ledger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class LedgerEntry
{
public LedgerEntry(DateTime date, string desc, float chg)
public LedgerEntry(DateTime date, string desc, decimal chg)
AndrewYHuang marked this conversation as resolved.
Show resolved Hide resolved
{
Date = date;
Desc = desc;
Expand All @@ -14,14 +14,14 @@ public LedgerEntry(DateTime date, string desc, float chg)

public DateTime Date { get; }
public string Desc { get; }
public float Chg { get; }
public decimal Chg { get; }
}

public static class Ledger
{
public static LedgerEntry CreateEntry(string date, string desc, int chng)
{
return new LedgerEntry(DateTime.Parse(date, CultureInfo.InvariantCulture), desc, chng / 100.0f);
return new LedgerEntry(DateTime.Parse(date, CultureInfo.InvariantCulture), desc, chng / 100.0m);
}

private static CultureInfo CreateCulture(string cur, string loc)
Expand Down Expand Up @@ -113,9 +113,9 @@ private static string Description(string desc)
return desc;
}

private static string Change(IFormatProvider culture, float cgh)
private static string Change(IFormatProvider culture, decimal cgh)
{
return cgh < 0.0 ? cgh.ToString("C", culture) : cgh.ToString("C", culture) + " ";
return cgh < 0.0m ? cgh.ToString("C", culture) : cgh.ToString("C", culture) + " ";
}

private static string PrintEntry(IFormatProvider culture, LedgerEntry entry)
Expand Down
2 changes: 1 addition & 1 deletion generators/Exercises/Generators/BookStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected override void UpdateTestMethod(TestMethod testMethod)
if (testMethod.Input["basket"] is JArray)
testMethod.Input["basket"] = Array.Empty<int>();

testMethod.Expected = testMethod.Expected / 100.0f;
testMethod.Expected = testMethod.Expected / 100.0m;
testMethod.InputParameters = new[] { "basket" };
testMethod.UseVariablesForInput = true;
}
Expand Down
1 change: 1 addition & 0 deletions generators/Output/Rendering/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public string Object(object val)
switch (val)
{
case string str: return String(str);
case decimal dec: return Decimal(dec);
case double dbl: return Double(dbl);
case int i: return Int(i);
case uint ui: return Uint(ui);
Expand Down
2 changes: 2 additions & 0 deletions generators/Output/Rendering/RenderNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Exercism.CSharp.Output.Rendering
{
public partial class Render
{
public string Decimal(decimal dec) => $"{dec.ToString(CultureInfo.InvariantCulture)}m";
AndrewYHuang marked this conversation as resolved.
Show resolved Hide resolved

public string Double(double dbl) => dbl.ToString(CultureInfo.InvariantCulture);

public string Float(float flt) => flt.ToString(CultureInfo.InvariantCulture);
Expand Down