所以我目前正在尝试以初学者的身份掌握 c#,我正在构建一个图书馆管理系统。我已经添加了我需要的一切,但是,我想添加它的一个功能特性,你可以借一本书,它会将它标记为已借,并且在他们归还之前没有其他人能够借出它。我尝试为它添加一个布尔值来将一本书标记为已借,但它似乎并没有将一本书标记为已借,而是让我无限次运行借阅功能。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Bibliotek
{
//Definerar klassen Book
class Book
{
public boolean IsBorrowed
{ get; set; }
public string Author
{ get; set; }
public string Title
{ get; set; }
}
class Program
{
static List<Book> BookList = new List<Book>();
static Book Book = new Book();
//Menyn och startsidan till programmet
static void Main(string[] args)
{
using (StreamReader readFile = new StreamReader("Bookfil.txt"))
{
string s;
while ((s = readFile.ReadLine()) != null)
{
Book Book = new Book();
string[] BookData = s.Split(',');
Book.Author = BookData[0];
Book.Title = BookData[1];
BookList.Add(Book);
}
readFile.Close();
}
bool stäng = true;
while (stäng)
{
Console.WriteLine("\n\nVälkommen till biblioteket!\n" +
"\n1)Lägg till Bok\n" +
"2)Radera Bok\n" +
"3)Leta efter Bok\n" +
"4)Låna Bok\n" +
"5)Lämna tillbaks Bok\n" +
"6)Stäng program\n\n");
int val = int.Parse(Console.ReadLine());
if (val == 1)
{
AddBook();
}
else if (val == 2)
{
RemoveBook();
}
else if (val == 3)
{
SearchBook();
}
else if (val == 4)
{
BorrowBook();
}
else if (val == 5)
{
ReturnBook();
}
else if (val == 6)
{
Console.WriteLine("Tack!");
stäng = false;
break;
}
else
{
Console.WriteLine("Invalid option\nFörsök igen! ");
}
}
public static void BorrowBook()
{
Console.Write("Mata in en Author eller Booktitel: ");
string borrow = Console.ReadLine();
foreach (Book b in BookList)
{
if (b.Title.Equals(borrow))
{
if (b.IsBorrowed == true)
{
Console.WriteLine("Book is already taken");
}
else
{
Console.WriteLine(b.Author + ":" + b.Title);
b.IsBorrowed = true;
}
}
}
}