package dk.hansen; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; public class SimpleBookManager implements BookManagerIF { private static HashMap books = new HashMap(); static { System.out.println("SimpleBookManager created - static code"); // Create some Books for testing Book book; String id; String title; String author; int year; id = "ID30"; title = "The Tempest"; author = "William Shakespeare"; year = 1611; book = new Book(id, title, author, year); books.put(id, book); id = "ID31"; title = "A Midsummer Night's Dream"; author = "William Shakespeare"; year = 1595; book = new Book(id, title, author, year); books.put(id, book); } public void createBook(String id, String title, String author, int year) throws DAOException { Book book = (Book)books.get(id); if (book != null) throw new DAOException("Id " + id + " is already used"); book = new Book(id, title, author, year); books.put(id, book); } public void updateBook(String id, String title, String author, int year) throws DAOException { Book book = (Book)books.get(id); if (book == null) throw new DAOException("Id " + id + " was not found"); book.setTitle(title); book.setAuthor(author); book.setYear(year); } public void deleteBook(String id) throws DAOException { Book book = (Book)books.get(id); if (book == null) throw new DAOException("Id " + id + " was not found"); books.remove(id); } public Book getBook(String id) throws DAOException { Book book = (Book)books.get(id); return book; } public Collection getAll() { return books.values(); } public Collection findBookTitle(String title) { Collection hits = new ArrayList(); Collection c = books.values(); for (Iterator it = c.iterator(); it.hasNext();) { Book book = (Book)it.next(); if (book.getTitle().toUpperCase().indexOf(title.toUpperCase()) > -1) { hits.add(book); } } return hits; } public Collection findBookAuthor(String author) { Collection hits = new ArrayList(); Collection c = books.values(); for (Iterator it = c.iterator(); it.hasNext();) { Book book = (Book)it.next(); if (book.getAuthor().toUpperCase().indexOf(author.toUpperCase()) > -1) { hits.add(book); } } return hits; } public Collection findBookYear(int year) { Collection hits = new ArrayList(); Collection c = books.values(); for (Iterator it = c.iterator(); it.hasNext();) { Book book = (Book)it.next(); if (book.getYear() == year) { hits.add(book); } } return hits; } }