public static void Run(CommandLineArgs args) { if (args.InputFiles?.Any() != true) { Console.WriteLine(" ERROR: No input file specified."); return; } if (File.Exists(args.OutputFile)) { if (args.Quiet) { Console.WriteLine(" ERROR: Output file already exists."); return; } Console.WriteLine("Output file already exists. Overwrite (y/n)?"); if (Console.ReadLine().TrimStart().ToLower() != "y") { return; } } var changeTabulator = new ChangeTabulator( new Domain.ChangeTabulationSrategies.RandomTabulationStrategy(), new Domain.ChangeTabulationSrategies.BigEndianTabulationStrategy()); using var sw = string.IsNullOrEmpty(args.OutputFile) ? null : new StreamWriter(args.OutputFile); foreach (var inputFile in args.InputFiles) { if (!File.Exists(inputFile)) { if (!args.Quiet) { Console.WriteLine($" WARNING: Input file \"{inputFile}\" not found, skipping."); } continue; } if (!args.Quiet) { Console.WriteLine("----------------------------------------------"); Console.WriteLine($"- {Path.GetFullPath(inputFile)}"); Console.WriteLine("----------------------------------------------"); } using var sr = new StreamReader(inputFile); using var cr = new CsvHelper.CsvReader(sr, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture) { //BadDataFound = a => //{ // var line = string.IsNullOrWhiteSpace(a.RawRecord) ? "" : $"# {a.RawRecord}"; // sw?.WriteLine(line); // if (!args.Quiet) // Console.WriteLine(line); //}, HasHeaderRecord = false, HeaderValidated = null, IgnoreBlankLines = false, ShouldSkipRecord = a => { if (a.Record.Length != 2 || !decimal.TryParse(a.Record[0], out _) || !decimal.TryParse(a.Record[1], out _)) { var line = string.Join(",", a.Record); sw?.WriteLine(line); if (!args.Quiet) { Console.WriteLine(line); } return(true); } return(false); } }); while (cr.Read()) { var input = cr.GetRecord <InputRecord>(); var result = $"Amount owed: {input.AmountOwed:C} Amount tendered: {input.AmountTendered:C}" + ( input.AmountOwed < 0 ? "ERROR: Cannot owe negative money." : input.AmountTendered < 0 ? "ERROR: Cannot tender negative money." : input.AmountTendered < input.AmountOwed ? "ERROR: Need more money." : input.AmountOwed == input.AmountTendered ? "No change owed." : ($" Change due: {input.AmountTendered - input.AmountOwed:C}{IsDivisibleByThree(input.AmountOwed, input.AmountTendered)} => " + changeTabulator.TabulateChange(input.AmountOwed, input.AmountTendered) .PrettyPrint())); sw?.WriteLine(result); if (!args.Quiet) { Console.WriteLine(result); } } } }
public void GIVEN_an_amount_due_less_than_zero_WHEN_TabulateChange_is_called_THEN_an_exception_should_be_thrown() { Assert.Catch <ArgumentException>(() => tabulator.TabulateChange(-1, 10)); }