package dk.hansen; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; public class SimpleVhsManager implements VhsManagerIF { private static HashMap vhss = new HashMap(); static { System.out.println("SimpleVhsManager created - static code"); // Create some Vhss for testing Vhs vhs; String id; int year; String title; int tapeLength; id = "ID33"; year = 1985; title = "Who knows this movie?"; tapeLength = 240; vhs = new Vhs(id, year, title, tapeLength); vhss.put(id, vhs); id = "ID34"; year = 1999; title = "I know this movie!"; tapeLength = 120; vhs = new Vhs(id, year, title, tapeLength); vhss.put(id, vhs); id = "ID35"; year = 2001; title = "I know it as well!"; tapeLength = 140; vhs = new Vhs(id, year, title, tapeLength); vhss.put(id, vhs); } public void createVhs(String id, int year, String title, int tapeLength) throws DAOException { Vhs vhs = (Vhs)vhss.get(id); if (vhs != null) throw new DAOException("Id " + id + " is already used"); vhs = new Vhs(id, year, title, tapeLength); vhss.put(id, vhs); } public void updateVhs(String id, int year, String title, int tapeLength) throws DAOException { Vhs vhs = (Vhs)vhss.get(id); if (vhs == null) throw new DAOException("Id " + id + " was not found"); vhs.setYear(year); vhs.setTitle(title); vhs.setTapeLength(tapeLength); } public void deleteVhs(String id) throws DAOException { Vhs vhs = (Vhs)vhss.get(id); if (vhs == null) throw new DAOException("Id " + id + " was not found"); vhss.remove(id); } public Vhs getVhs(String id) throws DAOException { Vhs vhs = (Vhs)vhss.get(id); return vhs; } public Collection getAll() { return vhss.values(); } public Collection findVhsTitle(String title) { Collection hits = new ArrayList(); Collection c = vhss.values(); for (Iterator it = c.iterator(); it.hasNext();) { Vhs vhs = (Vhs)it.next(); if (vhs.getTitle().toUpperCase().indexOf(title.toUpperCase()) > -1) { hits.add(vhs); } } return hits; } public Collection findVhsYear(int year) { Collection hits = new ArrayList(); Collection c = vhss.values(); for (Iterator it = c.iterator(); it.hasNext();) { Vhs vhs = (Vhs)it.next(); if (vhs.getYear() == year) { hits.add(vhs); } } return hits; } }