借阅系统逻辑
本课属于 Coddy C# 学习旅程的“面向对象编程”单元。第 64 课,共 70 课。
挑战
简单通过实现核心借阅逻辑,让你的图书馆系统运转起来!你将创建一个集中式系统,协调书籍和用户之间的操作,执行借阅限额等图书馆规则,并确保只有在书籍可借时才能借阅。
你将继续使用现有的四个文件:
Book.cs:保留你的 Book 类,其中包含Id、Title、Author和IsAvailable属性,以及上一课中的Borrow()和Return()方法。User.cs:保留你的 User 类,其中包含Name、MemberId、BorrowedCount,以及用于管理已借书籍的方法。添加一个常量MaxBooks,将其设为 3,表示用户一次最多可以借阅的书籍数量。添加一个方法CanBorrow(),仅当用户已借阅的书籍少于MaxBooks时才返回 true。Library.cs:在这里实现借阅系统的逻辑。你的 Library 类应包含书籍和用户的集合,以及添加书籍和用户的方法。实现两个关键方法:BorrowBook(string bookId, int memberId):协调借阅流程。该方法应检查书籍是否存在、用户是否存在、书籍是否可借,以及用户是否还能借阅更多书籍。针对每种失败情况或成功情况返回相应的消息。ReturnBook(string bookId, int memberId):处理还书,在办理还书前确认用户确实借了这本书。
Program.cs:处理图书馆操作并显示结果。
你的 BorrowBook 方法应根据具体情况返回以下消息:
Book not found:书籍 ID 不存在时User not found:会员 ID 不存在时{Title} is not available:书籍已被借出时{Name} has reached the borrowing limit:用户已经借了 3 本书时{Name} borrowed {Title}:成功借出时
你的 ReturnBook 方法应返回:
Book not found或User not found:ID 无效时{Name} did not borrow {Title}:用户试图归还一本自己没有借过的书时{Name} returned {Title}:成功归还时
你将收到以下输入:
- 图书馆名称
- 书籍数量,接着是每本书的信息,格式为
id|title|author - 用户数量,接着是每个用户的信息,格式为
name|memberId - 操作数量,接着是每项操作的信息,格式为
action|bookId|memberId
将每项操作的结果单独打印在一行中。
例如,如果输入为:
Central Library
4
B001|The Hobbit|Tolkien
B002|1984|Orwell
B003|Dune|Herbert
B004|Foundation|Asimov
2
Alice|101
Bob|102
6
borrow|B001|101
borrow|B002|101
borrow|B003|101
borrow|B004|101
borrow|B001|102
return|B002|101输出应为:
Alice borrowed The Hobbit
Alice borrowed 1984
Alice borrowed Dune
Alice has reached the borrowing limit
The Hobbit is not available
Alice returned 1984注意,Library 类充当协调者:它会在允许操作前验证所有条件,让 Book 类和 User 类专注于各自的职责。这种职责分离能让你的代码更易于维护和测试!
亲手试试
using System;
using System.Collections.Generic;
namespace LibrarySystem
{
class Program
{
public static void Main(string[] args)
{
// 读取库名称
string libraryName = Console.ReadLine();
// 使用库名称创建 Library 实例
Library library = new Library(libraryName);
// 读取图书数量
int numBooks = Convert.ToInt32(Console.ReadLine());
// 读取并添加图书
for (int i = 0; i < numBooks; i++)
{
string bookInput = Console.ReadLine();
string[] bookParts = bookInput.Split('|');
string bookId = bookParts[0];
string bookTitle = bookParts[1];
string bookAuthor = bookParts[2];
Book book = new Book(bookId, bookTitle, bookAuthor);
library.AddBook(book);
}
// 读取用户数量
int numUsers = Convert.ToInt32(Console.ReadLine());
// 读取并注册用户
for (int i = 0; i < numUsers; i++)
{
string userInput = Console.ReadLine();
string[] userParts = userInput.Split('|');
string userName = userParts[0];
int memberId = Convert.ToInt32(userParts[1]);
User user = new User(userName, memberId);
library.RegisterUser(user);
}
// 读取操作数量
int numOperations = Convert.ToInt32(Console.ReadLine());
// 处理操作
for (int i = 0; i < numOperations; i++)
{
string operationInput = Console.ReadLine();
string[] opParts = operationInput.Split('|');
string action = opParts[0];
string bookId = opParts[1];
int memberId = Convert.ToInt32(opParts[2]);
if (action == "borrow")
{
string result = library.BorrowBook(bookId, memberId);
Console.WriteLine(result);
}
else if (action == "return")
{
string result = library.ReturnBook(bookId, memberId);
Console.WriteLine(result);
}
}
}
}
}
面向对象编程中的所有课
自己练习: C# 在线编译器