2

In this code when I add 2 books and try to delete 1st and 2nd book, 1st gets deleted and 2nd one didn't. In next case when I add 3 books and delete one by one 1 gets deleted and 2 not and 3rd one becomes missing when I delete 2. When I add 4 books 2 and 4 not deleted.

My code (I'm a trainee so I'm a beginner in 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);
            }
        }
    }
}
VMAtm
  • 27,943
  • 17
  • 79
  • 125

4 Answers4

1

You should remove the books by using the bookId or more specifically by the unique Id of the object. Your posted code is removing it from the list using the position of book. The sole purpose of having a bookId is to identify the book from any number of books and then quickly manipulate it using code.

Let me tell you something about Object Oriented Programming (OOP) which will help you some way. Every object has three features in it:

  1. Identity
  2. State
  3. Behavior

Here, Identity (something unique like a bookId which can be used to select it) is the thing that you should use to manipulate your objects (books), not the position of the object.

Enough theory, In your code

bookList.RemoveAt(Del - 1);

is the culprit. Change it to work on the basis of bookId and your problem will be solved.

Thanks

vivek
  • 1,595
  • 2
  • 18
  • 35
0

The problem is that when you remove a book, your booklist is being reordered so the book ids no longer reflect the position that they were in when you added them. E.g. your bookList has 2 books (bookList[0] and bookList[1]). When you delete 1 book, you are left with booklist[0], so when you try and delete the next book (with your code it says you using del -1 to get the position) you are then trying to delete from your array at bookList[1] which of course, does not exist.

0

In your code you are mixing up the List and Array data structure. Let's take a closer look:

if (bookList.Exists(x => x.bookId == Del))

Here you are using the iterative search in your book list for a book. First of all, you can do it faster by introducing additional dictionary, then your program will be faster. Second is that after this, you get the id of the book, and remove previous book, as it is not a list, but the array:

bookList.RemoveAt(Del - 1);

Why this isn't work as expected? It's simple, lets say that you've added three books, and your list looks like (arrows here are links between books in your list):

1 --> 2 --> 3

After deleting the second book your list looks like:

1 --> 3

If you add another book, it will have id equal to 3! because of this line (bookList.Count equal to 2 here):

Console.WriteLine("Book Id:{0}", book.bookId = bookList.Count + 1);

So your list looks like this:

1 --> 3 --> 3

So you have to maintain additional counter for unique identification for your books. Another thing is that you have to learn is additional LINQ methods which you can use, such as RemoveAll for easy delete from list, Find for easy searching into the list, and so on. You can find some advises about LINQ here.

Community
  • 1
  • 1
VMAtm
  • 27,943
  • 17
  • 79
  • 125
0

The Problem is inconstancy in book id. It should be unique in list to identify correct book.

Here is quick fix .

Add Linq namespace in project :

using System.Linq;

Modify GetBook() Method to generate unique bookId by using max book id number from list

var bookId = bookList.Count> 0 ? bookList.Max(b => b.bookId) : 0;
Console.WriteLine("Book Id:{0}", book.bookId = bookId + 1);