图书和用户模型
本课属于 Coddy C# 学习旅程的“面向对象编程”单元。第 63 课,共 70 课。
挑战
简单让我们通过为 Book 和 User 模型添加更丰富的功能来扩展你的 Library System。你将为图书添加状态跟踪功能,为用户添加借阅功能,并使用适当的封装来保护数据。
你将继续使用由 4 个文件组成的结构:
Book.cs:为 Book 类添加唯一标识符和状态跟踪功能。每本书都应有Id(字符串)、Title(字符串)、Author(字符串)和IsAvailable(bool,默认值为 true)。添加Borrow()和Return()方法,用于更改可借阅状态并返回表示成功或失败的消息。如果有人尝试借阅不可借阅的书,返回"{Title} is not available"。如果有人尝试归还一本已经可借阅的书,返回"{Title} was not borrowed"。操作成功时,应返回"{Title} has been borrowed"或"{Title} has been returned"。User.cs:扩展 User 类,以跟踪借阅的图书。添加Name(字符串)、MemberId(int),以及一个用于存储借阅图书 ID 的私有列表。添加只读属性BorrowedCount,用于返回用户当前借阅的图书数量。添加AddBorrowedBook(string bookId)和RemoveBorrowedBook(string bookId)方法,以管理用户借阅的图书。还要添加HasBook(string bookId)方法,如果用户借阅了指定的图书,该方法就返回 true。Library.cs:Library 类应维护图书和用户的集合。添加AddBook(Book book)和RegisterUser(User user)方法,用于向图书馆添加图书和用户。添加GetBookById(string id)方法,用于返回具有该 ID 的图书(如果未找到,则返回 null),并添加GetUserById(int memberId)方法,用类似的方式查找用户。Program.cs:根据输入处理图书馆操作,将所有内容整合起来。
你将收到以下输入:
- 图书馆名称
- 要添加的图书数量
- 每本书的信息:
id|title|author - 要注册的用户数量
- 每位用户的信息:
name|memberId - 要执行的操作数量
- 每项操作的信息:
action|bookId|memberId,其中 action 为borrow或return
对于每项操作,打印借阅或归还操作的结果。所有操作完成后,为每位用户打印一条摘要,显示其姓名和当前借阅的图书数量,格式为 {Name}: {BorrowedCount} book(s)。
例如,如果输入如下:
Downtown Library
2
B001|Clean Code|Robert Martin
B002|Design Patterns|Gang of Four
2
Alice|101
Bob|102
4
borrow|B001|101
borrow|B002|101
borrow|B001|102
return|B001|101输出应为:
Clean Code has been borrowed
Design Patterns has been borrowed
Clean Code is not available
Clean Code has been returned
Alice: 1 book(s)
Bob: 0 book(s)注意,Alice 成功借阅了两本书,但 Bob 尝试借阅 Clean Code 时失败了,因为 Alice 已经借走了这本书。Alice 归还 Clean Code 后,她的借阅数量降到了 1。这展示了你的模型如何协同工作来落实图书馆规则!
亲手试试
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());
// 读取并注册用户
List<User> usersList = new List<User>();
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);
usersList.Add(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]);
Book book = library.GetBookById(bookId);
User user = library.GetUserById(memberId);
if (action == "borrow")
{
string result = book.Borrow();
Console.WriteLine(result);
if (result.Contains("has been borrowed"))
{
user.AddBorrowedBook(bookId);
}
}
else if (action == "return")
{
string result = book.Return();
Console.WriteLine(result);
if (result.Contains("has been returned"))
{
user.RemoveBorrowedBook(bookId);
}
}
}
// 输出每个用户的汇总信息
foreach (User user in usersList)
{
Console.WriteLine($"{user.Name}: {user.BorrowedCount} book(s)");
}
}
}
}面向对象编程中的所有课
自己练习: C# 在线编译器