|
The SimpleDVDManager
Our first implementation of this interface will be a manager
that stores the DVDs in a HashMap. This
implementation will only be used for testing so we'll not
persist the data. When the manager is loaded we'll create and
store two example DVDs in the
HashMap:
package dk.hansen;
import java.util.*;
public class SimpleDVDManager implements DVDManagerIF {
private static HashMap dvds = new HashMap();
static {
// Create two DVDs for testing
String id; DVD dvd;
id = "ID10";
dvd = new DVD(id, "Troy");
dvds.put(id, dvd);
id = "ID11";
dvd = new DVD(id, "Peter Pan");
dvds.put(id, dvd);
}
public void createDVD(String id, String title) throws DAOException {
DVD dvd = (DVD)dvds.get(id);
if (dvd != null) throw new DAOException("Id " + id + " is already used");
dvd = new DVD(id, title);
dvds.put(id, dvd);
}
public void updateDVD(String id, String title) throws DAOException {
DVD dvd = (DVD)dvds.get(id);
if (dvd == null) throw new DAOException("Id " + id + " was not found");
dvd.setTitle(title);
}
public void deleteDVD(String id) throws DAOException {
DVD dvd = (DVD)dvds.get(id);
if (dvd == null) throw new DAOException("Id " + id + " was not found");
dvds.remove(id);
}
public DVD getDVD(String id) {
DVD dvd = (DVD)dvds.get(id);
return dvd;
}
public Collection getAll() {
return dvds.values();
}
public Collection findDVDTitle(String title) {
Collection hits = new ArrayList();
Collection c = dvds.values();
for (Iterator it = c.iterator(); it.hasNext();) {
DVD dvd = (DVD)it.next();
if (dvd.getTitle().toUpperCase().indexOf(title.toUpperCase()) > -1) {
hits.add(dvd);
}
}
return hits;
}
}
The create, update and delete methods may throw an exception, as
specified in table 1.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|