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