package btl; import java.util.*; import java.io.*; /** * BT Library Item Database using a text file. * Uses the Singleton Design Pattern! */ /** * Item Database using a text file. * Uses the Singleton Design Pattern! */ public class FileItemDB implements ItemDB { // The database filename. private static String datafile = "btl\\Items\\Items.txt"; // The hashmap used to store the Item objects, keyed by the Item id. private HashMap map; // The private reference to the one and only instance of the database. private static ItemDB uniqueInstance = null; /** * Returns a reference to the single instance. * Creates the instance if it does not yet exist. * (This is called lazy instantiation.) */ public static ItemDB instance() { if(uniqueInstance == null) uniqueInstance = new FileItemDB(); return uniqueInstance; } /** * The private constructor! */ private FileItemDB() { map = new HashMap(); open(); } /* * Opens the database. * In this case, opens the text data file. Parses each * line and adds an entry to the hash map using the id as * the key and the Item object as the value. */ public void open () { try { FileReader fr = new FileReader (datafile); BufferedReader br = new BufferedReader (fr); String line; // Skip header line! line = br.readLine(); while ((line = br.readLine()) != null) { Item Item = parseItemRecord (line); map.put(Item.getId(), Item); } fr.close(); } catch (Exception e) { System.err.println (e); } } /** * Closes the database. * In this case, does nothing. */ public void close() { } /** * Parses the text file record. * Fields are tab separated. */ private Item parseItemRecord (String str) throws IOException { StringTokenizer st = new StringTokenizer (str, "\t"); String id = st.nextToken(); String title = st.nextToken(); String author = st.nextToken(); String genre = st.nextToken(); String binding = st.nextToken(); String strPrice = st.nextToken(); double price = Double.parseDouble(strPrice); String availability = st.nextToken(); return new Item (id, title, author, genre, binding, price, availability); } /** * Search by key. */ public Item searchByKey(String key) { return (Item) map.get(key); } /** * Search by title. */ public Item[] searchTitle(String ss) { ArrayList l = new ArrayList(); Set s = map.keySet(); Iterator iter = s.iterator(); while (iter.hasNext()) { Item b = (Item) map.get((String)iter.next()); String title = b.getTitle(); if (title.indexOf(ss) != -1) l.add(b); } Item[] b = new Item[l.size()]; return (Item[]) l.toArray(b); } /** * Search by author. */ public Item[] searchAuthor(String ss) { ArrayList l = new ArrayList(); Set s = map.keySet(); Iterator iter = s.iterator(); while (iter.hasNext()) { Item b = (Item) map.get((String)iter.next()); String author = b.getAuthor(); if (author.indexOf(ss) != -1) l.add(b); } Item[] b = new Item[l.size()]; return (Item[]) l.toArray(b); } /** * Search by genre. */ public Item[] searchGenre(String ss) { ArrayList l = new ArrayList(); Set s = map.keySet(); Iterator iter = s.iterator(); while (iter.hasNext()) { Item b = (Item) map.get((String)iter.next()); String genre = b.getGenre(); if (genre.equalsIgnoreCase(ss)) l.add(b); } Item[] b = new Item[l.size()]; return (Item[]) l.toArray(b); } }