I have set up an ArrayList of type book for my library application. The current feature I am trying to implement is editing the details of a book. I have a variable called bookID
so when I call this method through the ArrayList it will be newBook.get(index).getBookID();
. And with this information I would like to check, and do the following things:
- There exists an element of the array with this ID
- Update the existing title with a new title
Problems I am being faced with:
- Looping through to get the index where the ID exists
- Updating the existing title, and replacing it with a new title
What I have came up with so far:
// Editing book in ArrayList
public void editBook(){
System.out.println("==============================");
// Ensuring array has at least one book
if(newBook.size() > 0){
// Get which part, and book the user would like to edits
System.out.print("Enter Book ID to begin editing: ");
int bookID = sc.nextInt();
System.out.println("Press 1 or 2 to edit.");
System.out.println("1: Edit title");
System.out.println("2: Edit author");
System.out.print("Enter option: ");
int editOption = sc.nextInt();
// Switch statement which will handle editing book
switch(editOption){
case 1:
// New title
System.out.print("Enter new title: ");
String newTitle = sc.nextLine();
sc.next();
// Updates title
newBook.get(position).setTitle(newTitle);
// Prints out title
System.out.println(newBook.get(position).getTitle());
break; // Edit title
Above code is only partial, and anything below this is irrelevant to the question.