0

Salam!

In java, i required to create an ArrayList of a class inside that class. Is It Possible??? As i know that if i create object of a class in that class constructor, then it will cause StackOverFlow. Complete detail of the problem is given below. How can i create ArrayList of BookInfo class, in that class itself???

Problem Statement:
You are required to write a java program which contains only two classes named as “BookInfo” and “BookMgtSys”.

BookInfo class must have the following data members: • ISBN • Book Name • Author Name • Total Books When you will add book then it will increment variable Total Books and decrement when you will delete book.

BookInfo class must have the following member methods: • Default constructor • Parameterized constructor • addbook ()
• searchBook () • deleteBook ()

BookMgtSys is a public driver class that contains the main() method. The name of you file should be BookMgtSys as it is a public class in your program.

Detailed Description:

Default constructor: The default constructor will create an ArrayList.

Parameterized constructor: It should take three String parameters named as (ISBN, Book Name, and Author Name) and initializes the data members values with the passed parameters. addBook(): This method will ask the user to enter ISBN, Book name and Author name and then add the book in ArrayList and increment the Total Book variable. After successful adding the book, A message box should be displayed containing Book information. searchBook(): This method takes one parameter which is book name and search the book name in the ArrayList. If search is found then display the Book information otherwise display message that Book not found. deleteBook(): This method takes one parameter which is book name and delete the book from the ArrayList and then decrement the Total Book variable.

Thanks for your kind help.

Code is given below. But it not compiles. It gives following error:

C:\Code>javac BookMgtSys.java BookMgtSys.java:39: error: -> expected bookList = new ArrayList() (); ^ BookMgtSys.java:39: error: ';' expected bookList = new ArrayList() (); ^ BookMgtSys.java:67: error: reached end of file while parsing } ^ 3 errors

import javax.swing.*;
import java.util.*;
public class BookMgtSys
{
public static void main(String[] args)
{

System.out.println("Bismillah");
BookInfo Books=new BookInfo();
String  UserOption = JOptionPane.showInputDialog("Enter 1 to Add Book\r\nEnter 2 to Search Book\r\nEnter 3 to Delete Book\r\nEnter 4 to Exit System");
switch(UserOption)
{
    case "1":
    //  Books.addbook();
    break;
    case "2":
    break;
    case "3":
    break;
    case "4":
    break;

}
//JOptionPane.showMessageDialog(null, "You entered " + ipstring);
}
}
class BookInfo
{
 String ISBN;
 String BookName;
 String AuthorName;
 static int TotalBooks;
ArrayList<BookInfo> bookList;
//constructor
// ArrayList<BookInfo> bookList;
//BookInfo class must have the following member methods:
public BookInfo()
{
    bookList = new ArrayList() <BookInfo>();
//persons = new ArrayList()<PersonInfo>();
}
// public void BookInfo(String ISBN, String BookName, String AuthorName)
// {
// this.ISBN=ISBN;
// this.BookName=BookName;
// this.AuthorName=AuthorName;
// TotalBooks++;
// }
 // public void addbook ()
 // {
     // // this.ISBN=JOptionPane.showInputDialog("Enter The ISBN");
     // // this.BookName=JOptionPane.showInputDialog("Enter The Name");
     // // this.AuthorName=JOptionPane.showInputDialog("Enter The Author");

      // TotalBooks++;
 // }
// public void searchBook ()
// {

// }
// public void deleteBook ()
// {
    // if(TotalBooks>0)
        // TotalBooks--;
// }

}
1
  • Upvoted for the effort put in asking a well formatted question for your first try ;) Welcome on StackOverFlow Commented Nov 21, 2015 at 18:43

3 Answers 3

3

Creating an ArrayList<T> inside the constructor of class T will not create a recursive loop, because you are not creating additional instances of T, merely a container into which you can later add T instances.

Sign up to request clarification or add additional context in comments.

2 Comments

It gives the following error when i create array list in constructor C:\Code>javac BookMgtSys.java BookMgtSys.java:39: error: -> expected bookList = new ArrayList() <BookInfo>(); ^ BookMgtSys.java:39: error: ';' expected bookList = new ArrayList() <BookInfo>(); ^ BookMgtSys.java:67: error: reached end of file while parsing } ^ 3 errors
@RafiqueAbdullah Please post your code, it will be much easier :) edit : just done ^^ ty
2

Please post your code here?

You wrote new ArrayList() <BookInfo>();, yes? If true, remove a brackets after ArrayList.

3 Comments

@lhor I modified the post. now it contains the code.
@lhor i modified as you said. It compiling without error. but i can't understand your solution.
In Java if you want create object you should write name of class and put brackets. If you want to put the generic type, you should put <> brackets after name of class and after that put round brackets. If you write for example Array arr = new ArrayList()<BookInfo();> it's mean you create ArrayList object and part of code <BookInfo();> is not valide, because you close the brackets near a ArrayList.
2
 ArrayList<BookInfo> bookList = new ArrayList<BookInfo>();

But please see @IhorYatsenko's answer if it solves your problem.

1 Comment

FYI (probably for future readers at this point), you should not edit code into others' posts (or change code which an asker has provided in their question). Instead, give attribution (preferably with a link) and write your own answer as you have done (although this is code-only and really should include an explanation of how it helps).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.