那你用c#语言写一个图书管理系统出来
时间: 2023-11-14 17:04:21 浏览: 65
好的,我可以为您编写一个简单的图书管理系统。以下是一个基本的图书管理系统,它包括添加、删除和查找图书的功能:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public int Year { get; set; }
}
class Library
{
private List<Book> books = new List<Book>();
public void AddBook(Book book)
{
books.Add(book);
}
public void RemoveBook(string title)
{
Book bookToRemove = books.FirstOrDefault(b => b.Title == title);
if (bookToRemove != null)
{
books.Remove(bookToRemove);
Console.WriteLine("Book removed successfully.");
}
else
{
Console.WriteLine("Book not found.");
}
}
public void FindBook(string title)
{
Book bookToFind = books.FirstOrDefault(b => b.Title == title);
if (bookToFind != null)
{
Console.WriteLine($"Title: {bookToFind.Title}");
Console.WriteLine($"Author: {bookToFind.Author}");
Console.WriteLine($"Year: {bookToFind.Year}");
}
else
{
Console.WriteLine("Book not found.");
}
}
}
class Program
{
static void Main(string[] args)
{
Library library = new Library();
// Add books to the library
library.AddBook(new Book { Title = "The Great Gatsby", Author = "F. Scott Fitzgerald", Year = 1925 });
library.AddBook(new Book { Title = "To Kill a Mockingbird", Author = "Harper Lee", Year = 1960 });
library.AddBook(new Book { Title = "1984", Author = "George Orwell", Year = 1949 });
// Remove a book from the library
library.RemoveBook("To Kill a Mockingbird");
// Find a book in the library
library.FindBook("1984");
}
}
```
这个简单的图书管理系统可以帮助您了解如何在 C# 中创建一个基本的类和方法,并使用列表来存储和管理数据。当然,如果你需要更完整的图书管理系统,需要更多的功能,比如借阅还书,读者管理等等,需要更加详细的需求分析和设计。
阅读全文