用C#编写一个图书管理系统
时间: 2023-09-24 07:13:42 浏览: 48
好的,下面是一个简单的图书管理系统的C#代码示例:
```csharp
using System;
using System.Collections.Generic;
namespace BookManagementSystem
{
class Program
{
static void Main(string[] args)
{
List<Book> books = new List<Book>();
while (true)
{
Console.WriteLine("1. Add Book");
Console.WriteLine("2. Remove Book");
Console.WriteLine("3. List Books");
Console.WriteLine("4. Exit");
Console.Write("Enter your choice: ");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.Write("Enter book title: ");
string title = Console.ReadLine();
Console.Write("Enter author name: ");
string author = Console.ReadLine();
Console.Write("Enter publication year: ");
int year = int.Parse(Console.ReadLine());
books.Add(new Book(title, author, year));
break;
case 2:
Console.Write("Enter book title to remove: ");
string titleToRemove = Console.ReadLine();
books.RemoveAll(book => book.Title == titleToRemove);
break;
case 3:
Console.WriteLine("Title\tAuthor\tPublication Year");
foreach (Book book in books)
{
Console.WriteLine($"{book.Title}\t{book.Author}\t{book.PublicationYear}");
}
break;
case 4:
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid choice.");
break;
}
}
}
}
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public int PublicationYear { get; set; }
public Book(string title, string author, int year)
{
Title = title;
Author = author;
PublicationYear = year;
}
}
}
```
这个图书管理系统使用了一个 `Book` 类来存储每本书的信息,并使用 `List<Book>` 来保存所有书籍。主程序循环显示菜单,等待用户输入选择,然后根据选择执行相应的操作。用户可以添加、删除和列出所有书籍。