Last active
January 13, 2026 18:56
-
-
Save sunmeat/c7f5f6e5e041b5f4e5e4dcc0e24a8985 to your computer and use it in GitHub Desktop.
SRP nice example C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| class Product | |
| { | |
| public string? Name { get; set; } | |
| public decimal Price { get; set; } | |
| } | |
| class Order | |
| { | |
| private List<Product> items = new List<Product>(); | |
| // робота з замовленням | |
| public void Calculate() | |
| { | |
| decimal total = 0; | |
| foreach (var item in items) | |
| { | |
| total += item.Price; | |
| } | |
| Console.WriteLine($"Загальна сума: {total}"); | |
| } | |
| public void AddItem(Product product) | |
| { | |
| items.Add(product); | |
| Console.WriteLine("Елемент додано!"); | |
| } | |
| public Product[] GetItems() | |
| { | |
| return items.ToArray(); | |
| } | |
| } | |
| // окремий клас для роботи з БД | |
| class OrderRepository | |
| { | |
| // робота з бд | |
| public Order Load(int orderId) | |
| { | |
| Console.WriteLine($"Завантаження замовлення з ID {orderId} з бази даних."); | |
| var order = new Order(); | |
| // симуляція завантажених даних | |
| if (orderId == 1) | |
| { | |
| var product = new Product { Name = "Книга", Price = 200m }; | |
| order.AddItem(product); | |
| } | |
| return order; | |
| } | |
| public void Save(Order order) | |
| { | |
| Console.WriteLine("Збереження замовлення в базу даних."); | |
| } | |
| } | |
| // окремий клас для роздруківки | |
| class PrintManager | |
| { | |
| // формування звітів | |
| public void Print(Order order) | |
| { | |
| Console.WriteLine("Друк звіту:"); | |
| foreach (var item in order.GetItems()) | |
| { | |
| Console.WriteLine($"{item.Name}: {item.Price}"); | |
| } | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; | |
| var repository = new OrderRepository(); | |
| var printManager = new PrintManager(); | |
| var order = repository.Load(1); | |
| var newProduct = new Product { Name = "Зошит", Price = 50m }; | |
| order.AddItem(newProduct); | |
| order.Calculate(); | |
| printManager.Print(order); | |
| repository.Save(order); | |
| var items = order.GetItems(); | |
| Console.WriteLine($"Кількість елементів: {items.Length}"); | |
| } | |
| } | |
| // ======================================================================================================== | |
| // ПРАКТИКА: використовуючи принцип SRP, розбити клас Student на 5+ дрібніших типів (не забуваючи про зв'язки між класами) | |
| class Student | |
| { | |
| public string? FirstName { get; set; } | |
| public string? Surname { get; set; } | |
| public string? Lastname { get; set; } | |
| public string? Country { get; set; } | |
| public string? Region { get; set; } | |
| public string? City { get; set; } | |
| public string? Street { get; set; } | |
| public int HouseNumber { get; set; } | |
| public char Korpus { get; set; } | |
| public short PostalCode { get; set; } | |
| public int BirthDay { get; set; } | |
| public int BirthMonth { get; set; } | |
| public int BirthYear { get; set; } | |
| public string? ZnakZodiaka { get; set; } | |
| public int StartDay { get; set; } | |
| public int StartMonth { get; set; } | |
| public int StartYear { get; set; } | |
| public int Kurs { get; set; } | |
| public string? GroupName { get; set; } | |
| public string? Specialization { get; set; } | |
| public int StudentsCount { get; set; } | |
| public int LessonsVisited { get; set; } | |
| public int LessonsLate { get; set; } | |
| public string? TeacherName { get; set; } | |
| public string? SubjectName { get; set; } | |
| public int[]? DzRates { get; set; } | |
| public float DzAverageRate { get; set; } | |
| public int[]? PracticeRates { get; set; } | |
| public float PracticeAverageRate { get; set; } | |
| public int[]? ExamRates { get; set; } | |
| public float ExamAverageRate { get; set; } | |
| public int[]? ZachetRates { get; set; } | |
| public int ZachetCount { get; set; } | |
| public float ZachetAverageRate { get; set; } | |
| public double TotalAverageRate { get; set; } | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment