c#做图书馆管理系统
时间: 2024-01-03 13:21:48 浏览: 26
以下是一个简单的C#图书馆管理系统的示例:
```csharp
using System;
using System.Collections.Generic;
namespace LibraryManagementSystem
{
class Program
{
static void Main(string[] args)
{
Library library = new Library();
bool isRunning = true;
while (isRunning)
{
Console.WriteLine("欢迎使用图书馆管理系统!");
Console.WriteLine("1. 添加图书");
Console.WriteLine("2. 借阅图书");
Console.WriteLine("3. 归还图书");
Console.WriteLine("4. 查看图书");
Console.WriteLine("5. 退出系统");
Console.Write("请选择操作:");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.Write("请输入图书名称:");
string bookName = Console.ReadLine();
library.AddBook(bookName);
Console.WriteLine("图书添加成功!");
break;
case 2:
Console.Write("请输入要借阅的图书名称:");
string borrowBookName = Console.ReadLine();
bool borrowResult = library.BorrowBook(borrowBookName);
if (borrowResult)
{
Console.WriteLine("借阅成功!");
}
else
{
Console.WriteLine("借阅失败,该图书不存在或已被借阅!");
}
break;
case 3:
Console.Write("请输入要归还的图书名称:");
string returnBookName = Console.ReadLine();
bool returnResult = library.ReturnBook(returnBookName);
if (returnResult)
{
Console.WriteLine("归还成功!");
}
else
{
Console.WriteLine("归还失败,图书不存在或未被借阅!");
}
break;
case 4:
Console.WriteLine("图书列表:");
library.DisplayBooks();
break;
case 5:
isRunning = false;
Console.WriteLine("感谢使用图书馆管理系统,再见!");
break;
default:
Console.WriteLine("无效的选择,请重新输入!");
break;
}
Console.WriteLine();
}
}
}
class Library
{
private List<string> books;
public Library()
{
books = new List<string>();
}
public void AddBook(string bookName)
{
books.Add(bookName);
}
public bool BorrowBook(string bookName)
{
if (books.Contains(bookName))
{
books.Remove(bookName);
return true;
}
else
{
return false;
}
}
public bool ReturnBook(string bookName)
{
if (!books.Contains(bookName))
{
books.Add(bookName);
return true;
}
else
{
return false;
}
}
public void DisplayBooks()
{
foreach (string book in books)
{
Console.WriteLine(book);
}
}
}
}
```