在这段代码中,当我添加2本书并尝试删除第1本和第2本时,第1本被删除,第2本没有。在下一种情况下,当我添加3本书并逐一删除时,1本被删除,2本没有,当我删除2本时,第三本不见了。当我添加4本书2和4未删除。
我的代码(我是实习生,所以我是c#的初学者)
using System;
using System.Collections.Generic;
namespace LibraryManagement
{
//Defining a class Book
class Book
{
public int bookId;
public string bookName;
public int bookPrice;
public int bookCount;
public int x;
}
//Defining a class Borrow
class BorrowDetails
{
public int userId;
public string userName;
public string userAddress;
public int borrowBookId;
public DateTime borrowDate;
public int borrowCount;
}
class Program
{
static List<Book> bookList = new List<Book>();
static List<BorrowDetails> borrowList = new List<BorrowDetails>();
static Book book = new Book();
static BorrowDetails borrow = new BorrowDetails();
//Password verfication and Menu
static void Main(string[] args)
{
Console.Write("Welcome !!!nEnter your password :");
string password = Console.ReadLine();
if (password == "sync")
{
bool close = true;
while (close)
{
Console.WriteLine("nMenun" +
"1)Add bookn" +
"2)Delete bookn" +
"3)Search bookn" +
"4)Borrow bookn" +
"5)Return bookn" +
"6)Closenn");
Console.Write("Choose your option from menu :");
int option = int.Parse(Console.ReadLine());
if (option == 1)
{
GetBook();
}
else if (option == 2)
{
RemoveBook();
}
else if (option == 3)
{
SearchBook();
}
else if (option == 4)
{
Borrow();
}
else if (option == 5)
{
ReturnBook();
}
else if (option == 6)
{
Console.WriteLine("Thank you");
close = false;
break;
}
else
{
Console.WriteLine("Invalid optionnRetry !!!");
}
}
}
else
{
Console.WriteLine("Invalid password");
}
Console.ReadLine();
}
//To add book details to the Library database
public static void GetBook()
{
Book book = new Book();
Console.WriteLine("Book Id:{0}", book.bookId = bookList.Count + 1);
Console.Write("Book Name:");
book.bookName = Console.ReadLine();
Console.Write("Book Price:");
book.bookPrice = int.Parse(Console.ReadLine());
Console.Write("Number of Books:");
book.x = book.bookCount = int.Parse(Console.ReadLine());
bookList.Add(book);
}
//To delete book details from the Library database
public static void RemoveBook()
{
Book book = new Book();
Console.Write("Enter Book id to be deleted : ");
int Del = int.Parse(Console.ReadLine());
if (bookList.Exists(x => x.bookId == Del))
{
bookList.RemoveAt(Del - 1);
Console.WriteLine("Book id - {0} has been deleted", Del);
}
else
{
Console.WriteLine("Invalid Book id");
}
bookList.Add(book);
}
//To search book details from the Library database using Book id
public static void SearchBook()
{
Book book = new Book();
Console.Write("Search by BOOK id :");
int find = int.Parse(Console.ReadLine());
if (bookList.Exists(x => x.bookId == find))
{
foreach (Book searchId in bookList)
{
if (searchId.bookId == find)
{
Console.WriteLine("Book id :{0}n" +
"Book name :{1}n" +
"Book price :{2}n" +
"Book Count :{3}", searchId.bookId, searchId.bookName, searchId.bookPrice, searchId.bookCount);
}
}
}
else
{
Console.WriteLine("Book id {0} not found", find);
}
}
//To borrow book details from the Library
public static void Borrow()
{
Book book = new Book();
BorrowDetails borrow = new BorrowDetails();
Console.WriteLine("User id : {0}", (borrow.userId = borrowList.Count + 1));
Console.Write("Name :");
borrow.userName = Console.ReadLine();
Console.Write("Book id :");
borrow.borrowBookId = int.Parse(Console.ReadLine());
Console.Write("Number of Books : ");
borrow.borrowCount= int.Parse(Console.ReadLine());
Console.Write("Address :");
borrow.userAddress = Console.ReadLine();
borrow.borrowDate = DateTime.Now;
Console.WriteLine("Date - {0} and Time - {1}", borrow.borrowDate.ToShortDateString(), borrow.borrowDate.ToShortTimeString());
if (bookList.Exists(x => x.bookId == borrow.borrowBookId))
{
foreach (Book searchId in bookList)
{
if (searchId.bookCount >= searchId.bookCount - borrow.borrowCount && searchId.bookCount - borrow.borrowCount >= 0)
{
if (searchId.bookId == borrow.borrowBookId)
{
searchId.bookCount = searchId.bookCount - borrow.borrowCount;
break;
}
}
else
{
Console.WriteLine("Only {0} books are found", searchId.bookCount);
break;
}
}
}
else
{
Console.WriteLine("Book id {0} not found", borrow.borrowBookId);
}
borrowList.Add(borrow);
}
//To return borrowed book to the library
public static void ReturnBook()
{
Book book = new Book();
Console.WriteLine("Enter following details :");
Console.Write("Book id : ");
int returnId = int.Parse(Console.ReadLine());
Console.Write("Number of Books:");
int returnCount = int.Parse(Console.ReadLine());
if (bookList.Exists(y => y.bookId == returnId))
{
foreach (Book addReturnBookCount in bookList)
{
if (addReturnBookCount.x >= returnCount + addReturnBookCount.bookCount)
{
if (addReturnBookCount.bookId == returnId)
{
addReturnBookCount.bookCount = addReturnBookCount.bookCount + returnCount;
break;
}
}
else
{
Console.WriteLine("Count exists the actual count");
break;
}
}
}
else
{
Console.WriteLine("Book id {0} not found", returnId);
}
}
}
}
您应该通过使用bookId
或更具体地使用对象的惟一Id来删除books
。你张贴的代码是从列表中删除它使用的书的位置。拥有bookId
的唯一目的是从任意数量的books
中识别书,然后使用代码快速操作它。
让我告诉你一些关于面向对象编程(OOP)的东西,这将在某种程度上帮助你。每个对象都有三个特征:
在这里,标识符(像bookId
这样的唯一的东西,可以用来选择它)是你应该用来操作对象(书籍)的东西,而不是对象的位置。
足够的理论,在你的代码
bookList.RemoveAt(Del - 1);
是罪魁祸首。在bookId
的基础上改变它,你的问题就解决了。
谢谢
问题是,当您删除一本书时,您的图书列表将被重新排序,因此图书id不再反映您添加它们时的位置。例如,你的bookList有2本书(bookList[0]和bookList[1])。当您删除1本书时,您将留下booklist[0],因此当您尝试删除下一本书时(您的代码显示您使用del -1来获取位置),您将尝试从booklist[1]数组中删除,当然,booklist[1]不存在。
在您的代码中,您混淆了List
和Array
数据结构。让我们仔细看看:
if (bookList.Exists(x => x.bookId == Del))
在这里,您正在使用图书列表中的迭代搜索来查找一本书。首先,你可以通过引入额外的字典来做得更快,然后你的程序会更快。其次,在此之后,您将获得图书的id
,并删除前一本图书,因为它不是列表,而是数组:
bookList.RemoveAt(Del - 1);
为什么不像预期的那样工作?这很简单,假设您已经添加了三本书,您的列表看起来像(这里的箭头是列表中书籍之间的链接):
1 --> 2 --> 3
删除第二本书后,列表如下:
1 --> 3
如果您添加另一本书,它的id将等于3
!因为这一行(bookList.Count
在这里等于2):
Console.WriteLine("Book Id:{0}", book.bookId = bookList.Count + 1);
所以你的列表是这样的:
1 --> 3 --> 3
因此,您必须维护额外的计数器来惟一标识您的图书。另一件事是,你必须学习额外的LINQ方法,你可以使用,如RemoveAll
方便地从列表中删除,Find
方便地搜索到列表,等等。
问题是book id不稳定。它应该是唯一的列表,以确定正确的书。
这是一个快速解决方案。
在项目中添加Linq命名空间:
using System.Linq;
修改GetBook()方法,通过使用列表中的最大图书id号来生成唯一的bookId
var bookId = bookList.Count> 0 ? bookList.Max(b => b.bookId) : 0;
Console.WriteLine("Book Id:{0}", book.bookId = bookId + 1);