first i am sorry if the tittle is misleading. I am kind of new to programing and don't know how to describe my problem correctly.
Here is my code, i have 1 parent class, and 2 child class that extend the parent and 1 main class.
Book.java
public class Book {
private String bookTittle;
private String bookAuthor;
public Book(String tittle, String author)
{
this.bookTittle = tittle;
this.bookAuthor = author;
}
public String getTittle()
{
return bookTittle;
}
// other getter and setter
}
Science.java
public class Science extends Book {
private int SciID;
public Science(String tittle, String author, int ScienceID)
{
super(tittle, author);
this.SciID = ScienceID;
}
// setter and getter
}
Fiction.java
public class Fiction extends Book {
private int fictId;
public Fiction(String tittle, String author, int fictionID)
{
super(tittle, author);
this.fictId = fictionID;
}
// setter and getter
}
LibraryMain.java
import java.util.*;
public class LibraryMain {
private ArrayList<Book> LibBook = new ArrayList<>();
public LibraryMain()
{
LibBook.add(new Science("Frog", "June", 101));
LibBook.add(new Fiction("TimeTravel", "Kiel", 201));
}
public boolean bookSearch(String bookTittle)
{
for (int i = 0;i<LibBook.size();i++)
{
if(LibBook.get(i).getTittle().equalsIgnoreCase(bookTittle)){
return true;
}
}
return false;
}
public static void main(String[] args) {
LibraryMain mainLib = new LibraryMain();
Scanner in = new Scanner(System.in);
System.out.println("--Search book--");
System.out.print("Book tittle: ");
String tittle = in.nextLine();
if(mainLib.bookSearch(tittle)){
System.out.println("Book Found!");
}
else{
System.out.println("Not Found");
}
}
}
Currently the method on LibraryMain can only find if the book exist in the arraylist or not, is there anyway to know from which class the book came from?
Current result is
--Search book--
Book tittle: frog
Book Found!
How i wanted the result to be
--Search book--
Book tittle: frog
Book Found! frog is a science book