diff --git a/UnitTestProject/LinqUnitTests.cs b/UnitTestProject/LinqUnitTests.cs index decf5bd..a87480a 100644 --- a/UnitTestProject/LinqUnitTests.cs +++ b/UnitTestProject/LinqUnitTests.cs @@ -66,7 +66,7 @@ public void Test_WhatIsTheTotalNumberOfTransactions() [Test] public void Test_WhatIsTheTotalQuantityPurchased() { - var result = ""; // TODO + var result = transactions.Sum(t => t.Quantity); Assert.AreEqual(3001, result); } @@ -74,15 +74,21 @@ public void Test_WhatIsTheTotalQuantityPurchased() [Test] public void Test_WhatIsTheTotalQuantityPurchasedIn2016() { - var result = ""; // TODO + var result = transactions + .Where(t => t.Date.Year == 2016) + .Sum(t => t.Quantity); Assert.AreEqual(1160, result); } - + [Test] public void Test_WhatIsTheTotalQuantityPurchasedInThePast7Days() { - var result = ""; // TODO + DateTime fromDate = DateTime.Now.AddDays(-7); + + var result = transactions + .Where(t => t.Date >= fromDate) + .Sum(t => t.Quantity); Assert.AreEqual(32, result); } @@ -90,7 +96,9 @@ public void Test_WhatIsTheTotalQuantityPurchasedInThePast7Days() [Test] public void Test_HowManyTransactionsBoughtMoreThan1Quantity() { - var result = ""; // TODO + var result = transactions + .Where(t => t.Quantity > 1) + .Count(); Assert.AreEqual(1001, result); } @@ -98,7 +106,9 @@ public void Test_HowManyTransactionsBoughtMoreThan1Quantity() [Test] public void Test_HowManyTransactionsOccuredOnSundays() { - var result = ""; // TODO + var result = transactions + .Where(t => t.Date.DayOfWeek == DayOfWeek.Sunday) + .Count(); Assert.AreEqual(267, result); } @@ -106,7 +116,7 @@ public void Test_HowManyTransactionsOccuredOnSundays() [Test] public void Test_WhatIsTheAverageQuantityPurchased() { - var result = 0; // TODO + var result = transactions.Average(t => t.Quantity); Assert.AreEqual(1.5005, result, 0.0001); } @@ -114,7 +124,9 @@ public void Test_WhatIsTheAverageQuantityPurchased() [Test] public void Test_HowManyBagsOfChipsHaveBeenBought() { - var result = ""; // TODO + var result = transactions + .Where(t => t.ProductName == "Chips") + .Sum(t => t.Quantity); Assert.AreEqual(390, result); } @@ -122,7 +134,10 @@ public void Test_HowManyBagsOfChipsHaveBeenBought() [Test] public void Test_HowManyBagsOfChipsHasJasonBought() { - var result = ""; // TODO + var result = transactions + .Where(t => t.ProductName == "Chips") + .Where(t => t.UserName == "Jason") + .Sum(t => t.Quantity); Assert.AreEqual(44, result); } @@ -130,7 +145,11 @@ public void Test_HowManyBagsOfChipsHasJasonBought() [Test] public void Test_HowManyBagsOfChipsDidJasonBuyIn2015() { - var result = ""; // TODO + var result = transactions + .Where(t => t.ProductName == "Chips") + .Where(t => t.UserName == "Jason") + .Where(t => t.Date.Year == 2015) + .Sum(t => t.Quantity); Assert.AreEqual(33, result); } @@ -138,7 +157,13 @@ public void Test_HowManyBagsOfChipsDidJasonBuyIn2015() [Test] public void Test_HowManyBagsOfChipsDidJasonBuyInMay2016() { - var result = ""; // TODO + const int MAY = 5; + + var result = transactions + .Where(t => t.ProductName == "Chips") + .Where(t => t.UserName == "Jason") + .Where(t => t.Date.Year == 2016 && t.Date.Month == MAY) + .Sum(t => t.Quantity); Assert.AreEqual(2, result); } @@ -146,7 +171,16 @@ public void Test_HowManyBagsOfChipsDidJasonBuyInMay2016() [Test] public void Test_WhatProductSellsTheMostBetween12And1PM() { - var result = ""; // TODO + List productByCount = transactions + .Where(t => t.Date.Hour >= 12 && t.Date.Hour <= 13) + .Select(t => new ItemByCount(t.ProductName, transactions.Where(p => p.ProductName == t.ProductName).Sum(i => i.Quantity))) + .Distinct() + .ToList(); + + var result = productByCount + .Where(t => t.count == productByCount.Max(pc => pc.count)) + .Select(t => t.item) + .First(); Assert.AreEqual("Candy", result); } @@ -154,7 +188,15 @@ public void Test_WhatProductSellsTheMostBetween12And1PM() [Test] public void Test_WhatProductSellsTheLeast() { - var result = ""; // TODO + List productByCount = transactions + .Select(t => new ItemByCount(t.ProductName, transactions.Where(p => p.ProductName == t.ProductName).Sum(i => i.Quantity))) + .Distinct() + .ToList(); + + var result = productByCount + .Where(t => t.count == productByCount.Min(pc => pc.count)) + .Select(t => t.item) + .First(); Assert.AreEqual("Cookies", result); } @@ -162,7 +204,16 @@ public void Test_WhatProductSellsTheLeast() [Test] public void Test_WhoBoughtTheMostCandy() { - var result = ""; // TODO + List candyTransactions = transactions.Where(t => t.ProductName == "Candy").ToList(); + + var userByQuantities = candyTransactions.GroupBy(t => t.UserName, t => t.Quantity); + + List userByTotalQuantity = userByQuantities.Select(t => new ItemByCount(t.Key, t.Sum())).ToList(); + + var result = userByTotalQuantity + .Where(t => t.count == userByTotalQuantity.Max(q => q.count)) + .Select(t => t.item) + .First(); Assert.AreEqual("David", result); } @@ -170,15 +221,30 @@ public void Test_WhoBoughtTheMostCandy() [Test] public void Test_WhatIsTheTotalDollarValueOfAllTransactions() { - var result = 0; // TODO + Dictionary productPrices = products.ToDictionary(p => p.Name, p => p.Price); + + // double result = transactions.Aggregate(0.0, (total, next) => total + next.Quantity * products.Where(p => p.Name == next.ProductName).Select(p => p.Price).First()); + double result = transactions.Aggregate(0.0, (total, next) => total + next.Quantity * productPrices[next.ProductName]); Assert.AreEqual(3168.45, result, 0.001); } + private String getUserWhoSpentTheMostMoney() + { + Dictionary productPrices = products.ToDictionary(p => p.Name, p => p.Price); + var usersByPrices = transactions.GroupBy(t => t.UserName, t => t.Quantity * productPrices[t.ProductName]); + + Dictionary usersByTotalPrice = usersByPrices.ToDictionary(t => t.Key, t => t.Sum()); + + List> sorted = (from kv in usersByTotalPrice orderby kv.Value descending select kv).ToList(); + + return sorted[0].Key; + } + [Test] public void Test_WhoSpentTheMostMoney() { - var result = ""; // TODO + String result = getUserWhoSpentTheMostMoney(); Assert.AreEqual("Rod", result); } @@ -186,7 +252,12 @@ public void Test_WhoSpentTheMostMoney() [Test] public void Test_WhatIsThePasswordOfThePersonWhoSpentTheMostMoney() { - var result = ""; // TODO + String user = getUserWhoSpentTheMostMoney(); + + String result = users + .Where(u => u.Name == user) + .Select(u => u.Password) + .First(); Assert.AreEqual("optx", result); } diff --git a/problem-solving-followup/ProductByCount.cs b/problem-solving-followup/ProductByCount.cs new file mode 100644 index 0000000..5d7ecc0 --- /dev/null +++ b/problem-solving-followup/ProductByCount.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace problem_solving_followup +{ + public class ItemByCount : IEquatable + { + public string item; + public int count; + + public ItemByCount(string i, int c) + { + item = i; + count = c; + } + + public bool Equals(ItemByCount ic) + { + if (ic == null) + { + return false; + } + + return ic.item.Equals(this.item) && ic.count == this.count; + } + + public bool Equals(object o) + { + if (o == null) + { + return false; + } + + return this.Equals((ItemByCount)o); + } + + public override int GetHashCode() + { + int hashItem = item.GetHashCode(); + int hashCount = count.GetHashCode(); + + return hashItem ^ hashCount; + } + } +} \ No newline at end of file diff --git a/problem-solving-followup/problem-solving-followup.csproj b/problem-solving-followup/problem-solving-followup.csproj index d5a1d36..3512ae6 100644 --- a/problem-solving-followup/problem-solving-followup.csproj +++ b/problem-solving-followup/problem-solving-followup.csproj @@ -46,6 +46,7 @@ +