I am new to Java and am learning classes and objects.
I am making a book library as a practise exercise. I want to get the user to input data for a book using Scanner. I am aware I do not have a while loop in place but I would like to sort the code out for adding 1 book before I do that.
Currently I am stuck on how to iterate through and add multiple books. For example if the user choses to add a book I am assuming it should add books like book1name= Booklist.name. And for when the user wants to add another book I’m assuming it should be book2name= Booklist.name. However I am struggling on how to iterate through this (change it from book1name to book2name automatically) and how to change the variable name while the code is running.
import java.util.Scanner;
public class BookLibrary {
int counter=1;
public static void main (String[]args) {
Scanner choiceScan=new Scanner(System.in);
System.out.println("Do you want to 1:Add a book or 2:Remove a book");
int choice= choiceScan.nextInt();
if (choice==1) {
System.out.println("You want to add a book");
addBook();
}
}
public static void addBook() {
Scanner entryScan=new Scanner(System.in);
System.out.println("Enter the Name of the book");
String name=entryScan.nextLine();
System.out.println("Enter the Author of the book");
String author=entryScan.nextLine();
}
}
public class BookList {
String name;
String author;
int bookNum;
public BookList(){
}
public void printLibrary() {
System.out.println("Book name: "+name );
System.out.println("Author: " +author);
System.out.println("Book Number: "+bookNum);
}
}
>Solution :
Normally you wouldn’t change dynamically variable names. I think this is first a conceptual error rather than a coding issue.
Java provides several pre-made data structures, such as Maps, Sets, and Lists (this is what you need).
You shouldn’t need to create a BookList object. Instead you should create a Book class, and then instantiate a List of type Book.
I made some basic changes to your code including also some extra suggestions:
-
Renamed BookList as Book
-
Instantiated it using a constructor that ensures all parameters/dependencies are met
-
Added a List and now every new Book is added to that list after construction
-
EXTRA: you dont need to create one scanner per line to scan. I created a single scanner for the whole BookLibrary instance. (Scanners are not thread-safe, so be careful with that)
-
TODO: Add a while loop and every time you add a Book increase the counter so you get a new id
public class BookLibrary {
private int counter = 1;
private List bookList = new ArrayList<>();
private Scanner generalScanner = new Scanner(System.in);public static void main(String[] args) { final BookLibrary bookLibrary = new BookLibrary(); System.out.println("Do you want to 1:Add a book or 2:Remove a book"); int choice = bookLibrary.generalScanner.nextInt(); if (choice == 1) { System.out.println("You want to add a book"); bookLibrary.addBook(); } } public void addBook() { System.out.println("Enter the Name of the book"); String name = this.generalScanner.nextLine(); System.out.println("Enter the Author of the book"); String author = this.generalScanner.nextLine(); final Book book = new Book(name, author, this.counter); bookList.add(book); }Book class
public class Book {
String name;
String author;
int bookNum;public Book(String name, String author, int bookNum) { this.name = name; this.author = author; this.bookNum = bookNum; } public void printLibrary() { System.out.println("Book name: " + name); System.out.println("Author: " + author); System.out.println("Book Number: " + bookNum); } }