Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This will be switching out a menu-driven interface in Java with a GUI, with no JComponents, in Java. The menu-driven interface code will be listed

This will be switching out a menu-driven interface in Java with a GUI, with no JComponents, in Java. The menu-driven interface code will be listed below.

Replace the menu-driven interface with a GUI (NO JComponents). The GUI(NO JComponents) should look like the image below and support the following operations:

Inserting a new media item

Marking an item as on loan

Marking an item as returned

Removing a media item

image text in transcribed

Support two different list orderings: alphabetically by title and chronologically by return date. The user should be able to switch between these two orderings by clicking on the appropriate button in the GUI. See above for an example of title-based ordering and below for an example of date-based ordering.

Menu-Driven Interface Code

public class ProjectInterface {

private static Scanner in = new Scanner(System.in);

public static int getMenuOption() { int option = 0; String inputString = "";

while (option 6) {

System.out.println(""); System.out.println("1. List the items"); System.out.println("2. Add a new item"); System.out.println("3. Mark an item as on loan"); System.out.println("4. Mark an item as returned"); System.out.println("5. Remove an item"); System.out.println("6. Quit"); System.out.print("What would you like to do? ");

try { inputString = in.nextLine().trim(); option = Integer.parseInt(inputString); } catch (NumberFormatException e) { // intentionally empty }

if (option 6) { System.out.println("Error: " + option + " is not a valid option"); } }

System.out.println(""); return option; }

private static String getInput(String prompt) { System.out.print(prompt + " "); return in.nextLine().trim(); }

private static Date getDate(String prompt) throws Exception { System.out.print(prompt + " "); String dateString = in.nextLine().trim();

try { Date loanedOn = new SimpleDateFormat( "MM-dd-yyyy").parse(dateString); return loanedOn; } catch (ParseException e) { throw new Exception(dateString + " is not a valid date."); } }

public static void main(String[] args) {

MediaCollection collection = new MediaCollection();

try { collection.readCollection(); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("Continuing with a fresh collection"); }

int choice = getMenuOption();

while (choice != 6) { try { switch (choice) { case 1: collection.listItems(); break; case 2: collection.addItem(getInput("What is the item's title?"), getInput("What is the item's format (DVD, X-box, etc.)?")); break; case 3: MediaItem toLoan = collection.retrieveItem( getInput("What is the item's title?")); if (collection.isLoanable(toLoan)) { collection.loanItem(toLoan, getInput("Who did you loan " + toLoan.getTitle() + " to?"), getDate("What date did you loan it the them on? (MM-DD-YYYY)")); } else { System.out.println(toLoan.getTitle() + " is already on loan."); } break; case 4: MediaItem toReturn = collection.retrieveItem( getInput("What is the item's title?")); collection.returnItem(toReturn); break; case 5: MediaItem toRemove = collection.retrieveItem( getInput("What is the item's title?")); collection.removeItem(toRemove); break; } } catch (Exception e) { System.out.println(e.getMessage()); } choice = getMenuOption(); }

try { collection.storeCollection(); } catch (Exception e) { System.out.println(e.getMessage()); } } }

---------------------------------------------------------------------------------------------------

public class MediaItem implements Serializable { private String title; private String format; private String loanedTo; private Date dateLoaned; public MediaItem(String title, String format) { this.title = title; this.format = format; } public void loan(String loanedTo, Date loanedOn) { this.loanedTo = loanedTo; this.dateLoaned = loanedOn; } public void returnItem() { this.loanedTo = null; this.dateLoaned = null; } public String getTitle() { return title; } @Override public String toString() { String response = title + " - " + format; if (loanedTo != null) { response += " (" + loanedTo + " on " + dateLoaned + ")"; } response = response.replace("00:00:00 EDT ", ""); return response; }

@Override public int hashCode() { int hash = 7; hash = 31 * hash + Objects.hashCode(this.title); return hash; }

@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final MediaItem other = (MediaItem) obj; if (!Objects.equals(this.title, other.title)) { return false; } return true; } public String getLoanedTo() { return loanedTo; } }

----------------------------------------------------------------------------------

public class MediaCollection {

private ArrayList collection = new ArrayList();

public MediaItem retrieveItem(String title) throws Exception { MediaItem theItem = new MediaItem(title, "temp"); for (MediaItem item : collection) { if (item.equals(theItem)) { return item; } }

throw new Exception("There is no item called " + title + " in the collection."); } public void addItem(String title, String format) throws Exception { MediaItem newItem = new MediaItem(title, format); if (!collection.contains(newItem)) { collection.add(newItem); } else { throw new Exception("An item with the title " + title + " already exists"); } }

public void removeItem(MediaItem theItem) throws Exception { if (collection.contains(theItem)) { collection.remove(theItem); } else { throw new Exception("There is no item called " + theItem.getTitle() + " in the collection."); } }

public boolean isLoanable(MediaItem theItem) { return (theItem.getLoanedTo() == null); } public void loanItem(MediaItem theItem, String loanedTo, Date loanedOn) throws Exception { if (!isLoanable(theItem)) throw new Exception(theItem.getTitle() + " is already on loan."); theItem.loan(loanedTo, loanedOn); }

public void returnItem(MediaItem theItem) throws Exception {

if (theItem.getLoanedTo() == null) { throw new Exception(theItem.getTitle() + " is not on loan"); } else { theItem.returnItem(); } }

public void listItems() { System.out.println("Your Media Collection"); for (MediaItem item : collection) { System.out.println("\t" + item); } System.out.println(""); }

public void storeCollection() throws Exception { try (ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(new File("media.dat")));) { oos.writeObject(collection); } catch (Exception e) { throw new Exception("Unable to save the updates to the collection."); } }

public void readCollection() throws Exception {

File f = new File("media.dat");

if (f.exists()) { try (ObjectInputStream ois = new ObjectInputStream( new FileInputStream(f));) { collection = (ArrayList) ois.readObject(); } catch (Exception e) { System.out.println("Unable to load the collection."); } } } public int getSize() { return collection.size(); } }

Media Collection Avengers DVD Days of Future Past DVD Guardians of the Galaxy DVD (Mike on Sun May 15 2016) Iron Man DVD SpiderMan DVD (Jason on Fri Aug 21 2015) Thor DVD Title: Format: Add Remove Return Loaned To Loaned On: Loan Sort By title By date loaned Media Collection Avengers DVD Days of Future Past DVD Guardians of the Galaxy DVD (Mike on Sun May 15 2016) Iron Man DVD SpiderMan DVD (Jason on Fri Aug 21 2015) Thor DVD Title: Format: Add Remove Return Loaned To Loaned On: Loan Sort By title By date loaned

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions