import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
public class MyLinkedList
{
private int bookNo = 1;
private List<Book> list = new LinkedList<Book>();
//Add a book
public void add()
{
Book book = new Book();
String bookName = JOptionPane.showInputDialog("Please enter the book name:");
boolean flag = true;
float price = 0f;
while(flag)
{
try
{
//Accept the user input prices
price = Float.parseFloat(JOptionPane.showInputDialog("Please enter the price:"));
flag = false;
}catch(Exception e)
{
//When the client input is not a number when given the appropriate prompt, and returns to continue to enter the
System.out.println("Your input error, please re-enter.");
}
}
String author = JOptionPane.showInputDialog("Please enter the author:");
String description = JOptionPane.showInputDialog("Please enter a description:");
String createDate = JOptionPane.showInputDialog("Please enter the book's publication time:");
book.setBookNo(this.bookNo);
book.setBookName(bookName);
book.setPrice(price);
book.setAuthor(author);
book.setDescription(description);
book.setCreateDate(createDate);
list.add(book);
JOptionPane.showMessageDialog(null, "Added successfully");
bookNo ++;
}
/**
* Delete a book
*/
@SuppressWarnings("unchecked")
public void delete()
{
//Is there any list to determine where the record books, if not do not need to perform delete operations
if(list.size() > 0)
{
int no = 0;
try
{
no = Integer.parseInt(JOptionPane.showInputDialog(this.common()+"\nPlease enter the number you want to remove the book:"));
}catch(Exception e)
{
JOptionPane.showMessageDialog(null,"You enter the wrong number");
}
for(Iterator<Book> iter = list.iterator();iter.hasNext();)
{
Book book = iter.next();
//Enter the client number is equal to the list of books in the Book Number in the list when you delete the corresponding records
if(book.getBookNo() == no)
{
iter.remove();
JOptionPane.showMessageDialog(null, "Deleted successfully");
}
}
}else
{
JOptionPane.showMessageDialog(null,"No book could be deleted");
}
}
//Show list of all the books records
public void getBooks()
{
if(list.size()>0)
{
//Here call a common method to return that user want to show the book record.
JOptionPane.showMessageDialog(null,this.common());
}else
{
JOptionPane.showMessageDialog(null,"You do not add books");
}
}
/**Search Books
* According to the book name or author to find one or more books
*/
public void search()
{
//Receiving client input to find information
String nameOrAuthor = JOptionPane.showInputDialog("Please enter the name of the book or the author's name:");
for(Book book : list)
{
//When the list in keeping with user inquiries book record shows that all the information about the books
if(nameOrAuthor.equals(book.getBookName()) || nameOrAuthor.equals(book.getAuthor()))
{
JOptionPane.showMessageDialog(null,"BNO: " +book.getBookNo()+
"\nBookName: "+book.getBookName()+
"\nPrice: RM" + book.getPrice()+
"\nAuthor: "+book.getAuthor()+
"\nDescription: "+book.getDescription()+
"\nIssued date: "+book.getCreateDate());
}
}
}
/**
* This is a common method
* Will list all the records of splicing into a string to return
* @return String
*/
public String common()
{
Iterator<Book> itera = list.iterator();
String abc = "";
while(itera.hasNext())
{
Book book = itera.next();
abc = "BNO: " +book.getBookNo()+
"\nBookName: "+book.getBookName()+
"\nPrice: RM" + book.getPrice()+
"\nAuthor: "+book.getAuthor()+
"\nDescription: "+book.getDescription()+
"\nIssued date: "+book.getCreateDate();
}
return abc;
}
}