Justin L Justin L - 2 months ago 14
C# Question

C# method for finding Book in list, and removing a Book from the list

I have to make a Library class that has a list of Books with a Title, Author, and ID#. I have to make methods to add books, find books by title, display all books, and remove books.

Is my addBook() method correct?

I have an error with my findBook() method saying to create a constructor in the Book class but there already is a constructor.

Also I really have no idea where to begin to remove a Book from the list... I have looked up solutions for removing items from a list but I don't really get how I would implement it in my program.

I don't understand a lot of things because my first college course CSE 1301 was terrible, and I am in 1302 having to learn basic things plus the more complex things.

public class Library
{
public string name;
public List<Book> books = new List<Book>();

public void addBook(string title, string author)
{
int bookQuantity;

string btitle;
string bauthor;

bookQuantity = int.Parse(Console.ReadLine());

for (int x = 0; x <= bookQuantity; x++)
{
Console.WriteLine("Enter Title:");
btitle = Console.ReadLine();

Console.WriteLine("Enter Author:");
bauthor = Console.ReadLine();

books.Add(new Book(btitle, bauthor));
}
}

public void findBook()
{
var obj = new Book();
obj.ID = "xy";
string id = obj.ID;

Book result = books.Find(x => x.ID == "xy");
}

public void displayBooks()
{
foreach (Book b in books)
{
Console.WriteLine(b.ToString());
}
}

public void removeBook()
{

}

public Library()
{

}
}

public class Book
{
public string title;
public string author;
public int id;
static int isbn;

public string ID { get; set; }

public void assignID()
{
id = isbn;
isbn++;
}

public Book(string title, string author)
{
this.title = title;
this.author = author;
}

public override string ToString()
{
return string.Format("Title: " + title, "\nAuthor: " + author, "\nISBN: " + id);
}
}

Answer

The method is not correct. It has some flaws: 1. It calls I/O methods to get information from the user. But that's not what this function ought to be doing. Its job is to add a book to a library. It should be given information about the book via parameters, and then use that information to construct a new book and add it to the internal list. Nothing else. You actually have most of the code for that, so fixing your problem would be along the lines of deleting a bunch of code and reworking what is left. 2. The function says "add book", but it actually adds multiple books, based on user input, and ignores the arguments passed to it. A function should say what it does, and then actually do it.

The same goes for your findBook() code, which neither takes any parameters (how does it know what to find?) nor returns anything (whatever it finds is ignored and can't be used by any other code).

There are many other issues with your code, but I am focusing on the question you asked, about addBook(). In general, though, if you make functions that do what they say, take parameters and return things, and don't do extra work, or do I/O, then you will probably be able to improve the code on your own. Please let me know if you have follow up questions.