Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. List not printing out hardcoded books already added to list

List not printing out hardcoded books already added to list

Scheduled Pinned Locked Moved C#
algorithmsloungelearning
5 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14514432
    wrote on last edited by
    #1

    Hi, 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

    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);
        }
    }
    

    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;
    }
    }

    public class UserInput
    {
    public static Boolean KeepGoing = true;
    public static List bookList = new List();
    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("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
        }
    
    D 1 Reply Last reply
    0
    • M Member_14514432

      Hi, 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

      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);
          }
      }
      

      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;
      }
      }

      public class UserInput
      {
      public static Boolean KeepGoing = true;
      public static List bookList = new List();
      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("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
          }
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      This would be EASILY found running the code under the debugger. Look at the flow of your code, specifically when you call EnterBookInfo AND where you put the "hardcoded" books to get added. So, yeah, those books won't get added until you call, and wait for, user input. You're program structure is a bit messed up. You should NOT have a class called "AddBook". That should be a method on a collection class called Books. Typically, if you have a name that starts with a verb, "Add", it should be a method in a class. Nouns should be classes, like "Book" or "Books". "Search" should also be a method of the Books class. Both methods should NOT be prompting or handling user input. "PrintListOfBooks" should also be a method of a Books class. Your "UserInput" class, while OK, is a bit misplaced. You should be expanding it a bit to handle all prompting and user input for all menu options.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        This would be EASILY found running the code under the debugger. Look at the flow of your code, specifically when you call EnterBookInfo AND where you put the "hardcoded" books to get added. So, yeah, those books won't get added until you call, and wait for, user input. You're program structure is a bit messed up. You should NOT have a class called "AddBook". That should be a method on a collection class called Books. Typically, if you have a name that starts with a verb, "Add", it should be a method in a class. Nouns should be classes, like "Book" or "Books". "Search" should also be a method of the Books class. Both methods should NOT be prompting or handling user input. "PrintListOfBooks" should also be a method of a Books class. Your "UserInput" class, while OK, is a bit misplaced. You should be expanding it a bit to handle all prompting and user input for all menu options.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        M Offline
        M Offline
        Member_14514432
        wrote on last edited by
        #3

        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

        public static Boolean KeepGoing = true;
        public static List bookList = new List();

            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();
        
        D 1 Reply Last reply
        0
        • M Member_14514432

          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

          public static Boolean KeepGoing = true;
          public static List bookList = new List();

              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();
          
          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          It'll work. But for "optimal", you'd have to scrap your entire code and start from scratch, except for the Book class. That's about the only thing you've got going that's correct for "optimal".

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          M 1 Reply Last reply
          0
          • D Dave Kreskowiak

            It'll work. But for "optimal", you'd have to scrap your entire code and start from scratch, except for the Book class. That's about the only thing you've got going that's correct for "optimal".

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            M Offline
            M Offline
            Member_14514432
            wrote on last edited by
            #5

            Thanks for posting dave, I'll scrap everything and start over and try again.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups