Question
JAVA Using the for loop statement, define a method that reads and displays the details of the Book objects added in the ArrayList. Using Scanner
JAVA
Using the for loop statement, define a method that reads and displays the details of the Book objects added in the ArrayList.
Using Scanner and the for loop statement, define a method that reads in the ISBN of a book and deletes the corresponding Book from the ArrayList. The method should return true if the book is deleted and false if it is not (if a book with the ISBN does not exist in the ArrayList).
Book.java
public class Book {
private String title;
private String author;
private String ISBN;
private double RRP;
public Book(String t, String a, String ISBN, double RRP){
this.title = t;
this.author = a;
this.ISBN = ISBN;
this.RRP = RRP;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public double getRRP() {
return RRP;
}
public void setRRP(double RRP) {
this.RRP = RRP;
}
}
Main.java
import java.util.ArrayList;
import java.util.Scanner;
public class MyClass {
public static void main(String[] args){
ArrayList
Scanner sc = new Scanner(System.in);
for (int i=0; i<4; i++) {
System.out.println("Enter the book title: ");
String title = sc.next();
System.out.println("Enter the author of the book: ");
String author = sc.next();
System.out.println("Enter ISBN: ");
String ISBN = sc.next();
System.out.println("Enter RRP: ");
double RRP = sc.nextDouble();
Book b = new Book(title, author, ISBN, RRP);
myList.add(b);
}
sc.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started