List not printing out hardcoded book already added to list

Associate
Joined
11 Jul 2009
Posts
1,318
I'm trying to make a library where you can add books, search for books etc, I have a hardcoded book that you should be able to search for but it only displays the after I add a new book and then it will display the hardcoded book if you search for it again

the code

Code:
public class Book
    {
        public String BookName { get; set; }
        public String AuthorName { get; set; }
        public int NumberOfCopiesInStock { get; set; }
        public String ISBN { get; set; }

        public Book(String bookName, String authorName, int numberOfCopiesInStock, String isbn)
        {
            this.BookName = bookName;
            this.AuthorName = authorName;
            this.NumberOfCopiesInStock = numberOfCopiesInStock;
            this.ISBN = isbn;
        }

        public override String ToString()
        {
            return ("BookName: " + BookName
                  + "\n"
                  + "Author Name: " + AuthorName
                  + "\n"
                  + "Number Of Copies: " + NumberOfCopiesInStock
                  + "\n"
                  + "ISBN: " + ISBN);
        }
    }

Code:
public class Search
    {
        public Boolean SearchForBook()
        {
            Console.WriteLine("Please enter name of book: ");
            String search = Console.ReadLine();
            UserInput.MakeTheComputerSleep();
            foreach (Book b in UserInput.bookList)
            {
                if (b.BookName.Equals(search))
                {
                    Console.WriteLine(b);
                    return true;
                }
            }
            Console.WriteLine("Book doesn't exist! ");
            return false;
        }
    }

Code:
public class UserInput
    {
        public static Boolean KeepGoing = true;
        public static List<Book> bookList = new List<Book>();
        private static readonly String INPUT_ERROR_MESSAGE = "Please only enter NUMBERS!";
        private static readonly String INVALID_CHOICE = "INVALID CHOICE ENTERED!";
        private static readonly String SEARCH_MESSAGE = "Searching for book,please wait......";
        private static Random RandomObject = new Random();

        public static void WelcomeMessage()
        {
            Console.WriteLine("********************");
            Console.WriteLine("*       Cork       *");
            Console.WriteLine("*      Library     *");
            Console.WriteLine("********************");
        }

        private static void MenuHeader()
        {
            Console.WriteLine("********************");
            Console.WriteLine("*       Menu       *");
            Console.WriteLine("*      Options     *");
            Console.WriteLine("********************");
        }

        private static void MenuOptions()
        {
            Console.WriteLine("1.) Add Book.             ");
            Console.WriteLine("2.) Search for Book.      ");
            Console.WriteLine("3.) Delete Book.          ");
            Console.WriteLine("4.) Display all Book.     ");
            Console.WriteLine("0.) Exit.                 ");
        }

        public static void GetUserInput()
        {
            int choice;
            MenuHeader();
            do
            {
                MenuOptions();
                Console.WriteLine(">");
                choice = 0;
                while (!int.TryParse(Console.ReadLine(), out choice))
                {
                    Console.WriteLine(INPUT_ERROR_MESSAGE);
                }
                PickChoice(choice);
            } while (choice != 0);
        }

        private static void PickChoice(int choice)
        {
            switch (choice)
            {
                case 1:
                    AddBook addBook = new AddBook();
                    addBook.EnterBookInfo();
                    break;

                case 2:
                    Search search = new Search();
                    search.SearchForBook();
                    break;

                case 3:
                    break;

                case 4:
                    
                    AddBook addBook1 = new AddBook();
                    addBook1.PrintListOfBooks();
                    break;

                case 0:
                    Exit exit = new Exit();
                    exit.Exit_Application();
                    break;

                default:
                    Console.WriteLine(INVALID_CHOICE);
                    break;
            }
        }

        public static void MakeTheComputerSleep()
        {
            int number = RandomObject.Next(10) + 1;
            Console.WriteLine("Searching for Book.......");
            Thread.Sleep(number * 1000);
        }

        
    }


Code:
public class AddBook
    {
        public void EnterBookInfo()
        {
            while (UserInput.KeepGoing)
            {

                Console.WriteLine("Please enter name of book: ");
                String BookName = Console.ReadLine();

                if (BookName.Equals("end"))
                {
                    break;
                }

                Console.WriteLine("Please enter Author Name: ");
                String AuthorName = Console.ReadLine();

                Console.WriteLine("Please enter ISBN Number: ");
                String ISBN = Console.ReadLine();

                Console.WriteLine("Please enter number of books: ");
                int NumberOfCopiesInStock = Convert.ToInt32(Console.ReadLine());

                Book book1 = new Book("Neverwhere", "Neil Gaimen", 1, "aaaaa");
                Book book = new Book(BookName, AuthorName, NumberOfCopiesInStock, ISBN);
                UserInput.bookList.Add(book);
                UserInput.bookList.Add(book1);

            }

        }

        public void PrintListOfBooks()
        {
            foreach (Book b in UserInput.bookList)
            {
                Console.WriteLine(b.ToString());
                Console.WriteLine();
            }
        }
    }

Code:
class Program
    {
        public static void Main(string[] args)
        {
            UserInput.WelcomeMessage();
            UserInput.GetUserInput();
        }
    }

any help at all would be appreciated , thanks.
 
As far as I can see the code to add your 'hardcoded' book is added within the EnterBookInfo() function of your AddBook class.

If you want that book to be available from the start you need to be adding it at the start, most likely as the first thing in Main.

Your class structure doesn't seem to make much sense.

Having a class for a book, that's fine.

After that, I'd be creating a class for the library (probably called Library). It would include the List<T> for the books and all of the functionality for adding books, searching for books, etc.

The user interface shouldn't be mixed within it.
 
Last edited:
I think the problem is this line in the SearchForBook method (and the same problem in PrintListOfBooks method on the AddBook class):

foreach (Book b in UserInput.bookList)

That UserInput.bookList property could be empty. It only gets populated in the AddBook option so if you havn't used that option first the SearchForBook and PrintListOfBooks methods will both show no results.

You probably want to have a Library class like @bremen1874 said and hold a list of Book objects in there:

public class Library
{
public List<Book> allAvailableBooks = new List<Book>();
public Library(){
//load all books into allAvailableBooks here
}
}

Then in your search method:

foreach (Book b in Library.allAvailableBooks)
 
Last edited:
Thanks for responding fella's, I did some messing around and I added a method called AddBooks(); with the hardcoded books and then I added method calls within the switch statment to that method and it now seems to work the way I want it too


Code:
public static Boolean KeepGoing = true;
        public static List<Book> bookList = new List<Book>();
      
        private static readonly String INPUT_ERROR_MESSAGE = "Please only enter NUMBERS!";
        private static readonly String INVALID_CHOICE = "INVALID CHOICE ENTERED!";
        private static readonly String SEARCH_MESSAGE = "Searching for book,please wait......";
        private static Random RandomObject = new Random();
        
        
      

        public static void WelcomeMessage()
        {
            Console.WriteLine("********************");
            Console.WriteLine("*       Cork       *");
            Console.WriteLine("*      Library     *");
            Console.WriteLine("********************");
        }

        private static void MenuHeader()
        {
            Console.WriteLine("********************");
            Console.WriteLine("*       Menu       *");
            Console.WriteLine("*      Options     *");
            Console.WriteLine("********************");
        }

        private static void MenuOptions()
        {
            Console.WriteLine("1.) Add Book.             ");
            Console.WriteLine("2.) Search for Book.      ");
            Console.WriteLine("3.) Delete Book.          ");
            Console.WriteLine("4.) Display all Book.     ");
            Console.WriteLine("0.) Exit.                 ");
        }

        public static void GetUserInput()
        {
            int choice;
            MenuHeader();
            do
            {
                MenuOptions();
                Console.WriteLine(">");
                choice = 0;
                while (!int.TryParse(Console.ReadLine(), out choice))
                {
                    Console.WriteLine(INPUT_ERROR_MESSAGE);
                }
                PickChoice(choice);
            } while (choice != 0);
        }

        private static void PickChoice(int choice)
        {
            switch (choice)
            {
                case 1:
                    AddBook addBook = new AddBook();
                    addBook.EnterBookInfo();
                    break;

                case 2:
                    Search search = new Search();
                    search.SearchForBook();
                    AddBook();
                    break;

                case 3:
                    break;

                case 4:
                    //PrintListOfBooks();
                    
                    AddBook addBook1 = new AddBook();
                    AddBook();
                    addBook1.PrintListOfBooks();
                    break;

                case 0:
                    Exit exit = new Exit();
                    exit.Exit_Application();
                    break;

                default:
                    Console.WriteLine(INVALID_CHOICE);
                    break;
            }
        }

        public static void MakeTheComputerSleep()
        {
            int number = RandomObject.Next(10) + 1;
            Console.WriteLine("Searching for Book.......");
            Thread.Sleep(number * 1000);
        }

        public static void AddBook()
        {
            Book book1 = new Book("Neverwhere", "Neil Gaimen", 1, "aaaaa");
            bookList.Add(book1);
        }

I don't think this is the most optimal way though?
 
I think you'd enjoy it more if you got your head around the basic OOD principles.

You only really need three classes here.

Book
You've got that

Library
Should hold the list of books and provide the functions for adding, removing, searching, etc.
You'd create a global instance as the program starts. You could then immediately add your default book.

Interface
Just deals with user interaction.
Uses the functionality of the Library class to do the actual work.

Any reason you're writing console apps?
 
I think you'd enjoy it more if you got your head around the basic OOD principles.

You only really need three classes here.

Book
You've got that

Library
Should hold the list of books and provide the functions for adding, removing, searching, etc.
You'd create a global instance as the program starts. You could then immediately add your default book.

Interface
Just deals with user interaction.
Uses the functionality of the Library class to do the actual work.

Any reason you're writing console apps?

Its easier to get something up and running quickly but in saying that my goal is get this console app up and running and then see if I can get a gui version up and running, gonna take your advice regarding using the three classes and I'll post back here on how I get on thanks for the help.
 
Really, you wouldn't be storing the list of books in a variable in your code at all, that's what databases are for.
Your code would request the information from the database for each selection the user makes. For the search option, the code would select books from the database where the title matches the search term, or something similar to that. For the list of books, you'd select all the books from the database. When the user adds a book, the code would insert the book details into the database.

This is a more efficient way of storing and retrieving data but the main advantage here would be scalability. If you want to add another terminal you can just copy the same code onto it and the data storage is centralised so you can have as many as you want all running together.
 
Back
Top Bottom