• c# 设计一个图书管理系统


    设计一个简单的图书管理系统,包括以下类:

    1、Book 类:表示一本图书,包括图书编号、书名、作者、出版日期等属性。

    2、Library 类:表示图书馆,包括一个图书列表,可以添加图书、借出图书、归还图书等操作。

    3、Person 类:表示图书馆的读者,包括姓名、借阅的图书列表等属性。

    请用 C# 编写上述类,并提供简单的演示代码。

    解答:

    using System;
    using System.Collections.Generic;
    
    class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public DateTime PublicationDate { get; set; }
    }
    
    class Library
    {
        private List books = new List();
    
        public void AddBook(Book book)
        {
            books.Add(book);
            Console.WriteLine($"Book '{book.Title}' added to the library.");
        }
    
        public void BorrowBook(Book book, Person person)
        {
            if (books.Contains(book))
            {
                books.Remove(book);
                person.BorrowedBooks.Add(book);
                Console.WriteLine($"{person.Name} borrowed the book '{book.Title}'.");
            }
            else
            {
                Console.WriteLine($"Sorry, the book '{book.Title}' is not available in the library.");
            }
        }
    
        public void ReturnBook(Book book, Person person)
        {
            if (person.BorrowedBooks.Contains(book))
            {
                person.BorrowedBooks.Remove(book);
                books.Add(book);
                Console.WriteLine($"{person.Name} returned the book '{book.Title}'.");
            }
            else
            {
                Console.WriteLine($"{person.Name} did not borrow the book '{book.Title}'.");
            }
        }
    }
    
    class Person
    {
        public string Name { get; set; }
        public List BorrowedBooks { get; set; } = new List();
    }
    
    class Program
    {
        static void Main()
        {
            Book book1 = new Book { BookId = 1, Title = "Introduction to C#", Author = "John Smith", PublicationDate = new DateTime(2020, 1, 1) };
            Book book2 = new Book { BookId = 2, Title = "Data Structures and Algorithms", Author = "Alice Johnson", PublicationDate = new DateTime(2019, 5, 15) };
    
            Library library = new Library();
            library.AddBook(book1);
            library.AddBook(book2);
    
            Person person1 = new Person { Name = "Alice" };
            Person person2 = new Person { Name = "Bob" };
    
            library.BorrowBook(book1, person1);
            library.BorrowBook(book2, person2);
    
            library.ReturnBook(book1, person1);
            library.ReturnBook(book2, person2);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
  • 相关阅读:
    impala常用时间函数,date->string->timestamp互转
    十年前对敏捷开发的体会
    LeetCode——1455.检查单词是否为句中其他单词的前缀
    uniapp书写顶部选项卡代码详细例子
    建筑施工行业招投标资源众包分包系统站点开发
    postgresql中控制符带来的数据错觉
    python3.11版本pip install ddddocr调用时报错got an unexpected keyword argument ‘det‘ 解决
    react+canvas实现刮刮乐效果
    抢先看!中移创马大赛通信能力开放专题赛初赛评选结果新鲜出炉
    单一职责原则
  • 原文地址:https://blog.csdn.net/weixin_43160662/article/details/134497367