/Imports the classes necessary to run the Controller class.
import java.awt.ItemSelectable;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JOptionPane;
//Controller class connects the librarian to the rest of the program.
public class Controller
{
//Creates Hashmaps for contents and borrowers.
private HashMap <String,Item> contents = new HashMap <String,Item> ();
private HashMap <String,Borrower> borrowers = new HashMap <String, Borrower> ();
// Controller Constructor.
Controller()
{
//Declares Instances.
Book book;
Music music;
//Creates borrower accounts.
Borrower b = new Borrower();
b.setID("001");
b.setName("D. Manson");
borrowers.put(b.getID(),b);
b = new Borrower();
b.setID("002");
b.setName("V. Tran");
borrowers.put(b.getID(),b);
b = new Borrower();
b.setID("003");
b.setName("A. Dumas");
borrowers.put(b.getID(),b);
// Initialize catalog records
book = new Book();
book.setCode("HPDH");
book.setTitle("Harry Potter and the Deathly Hallows");
book.setAuthor("J.K.Rowling");
book.setSubject("Wizarding");
book.setPages("759");
book.setQuantity(3);
book.setImgURL("./images/hpdh.jpg");
contents.put(book.getCode(),book);
book = new Book();
book.setCode("CSH");
book.setTitle("The Complete Sherlock Holmes");
book.setAuthor("Sir Arthur Conan Doyle");
book.setSubject("Who Done It");
book.setPages("1122");
book.setQuantity(2);
book.setImgURL("./images/sherlock.jpg");
contents.put(book.getCode(),book);
book = new Book();
book.setCode("PL");
book.setTitle("Pirate Latitudes");
book.setAuthor("Michael Crichton");
book.setSubject("Adventure");
book.setPages("312");
book.setQuantity(1);
book.setImgURL("./images/pirate.jpg");
contents.put(book.getCode(),book);
music = new Music();
music.setCode("HWGA");
music.setTitle("Here We Go Again");
music.setArtist("Wynton Marsalis");
music.setGenre("Blues");
music.setFormat("Audio CD");
music.setQuantity(3);
music.setImgURL("./images/marsalis.jpg");
contents.put(music.getCode(), music);
music = new Music();
music.setCode("MC");
music.setTitle("McCartney");
music.setArtist("Paul McCartney");
music.setGenre("Rock");
music.setFormat("MP3 Download");
music.setQuantity(2);
music.setImgURL("./images/mccartney.jpg");
contents.put(music.getCode(), music);
music = new Music();
music.setCode("G");
music.setTitle("Guilt");
music.setArtist("Nero");
music.setGenre("Dubstep");
music.setFormat("MP3 Download");
music.setQuantity(4);
music.setImgURL("./images/nero.jpg");
contents.put(music.getCode(), music);
music = new Music();
music.setCode("S");
music.setTitle("Strobe");
music.setArtist("Deadmau5");
music.setGenre("House");
music.setFormat("MP3 Download");
music.setQuantity(1);
music.setImgURL("./images/deadmau5.jpg");
contents.put(music.getCode(), music);
music = new Music();
music.setCode("GT");
music.setTitle("Group Therapy");
music.setArtist("Above and Beyond");
music.setGenre("Trance");
music.setFormat("Audio CD");
music.setQuantity(2);
music.setImgURL("./images/grouptherapy.jpg");
contents.put(music.getCode(), music);
}
//Sorts the catalog items alphabetically.
String getCatalog()
{
//Sorts items by key values.
Vector <String> v = new Vector <String> (contents.keySet());
Collections.sort(v);
// Returns the sorted results.
Item myItem;
String msg = "";
for (Enumeration <String> e = v.elements(); e.hasMoreElements();)
{
String ID = (String) e.nextElement();
myItem = contents.get(ID);
msg += myItem.examine() + "\n \n";
}
return msg;
}
//Checks out an item.
void checkout(String borrowerID, String itemIDIn, String itemID,
String copyID, String dateOut, String dateDue)
{
try
{
//Lookup borrower, lookup item, add borrower to copy of item, and update borrower's record
Borrower borrower;
Item item;
borrower = borrowers.get(borrowerID);
item = contents.get(itemID);
item.addBorrower(copyID, borrowerID);
borrower.addItemOut(itemIDIn, dateOut, dateDue);
//Update item count by subtracting 1 from the existing quantity.
contents.get(itemID).quantity--;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"HI","CPP Library "
+ "Lending System", JOptionPane.INFORMATION_MESSAGE);
}
}
//Returns an item.
void checkIn(String itemIDIn, String itemID, String copyID, String dateIn)
{
try
{
//Lookups item, find out who borrowed it, and update borrower's record
Borrower borrower;
Item item;
item = contents.get(itemID);
borrower = borrowers.get(item.getBorrowerID(copyID));
borrower.addItemIn(itemIDIn, dateIn);
//Update item count by adding 1 to the existing quantity.
contents.get(itemID).quantity++;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Unable to process request",
"CPP Library Lending System", JOptionPane.INFORMATION_MESSAGE);
}
}
//Returns the borrower's history.
String getBorrowerHistory(String anID)
{
try
{
return borrowers.get(anID).getHistory();
}
catch (Exception e)
{
return "";
}
}
//Returns the Image associated with the item.
String getImgURL(String itemID)
{
String item = contents.get(itemID).getImgURL();
return item;
}
//Checks if the item has a quantity above 0.
boolean isItemAvailable(String itemID)
{
Item item = contents.get(itemID);
boolean result;
if (item.quantity > 0)
result = true;
else
result = false;
return result;
}
//Checks if the borrower is valid.
boolean isValidBorrower(String anID)
{
return borrowers.containsKey(anID);
}
//Checks if item is in the catalog.
boolean isValidItem(String itemID)
{
return contents.containsKey(itemID);
}
//Checks if the date is in a valid format.
boolean isValidDate (String aDate)
{
boolean result = false;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
sdf.setLenient(false);
try
{
sdf.parse(aDate);
return true;
}
catch(Exception e)
{
}
return result;
}
}