C#库管理控制台程序
C# Library Management console program
在这段代码中,当我添加 2 本书并尝试删除第 1 本书和第 2 本书时,第 1 本书被删除而第 2 本书没有。在下一种情况下,当我添加 3 本书并一一删除时,第 1 本书被删除,第 2 本书没有,第 3 本书在我删除 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("\nMenu\n" +
"1)Add book\n" +
"2)Delete book\n" +
"3)Search book\n" +
"4)Borrow book\n" +
"5)Return book\n" +
"6)Close\n\n");
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 option\nRetry !!!");
}
}
}
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);
}
}
}
}
问题是,当您删除一本书时,您的图书列表会重新排序,因此图书 ID 不再反映它们在您添加它们时所处的位置。
例如。您的 bookList 有 2 本书(bookList[0] 和 bookList[1])。当你删除一本书时,你会留下 booklist[0],所以当你尝试删除下一本书时(你的代码说你使用 del -1 来获取位置)然后你试图从你的数组中删除bookList[1] 当然不存在。
您应该使用 bookId
或更具体地说是通过对象的唯一 ID 删除 books
。您发布的代码使用书的位置将其从列表中删除。拥有 bookId
的唯一目的是从任意数量的 books
中识别出这本书,然后使用代码快速操作它。
让我告诉您一些有关面向对象编程 (OOP) 的知识,这会对您有所帮助。每个对象都具有三个特征:
- 身份
- 州
- 行为
在这里,Identity(像 bookId
一样独特的东西,可以用来 select 它)是你应该用来操纵你的对象(书籍)的东西,而不是对象。
足够的理论,在你的代码中
bookList.RemoveAt(Del - 1);
是罪魁祸首。改成在bookId
的基础上工作,你的问题就解决了
谢谢
在您的代码中,您混淆了 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
for easy delete from list, Find
for easy searching into the list, and so on. You can find some advises about LINQ here.
问题是图书 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);
在这段代码中,当我添加 2 本书并尝试删除第 1 本书和第 2 本书时,第 1 本书被删除而第 2 本书没有。在下一种情况下,当我添加 3 本书并一一删除时,第 1 本书被删除,第 2 本书没有,第 3 本书在我删除 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("\nMenu\n" +
"1)Add book\n" +
"2)Delete book\n" +
"3)Search book\n" +
"4)Borrow book\n" +
"5)Return book\n" +
"6)Close\n\n");
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 option\nRetry !!!");
}
}
}
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);
}
}
}
}
问题是,当您删除一本书时,您的图书列表会重新排序,因此图书 ID 不再反映它们在您添加它们时所处的位置。 例如。您的 bookList 有 2 本书(bookList[0] 和 bookList[1])。当你删除一本书时,你会留下 booklist[0],所以当你尝试删除下一本书时(你的代码说你使用 del -1 来获取位置)然后你试图从你的数组中删除bookList[1] 当然不存在。
您应该使用 bookId
或更具体地说是通过对象的唯一 ID 删除 books
。您发布的代码使用书的位置将其从列表中删除。拥有 bookId
的唯一目的是从任意数量的 books
中识别出这本书,然后使用代码快速操作它。
让我告诉您一些有关面向对象编程 (OOP) 的知识,这会对您有所帮助。每个对象都具有三个特征:
- 身份
- 州
- 行为
在这里,Identity(像 bookId
一样独特的东西,可以用来 select 它)是你应该用来操纵你的对象(书籍)的东西,而不是对象。
足够的理论,在你的代码中
bookList.RemoveAt(Del - 1);
是罪魁祸首。改成在bookId
的基础上工作,你的问题就解决了
谢谢
在您的代码中,您混淆了 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
for easy delete from list, Find
for easy searching into the list, and so on. You can find some advises about LINQ here.
问题是图书 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);