Last active
January 13, 2026 18:52
-
-
Save sunmeat/91af85e7fb5ff913a27ea85cb4a68bc3 to your computer and use it in GitHub Desktop.
SRP bad 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(); | |
| } | |
| public void Load() | |
| { | |
| // робота з бд | |
| Console.WriteLine("Завантаження з бази даних."); | |
| } | |
| public void Save() | |
| { | |
| Console.WriteLine("Збереження в базу даних."); | |
| } | |
| public void Print() | |
| { | |
| // формування звіту | |
| Console.WriteLine("Друк звіту:"); | |
| foreach (var item in items) | |
| { | |
| Console.WriteLine($"{item.Name}: {item.Price}"); | |
| } | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; | |
| var product1 = new Product { Name = "Книга", Price = 200m }; | |
| var product2 = new Product { Name = "Зошит", Price = 50m }; | |
| var order = new Order(); | |
| order.Load(); | |
| order.AddItem(product1); | |
| order.AddItem(product2); | |
| order.Calculate(); | |
| order.Print(); | |
| order.Save(); | |
| var items = order.GetItems(); | |
| Console.WriteLine($"Кількість елементів: {items.Length}"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment