fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BookExample
  5. {
  6.  
  7. public class Book
  8. {
  9.  
  10. public string Title { get; private set; }
  11. public string Author { get; private set; }
  12. public decimal Price { get; private set; }
  13.  
  14.  
  15. public Book(string title, string author, decimal price)
  16. {
  17.  
  18. Title = title;
  19. Author = author;
  20. Price = price;
  21. }
  22.  
  23.  
  24. public void DisplayDetails()
  25. {
  26. Console.WriteLine($"Title: {Title}");
  27. Console.WriteLine($"Author: {Author}");
  28. Console.WriteLine($"Price: ${Price:F2}");
  29. }
  30. }
  31.  
  32.  
  33. public class Librarian
  34. {
  35.  
  36. private List<Book> _books;
  37.  
  38.  
  39. public Librarian()
  40. {
  41. _books = new List<Book>();
  42. }
  43.  
  44.  
  45. public void AddBook(Book book)
  46. {
  47. _books.Add(book);
  48. Console.WriteLine($"Added book: {book.Title}");
  49. }
  50.  
  51.  
  52. public void DisplayAllBooks()
  53. {
  54. if (_books.Count == 0)
  55. {
  56. Console.WriteLine("No books in the library.");
  57. return;
  58. }
  59.  
  60. Console.WriteLine("Books in the library:");
  61. foreach (var book in _books)
  62. {
  63. book.DisplayDetails();
  64. Console.WriteLine();
  65. }
  66. }
  67. }
  68. // we should use the Dip principe here by adding interface item instead of make the librian deal with just books but its just a simple program to demonstrate the SRP
  69.  
  70. class Program
  71. {
  72. static void Main(string[] args)
  73. {
  74.  
  75. Librarian librarian = new Librarian();
  76.  
  77.  
  78. Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 12.99m);
  79. Book book2 = new Book("1984", "George Orwell", 9.99m);
  80. Book book3 = new Book("To Kill a Mockingbird", "Harper Lee", 10.99m);
  81.  
  82.  
  83. librarian.AddBook(book1);
  84. librarian.AddBook(book2);
  85. librarian.AddBook(book3);
  86.  
  87.  
  88. librarian.DisplayAllBooks();
  89. }
  90. }
  91. }
Success #stdin #stdout 0.06s 29064KB
stdin
Standard input is empty
stdout
Added book: The Great Gatsby
Added book: 1984
Added book: To Kill a Mockingbird
Books in the library:
Title: The Great Gatsby
Author: F. Scott Fitzgerald
Price: $12.99

Title: 1984
Author: George Orwell
Price: $9.99

Title: To Kill a Mockingbird
Author: Harper Lee
Price: $10.99