From 06fe0ff8934aa0fbf64168ae380d1300a09a315d Mon Sep 17 00:00:00 2001 From: cwolfes Date: Mon, 17 Nov 2025 08:15:32 +0100 Subject: [PATCH] refactoring report service - fixes typos ans imports - improves handling of exchange and data loading - implements formatted output - adds basic error handling --- SalesReporting/ReportService.cs | 96 ++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 30 deletions(-) diff --git a/SalesReporting/ReportService.cs b/SalesReporting/ReportService.cs index 17d5292..07c0143 100644 --- a/SalesReporting/ReportService.cs +++ b/SalesReporting/ReportService.cs @@ -1,45 +1,81 @@ -using Systen; -using -System.Collections.Generic; -System.IO; -System.Linq; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Text.Json; +using System.Threading.Tasks; namespace SalesReporting { + /// + /// Generates a simple sales report for a given year and month. + /// public class ReportService { - public void CreateReportForYearAndMonth(int year, int month) - { + // Exchange‑rates table + private static readonly IReadOnlyDictionary ExchangeRates + = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + { "USD", 1.0 }, // base currency + { "EUR", 1.1 }, + { "GBP", 1.3 } + }; + + // Public API - var allSales = JsonSerializer.Deserialize>(File.ReadAllText("sales.json")); - var filtered = allSales.Where(s => s.Date.Year == year && s.Date.Month == month).ToList(); - double total = 0; - foreach (var sale in filtered) + /// + /// Reads *sales.json*, filters by year/month, aggregates the totals, + /// and writes a human‑readable report to a file. + /// + public async Task CreateReportForYearAndMonthAsync(int year, int month) { - if (sale.Currency == "USD") - total += sale.Amount; - else if (sale.Currency == "EUR") - { - total += sale.Amount * 1.1; - } - else if (sale.Currency == "GBP") + var allSales = await LoadSalesAsync("sales.json"); + var filtered = allSales + .Where(s => s.Date.Year == year && s.Date.Month == month) + .ToList(); + + double totalUsd = filtered.Sum(s => s.Amount * GetExchangeRate(s.Currency)); + + var reportLines = new[] { - total += sale.Amount * 1.3; - } + $"Monatlicher Verkaufsbericht ({month:D2}/{year})", + new string('-', 40), + $"Gesamt Umsatz in USD: {totalUsd:N2}" + }; + + var fileName = $"report_{year}_{month:D2}.txt"; + await File.WriteAllLinesAsync(fileName, reportLines); + + Console.WriteLine($"Report generated successfully: {fileName}"); } - var reportRows = new List + + + /// + /// Looks up the exchange rate for a given currency. + /// If the currency is unknown, it defaults to 1.0 (USD). + /// + private static double GetExchangeRate(string currency) + => ExchangeRates.TryGetValue(currency, out var rate) ? rate : 1.0; + + /// + /// Reads the JSON file and returns a . + /// Throws if deserialization fails. + /// + private static async Task> LoadSalesAsync(string path) { - $"Monatlicher Verkaufsbericht ({month}/{year})", - $"-------------------------------", - $"Gesamt Umsatz in USD: {total}" + var json = await File.ReadAllTextAsync(path); + return JsonSerializer.Deserialize>(json) + ?? throw new InvalidOperationException($"Unable to deserialize {path}."); } - File.WriteAllLines($"report_{year}_{month}.txt", reportRows); - Console.WriteLine("Report generated successfully."); } - public class SaleObject + + /// + /// Model representing a single sale record. + /// + public sealed class SaleObject { - public DateTime Date { get; set; } - public double Amount { get; set; } - public string Currency { get; set; } + public DateTime Date { get; set; } + public double Amount { get; set; } + public string Currency { get; set; } = string.Empty; + } } \ No newline at end of file