You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
768 B

  1. /*
  2. * Written by : Bin Hong Lee
  3. * Last edited : 5/31/2016
  4. */
  5. import java.util.*;
  6. class BookFactory
  7. {
  8. private List<Book> books = new ArrayList<Book>();
  9. private int id;
  10. public BookFactory()
  11. {
  12. id = 0;
  13. }
  14. public Book newBook(String title)
  15. {
  16. Book temp = new Book(title, id);
  17. books.add(temp);
  18. id++;
  19. return temp;
  20. }
  21. public Book getBook(int index)
  22. {
  23. try
  24. {
  25. return books.get(index);
  26. }
  27. catch (Exception e)
  28. {
  29. throw new NullPointerException();
  30. }
  31. }
  32. public Book getBook(String title)
  33. {
  34. for (int i = 0; i < books.size(); i++)
  35. {
  36. Book temp = books.get(i);
  37. if(temp.getTitle() == title)
  38. {
  39. return temp;
  40. }
  41. }
  42. throw new NullPointerException();
  43. }
  44. }