我有一个具有Serve()
方法的库类。它的作用是将一个人从队列中排队并从堆栈中弹出一本书。借用Book
时Borrower
与ReturnBook()
相关联,方法INPUT
Lineup("Joker")
Add("Shelter", "Yung Jun", "9781250075611")
Add("The Paper Menagerie and Other Stories", "Liu, Ken", "9781481442541")
Serve()
Joker lined up.
Added Shelter (Yung Jun) to the stack.
Added The Paper Menagerie and Other Stories (Liu, Ken) to the stack.
Joker borrowed Shelter (Yung Jun)
可以接受与借书人员姓名相对应的字符串值。
如何在Book和借款人之间建立关联?这是我第一次遇到C#中的关联。这是一个输入和输出示例
ReturnSearchQuery
答案 0 :(得分:1)
执行此操作的一种方法是创建一个项目的属性,该项目属于另一个项目的类型。例如,Borrower
可能具有Book
属性。然后,当借款人借书时,您只需将Book
属性的值设置为他们刚借的书。
以下是一个如何运作的说明性示例:
首先,您应该创建一些类来表示对象。这是一个例子:
class Reader
{
public string Name { get; set; }
public Book BorrowedBook { get; set; }
}
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
}
看起来您有Queue
Readers
(称为borrowers
)和Stack
Books
。我还创建了一个名为Queue
的{{1}} Readers
,它代表了想要归还图书的人:
returners
现在,看起来你有一些辅助方法可以将读者添加到队列中,或者将书籍添加到堆栈中。因为我可以看到这些可能需要用于添加新项目或现有项目(例如当读者返回书籍时,现有书籍将被添加回库中),我创建了两个版本 - 一个没有参数(然后将从用户获取对象的详细信息),并使用参数(将该对象添加到队列或堆栈):
class Program
{
private static Queue<Reader> borrowers = new Queue<Reader>();
private static Queue<Reader> returners = new Queue<Reader>();
private static Stack<Book> books = new Stack<Book>();
最后,我们需要一些方法来服务我们的借款人(private static void Lineup()
{
var newReader = new Reader();
Console.Write("Enter the name of the new reader: ");
newReader.Name = Console.ReadLine();
// Now that we have a reader object, call the
// other version of this method to add it
Lineup(newReader);
}
private static void Lineup(Reader borrower)
{
borrowers.Enqueue(borrower);
Console.WriteLine($"{borrower.Name} lined up to borrow a book.");
}
private static void Add()
{
var newBook = new Book();
Console.Write("Enter the book title: ");
newBook.Title = Console.ReadLine();
Console.Write("Enter the book author: ");
newBook.Author = Console.ReadLine();
Console.Write("Enter the book ISBN: ");
newBook.ISBN = Console.ReadLine();
// Now that we have a book object, call the
// other version of this method to add it
Add(newBook);
}
private static void Add(Book book)
{
books.Push(book);
Console.WriteLine($"Added '{book.Title}' to the library.");
}
借款人和Dequeue
一本书,然后将该书交给借款人)和我们的退货人(Pop
本书到图书馆,如果这个人想借另一个,Push
他们回到借款人行中):
Enqueue
现在,我们只需要给我们的用户一些关于他们希望程序做什么的选项,并继续循环他们的输入,直到他们决定退出:
private static void ServiceBorrower()
{
if (borrowers.Count == 0)
{
Console.WriteLine($"There are no more borrowers waiting in line.");
}
else if (books.Count == 0)
{
Console.WriteLine($"There are no more books to loan.");
if (returners.Count > 0)
{
Console.WriteLine(" - Hint: There are people waiting to return books.");
}
}
else
{
var borrower = borrowers.Dequeue();
var book = books.Pop();
borrower.BorrowedBook = book;
Console.WriteLine($"{borrower.Name} borrowed {book.Title}");
returners.Enqueue(borrower);
}
}
private static void ServiceReturner()
{
if (returners.Count == 0)
{
Console.WriteLine($"There are no more returners waiting in line.");
}
else
{
var returner = returners.Dequeue();
var book = returner.BorrowedBook;
returner.BorrowedBook = null;
Add(book);
Console.WriteLine($"{returner.Name} has returned {book.Title}.");
Console.Write("Do they want to borrow another one (Y/N)?: ");
var input = Console.ReadKey();
Console.WriteLine();
if (input.Key == ConsoleKey.Y)
{
Lineup(returner);
}
}
}
输出: