// Return total cost of pack list. static decimal TotalCost(Packs requiredPacks) { decimal totalCost = 0; foreach (Pack pack in requiredPacks.PackList) { totalCost += pack.Cost; } return(totalCost); }
// Packs calculation static Packs MinimumPacksCalculation(List <Packs> tryPacksList, List <Pack> packsWithCode, int requiredQty) { while (tryPacksList.Count > 0) { List <Packs> toRemove = new List <Packs>(); // Try all pack combination from list foreach (Packs tryPacks in tryPacksList) { if (tryPacks.TotalSize == requiredQty) { return(tryPacks); } else if (tryPacks.TotalSize > requiredQty) { toRemove.Add(tryPacks); } } // Remove impossible combination from list foreach (Packs tryPacks in toRemove) { tryPacksList.Remove(tryPacks); } if (tryPacksList.Count < 1) { break; } // Add each type of pack to each pack list List <Packs> newTryPacksList = new List <Packs>(); foreach (Pack pack in packsWithCode) { foreach (Packs tryPacks in tryPacksList) { Packs newTryPacks = new Packs(); newTryPacks.TotalSize = tryPacks.TotalSize + pack.Size; List <Pack> newPackList = new List <Pack>(); foreach (Pack oldPack in tryPacks.PackList) { newPackList.Add(oldPack); } newPackList.Add(pack); newTryPacks.PackList = newPackList; newTryPacksList.Add(newTryPacks); } } tryPacksList = newTryPacksList; } return(null); }
static int RunOutputTest(string expectedOutput, Packs requiredPacks, string productCode, string testName) { string output = Program.CostPackBreakdown(requiredPacks, productCode); if (output.Equals(expectedOutput)) { Console.WriteLine(testName + ": Test Success"); return(1); } else { Console.WriteLine(testName + ": Test Fail"); Console.WriteLine("Expected:"); Console.WriteLine(expectedOutput); Console.WriteLine("Actual:"); Console.WriteLine(output); return(0); } }
static int RunPackTest(int qty, List <Pack> packList, string testName, List <int> expectedSizes) { try { Packs requiredPacks = Program.MinimumRequiredPacks(qty, packList); if (expectedSizes == null) { if (requiredPacks != null) { throw new Exception("Result is not null."); } } else { foreach (Pack pack in requiredPacks.PackList) { if (!expectedSizes.Remove(pack.Size)) { throw new Exception("Unexpected size returned."); } } if (expectedSizes.Count > 0) { throw new Exception("Incorrect number of packs."); } } Console.WriteLine(testName + ": Test Success"); return(1); } catch (Exception e) { Console.WriteLine(testName + ": Test Fail - " + e.Message); return(0); } }
// Breakdown of products inside the cart public static string CostPackBreakdown(Packs requiredPacks, string productCode) { decimal totalCost = TotalCost(requiredPacks); StringBuilder output = new StringBuilder(); output.Append(requiredPacks.TotalSize + " "); output.Append(productCode + " $" + totalCost); // Sort size by descending order requiredPacks.PackList.Sort( (x, y) => - 1 * x.Size.CompareTo(y.Size)); // Generate line of pack type and quantity int prevSize = 0; int packQty = 0; decimal prevCost = 0; foreach (Pack pack in requiredPacks.PackList) { if (prevSize != pack.Size && prevSize != 0) { output.Append(Environment.NewLine + " "); output.Append(packQty + " x "); output.Append(prevSize + " $" + prevCost); packQty = 1; // Current pack is different size } else { packQty += 1; } prevSize = pack.Size; prevCost = pack.Cost; } output.Append(Environment.NewLine + " "); output.Append(packQty + " x "); output.Append(prevSize + " $" + prevCost); return(output.ToString()); }
// Identify minimum required packs for specified quantity public static Packs MinimumRequiredPacks(int requiredQty, List <Pack> packsWithCode) { List <Packs> tryPacksList = new List <Packs>(); Packs tryPacks = new Packs(); foreach (Pack pack in packsWithCode) { tryPacks.TotalSize = pack.Size; List <Pack> newPackList = new List <Pack>(); newPackList.Add(pack); tryPacks.PackList = newPackList; tryPacksList.Add(tryPacks); tryPacks = new Packs(); } tryPacks = MinimumPacksCalculation(tryPacksList, packsWithCode, requiredQty); return(tryPacks); }
public static void RunTests() { int numSuccess = 0; int numTests = 0; List <Pack> packSH3 = new List <Pack>(); packSH3.Add(new Pack("Sliced Ham", "SH3", "3", "2.99")); packSH3.Add(new Pack("Sliced Ham", "SH3", "5", "4.49")); List <Pack> packYT2 = new List <Pack>(); packYT2.Add(new Pack("Yoghurt", "YT2", "4", "4.95")); packYT2.Add(new Pack("Yoghurt", "YT2", "10", "9.95")); packYT2.Add(new Pack("Yoghurt", "YT2", "15", "13.95")); List <Pack> packTR = new List <Pack>(); packTR.Add(new Pack("Toilet Rolls", "TR", "3", "2.95")); packTR.Add(new Pack("Toilet Rolls", "TR", "5", "4.45")); packTR.Add(new Pack("Toilet Rolls", "TR", "9", "7.99")); Console.WriteLine("Test pack breakdown"); numSuccess += RunPackTest(30, packYT2, "30 YT2 (2 packs)", new List <int> { 15, 15 }); numTests += 1; numSuccess += RunPackTest(28, packYT2, "28 YT2 (4 packs)", new List <int> { 10, 10, 4, 4 }); numTests += 1; numSuccess += RunPackTest(13, packYT2, "13 YT2 (0 packs)", null); numTests += 1; Console.WriteLine(); Console.WriteLine("Test final output"); string expectedOutput = "10 SH3 $8.98" + Environment.NewLine; expectedOutput += " 2 x 5 $4.49"; Packs requiredPacks = Program.MinimumRequiredPacks(10, packSH3); numSuccess += RunOutputTest(expectedOutput, requiredPacks, "SH3", "10 SH3"); numTests += 1; expectedOutput = "28 YT2 $29.80" + Environment.NewLine; expectedOutput += " 2 x 10 $9.95" + Environment.NewLine; expectedOutput += " 2 x 4 $4.95"; requiredPacks = Program.MinimumRequiredPacks(28, packYT2); numSuccess += RunOutputTest(expectedOutput, requiredPacks, "YT2", "28 YT2"); numTests += 1; expectedOutput = "12 TR $10.94" + Environment.NewLine; expectedOutput += " 1 x 9 $7.99" + Environment.NewLine; expectedOutput += " 1 x 3 $2.95"; requiredPacks = Program.MinimumRequiredPacks(12, packTR); numSuccess += RunOutputTest(expectedOutput, requiredPacks, "TR", "12 TR "); numTests += 1; Console.WriteLine(); Console.WriteLine(numSuccess + "/" + numTests + " successful"); }
static void Main(string[] args) { // Read the packs.txt file Dictionary <string, List <Pack> > packCodeDict = new Dictionary <string, List <Pack> >(); try { packCodeDict = GetPackData(); } catch (Exception e) { PressEnterToExit("Error reading Packs file: " + e.Message); } Console.WriteLine("************Online Grocery Store************"); Console.WriteLine("Name Code Packs\n" + "Sliced Ham SH3 3 @ $2.99, 5 @ $4.49\n" + "Yoghurt YT2 4 @ $4.95, 10 @ $9.95, 15 @ $13.95\n" + "Toilet Rolls TR 3 @ $2.95, 5 @ $4.45, 9 @ $ 7.99\n"); Console.WriteLine("Cart:"); string output = ""; // Read cart and display output breakdown while (1 == 1) { string input = Console.ReadLine(); // Press enter to display total price if (input == "") { FullCart(output); output = ""; continue; } string message = ValidateCartInput(input, packCodeDict); if (message != "") { FullCart("Error: " + message); output = ""; continue; } string[] inputCode = input.Split(' '); int qty = Int32.Parse(inputCode[0]); string code = inputCode[1]; Packs requiredPacks = MinimumRequiredPacks(qty, packCodeDict[code]); if (requiredPacks == null) { FullCart("Error: Input Valid Quantity"); output = ""; continue; } else { output += CostPackBreakdown(requiredPacks, code); output += Environment.NewLine; } } }