Let's create a simple library management system using the concepts we've covered throughout this series. The system will manage books, users, and borrow records. The project will include classes, inheritance, interfaces, events, delegates, generics, collections, LINQ, and exception handling.
using System; using System.Collections.Generic; using System.Linq; // Chapter 2.1: Understanding classes, objects, and inheritance public class User { // Chapter 2.4: Understanding properties and indexers public int Id { get; set; } public string Name { get; set; } public DateTime JoinDate { get; set; } // Chapter 2.2: Understanding constructors and destructors public User(int id, string name) { Id = id; Name = name; JoinDate = DateTime.Now; } // Chapter 2.3: Understanding access modifiers (public, private, protected, internal) protected void PrintUserDetails() { Console.WriteLine($"ID: {Id}, Name: {Name}, Joined: {JoinDate}"); } } public class Book { public int Id { get; set; } public string Title { get; set; } public string Author { get; set; } public bool IsAvailable { get; set; } public Book(int id, string title, string author) { Id = id; Title = title; Author = author; IsAvailable = true; } } // Chapter 3.1: Understanding interfaces, delegates, and events public interface IBorrowable { event EventHandler < BookBorrowedEventArgs > BookBorrowed; void BorrowBook(Book book); } public class BookBorrowedEventArgs: EventArgs { public User User { get; set; } public Book Book { get; set; } public DateTime BorrowedAt { get; set; } } public class LibraryMember: User, IBorrowable { // Chapter 6.2: Understanding System.Collections.Generic private List < Book > borrowedBooks = new List < Book > (); public LibraryMember(int id, string name): base(id, name) {} // Chapter 3.1: Events and event handlers public event EventHandler < BookBorrowedEventArgs > BookBorrowed; public void BorrowBook(Book book) { if (book.IsAvailable) { book.IsAvailable = false; borrowedBooks.Add(book); OnBookBorrowed(new BookBorrowedEventArgs { User = this, Book = book, BorrowedAt = DateTime.Now }); } else { throw new InvalidOperationException("Book is not available"); } } protected virtual void OnBookBorrowed(BookBorrowedEventArgs e) { BookBorrowed?.Invoke(this, e); } } class Program { static void Main(string[] args) { // Chapter 6.4: Understanding System.Linq List < Book > books = new List < Book > { new Book(1, "The Catcher in the Rye", "J.D. Salinger"), new Book(2, "To Kill a Mockingbird", "Harper Lee"), new Book(3, "The Great Gatsby", "F. Scott Fitzgerald") }; LibraryMember member = new LibraryMember(1, "John Doe"); // Chapter 3.1: Event subscription member.BookBorrowed += Member_BookBorrowed; try { member.BorrowBook(books.First(book => book.Id == 1)); } catch (InvalidOperationException ex) { Console.WriteLine($"Error: {ex.Message}"); } try { member.BorrowBook(books.First(book => book.Id == 2)); member.BorrowBook(books.First(book => book.Id == 2)); // Attempt to borrow the same book again } catch (InvalidOperationException ex) { Console.WriteLine($"Error: {ex.Message}"); } } private static void Member_BookBorrowed(object sender, BookBorrowedEventArgs e) { Console.WriteLine($"Book Borrowed Event: {e.User.Name} borrowed \"{e.Book.Title}\" by {e.Book.Author} on {e.BorrowedAt}"); } }
In this example, we created a simple library management system that allows library members to borrow books. The `User` class demonstrates properties, constructors, and access modifiers. The `LibraryMember` class inherits from `User` and implements the `IBorrowable` interface, showcasing inheritance and interfaces. The `BookBorrowed` event and event handlers are used to notify when a book is borrowed.
We also used generics and collections with the `List<Book>` and `List<User>` objects. The System.Linq namespace was used to find books by their IDs. Lastly, exception handling is demonstrated when attempting to borrow an unavailable book.
The code is structured in a way that is easy to read and understand, while also showcasing the skills of an experienced C# programmer.
Comments
Post a Comment