简体   繁体   中英

Calling deleteBook() method to remove all items from ArrayList

I want to remove all items from ArrayList. When i call deletBook() method from main class the ArrayList scope problem appears and i don't able to delete total items from list. addBook() and searchBook methods successfully run just problem in deletBook().

import java.util.*;
import javax.swing.JOptionPane;

public class BookInfoNew {
    private static String isbn;
    private static String bookName;
    private static String authorName;
    public static int totalBooks = 0;
    public static List<String> list;

    //default constructor
    public BookInfoNew() {
        list = new ArrayList<String>(); //create ArrayLis
    }
    //add book method
    public void addBook() {

        //show Input dialogue box to take input
        String isbn = JOptionPane.showInputDialog("Enter ISBN");
        String bookName = JOptionPane.showInputDialog("Enter Book name");
        String authorName = JOptionPane.showInputDialog("Enter Author name");

        //add books data to ArrayList
        list.add(isbn);
        list.add(bookName);
        list.add(authorName);
        totalBooks++; //increment Total books

        //show notification after successful added book
        JOptionPane.showMessageDialog(null, "Book Name:  " +bookName + "  added successful\n" + "Total Books: " +totalBooks);

    }
    //delete book method
    public void deletBook(Collection c) {
        String del = JOptionPane.showInputDialog("Enter book name to delete"); //show input dialogue

        Iterator<String> iter = c.iterator(); //going from one item to next in list

        //remove items from list
        while(iter.hasNext()) {
            iter.next();
            list.remove(del);
        }
        totalBooks--; //decrement total books
    }
}

//Main class
import java.util.*;
import javax.swing.JOptionPane;;

public class BookInfoNewMain {
    public static void main(String[] args) {
        BookInfoNew obj = new BookInfoNew();

        obj.addBook();
        obj.searchBook();
obj.deletBook(list);
    }
}

For better readeable code, you can create a BookInfoNews object and create the methods in other class, then just do your stuff and it's done.

package test;
import java.util.*;
import javax.swing.JOptionPane;

public class BookInfoMethod {
public static int totalBooks = 0;
public static List<BookInfoNews> list = new ArrayList<BookInfoNews>();      //create ArrayLis;

//default constructor
public BookInfoMethod() {
}
//add book method
public void addBook() {

    //show Input dialogue box to take input
    String isbn = "test";
    String bookName = "test";
    String authorName = "test";

    //add books data to ArrayList
    BookInfoNews newBook = new BookInfoNews(isbn, bookName, authorName);
    list.add(newBook);
    totalBooks++; //increment Total books

    //show notification after successful added book
    JOptionPane.showMessageDialog(null, "Book Name:  " +bookName + "  added successful\n" + "Total Books: " +totalBooks);

}
//delete book method
public void deletBook() {
    String del = "test";

   for (int i = 0; i < this.list.size() ; i++){
       if(del.equals(this.list.get(i).getBookName())){
           this.list.remove(i);
       }
   }
    totalBooks--; //decrement total books
    JOptionPane.showMessageDialog(null,"Total Books: " +totalBooks);
}

public BookInfoNews searchMethod(String bookName){
if(bookName == null){
    return null;
}
for(BookInfoNews tmp : this.list){
    if(bookName.equals(tmp.getBookName()))
        return tmp;
}
return null;
}
}

class BookInfoNews{
    private String isbn;
    private String bookName;
    private String authorName;
//default constructor
public BookInfoNews(String isbn,String bookName,String authorName) {
    this.isbn = isbn;
    this.bookName = bookName;
    this.authorName = authorName;
}
public String getIsbn() {
    return isbn;
}
public void setIsbn(String isbn) {
    this.isbn = isbn;
}
public String getBookName() {
    return bookName;
}
public void setBookName(String bookName) {
    this.bookName = bookName;
}
public String getAuthorName() {
    return authorName;
}
public void setAuthorName(String authorName) {
    this.authorName = authorName;
}


}

class BookInfoNewMain {
public static void main(String[] args) {
    BookInfoMethod obj = new BookInfoMethod();
    obj.addBook();
    obj.deletBook();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM