Files
ProgLab2/Services/SalaryHistory.cs
2025-11-13 07:51:17 +04:00

77 lines
3.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using su.divan2000.SalaryApp.Models;
namespace su.divan2000.SalaryApp.Services
{
public class SalaryHistory
{
private readonly List<SalaryData> history = new();
private const int MaxHistoryCount = 6;
public void Add(SalaryData data)
{
history.Add(data);
if (history.Count > MaxHistoryCount)
history.RemoveAt(0); // храним только последние 6 расчётов
}
public void ShowHistory()
{
if (!history.Any())
{
Console.WriteLine("История расчётов пуста.");
return;
}
for (int i = 0; i < history.Count; i++)
{
Console.WriteLine($"{i + 1}. {history[i].CalculationDate:d} — {history[i].TotalSalary:F2} руб.");
}
}
public void Compare(int first, int second)
{
if (first < 1 || second < 1 || first > history.Count || second > history.Count)
{
Console.WriteLine("Некорректные индексы для сравнения.");
return;
}
var a = history[first - 1];
var b = history[second - 1];
Console.WriteLine("\n=== Сравнение расчётов ===");
Console.WriteLine($"1: {a.CalculationDate:d} — {a.TotalSalary:F2} руб.");
Console.WriteLine($"2: {b.CalculationDate:d} — {b.TotalSalary:F2} руб.");
Console.WriteLine($"Разница по окладу: {b.SalaryBase - a.SalaryBase:+0.00;-0.00} руб.");
Console.WriteLine($"Разница по премиям: {b.Bonus - a.Bonus:+0.00;-0.00} руб.");
Console.WriteLine($"Разница по штрафам: {b.Penalty - a.Penalty:+0.00;-0.00} руб.");
Console.WriteLine($"Разница по итоговой зарплате: {b.TotalSalary - a.TotalSalary:+0.00;-0.00} руб.");
double perc = ((b.TotalSalary - a.TotalSalary) / a.TotalSalary) * 100;
Console.WriteLine($"Процентная разница: {perc:+0.0;-0.0}%");
}
public void ShowStatistics()
{
if (!history.Any())
{
Console.WriteLine("История расчётов пуста.");
return;
}
double avg = history.Average(h => h.TotalSalary);
var max = history.MaxBy(h => h.TotalSalary)!;
var min = history.MinBy(h => h.TotalSalary)!;
Console.WriteLine("\n=== Статистика ===");
Console.WriteLine($"Средняя зарплата: {avg:F2} руб.");
Console.WriteLine($"Максимальная зарплата: {max.TotalSalary:F2} руб. ({max.CalculationDate:d})");
Console.WriteLine($"Минимальная зарплата: {min.TotalSalary:F2} руб. ({min.CalculationDate:d})");
}
}
}