So I am new to java (previously learnt python,jss,html and css) and have recently moved on to learning Classes and Objects.
I am making a book library as a practise exercise and want to get the user to input data for the book using scanner I will display the code I have done so far. 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 im 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. I know i might have messed up the explination but hopefully you more experienced programmers can understand what i am getting at.
Any help is much appreciated.
Thanks a lot.
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);
}
}
I have tried finding solutions on google but struggled to describe the issue and dont want to resort to Chatgpt as i generally struggle with the explinations it provides.
1