0

I have these objects from "book" class in a list (BLibShelf) but would like to start adding them to such list with console input

namespace Library
{
    class Program
    {
        static void Main(string[] args)
        {
            Book book1 = new Book("book one", "Author one", 900, 35, 16);
            Book book2 = new Book("book two", "Author two", 240, 42, 8);
            Book book3 = new Book("book three", "Author three", 700, 23, 8);
         
            List<Book> BLibShelf = new List<Book>();
            BLibShelf.AddRange(new List<Book>() { book1, book2, book3, book4, book5, book6, book7,  book8 });

 Console.ReadLine();

        }
       
    }

here is my book class

class Book
    {
        public string title;
        public string author;
        public int pages;
        public int Libcopies;
        public int BCopies;
                     
        public Book(string nTitle, string nAuthor, int nPages, int nLcopies, int nBcopies)
        {
            title = nTitle;
            author = nAuthor;
            pages = nPages;
            Libcopies = nLcopies;
            BCopies = nBcopies;
        }

        
1
  • 3
    Get the inputs from the user using var title = Console.ReadLine(); then use these variables in the constructor to make a new book var book = new Book(title, author....); and then add it to the list using BLibShelf.Add(book); Commented Dec 27, 2021 at 21:38

1 Answer 1

1

I wrote a simple demo that supports inputting the new book title and pages.

You can implement the other fields yourself, hope it helps :)

    class Program
    {
        static void Main(string[] args)
        {
            Book book1 = new Book("book one", "Author one", 900, 35, 16);
            Book book2 = new Book("book two", "Author two", 240, 42, 8);
            Book book3 = new Book("book three", "Author three", 700, 23, 8);

            List<Book> BLibShelf = new List<Book>();
            BLibShelf.AddRange(new List<Book>() { book1, book2, book3 });

            Console.WriteLine("Total " + BLibShelf.Count + " books");

            Console.WriteLine("Please input the new book Title:");
            var title = Console.ReadLine();

            Console.WriteLine("Please input the new book Pages(integer):");
            var pages = int.Parse(Console.ReadLine());
            var newBook = new Book(title, string.Empty, pages, 0, 0);
            BLibShelf.Add(newBook);

            Console.WriteLine("Total " + BLibShelf.Count + " books");
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.