+1 (315) 557-6473 

Media Rental System Homework Solution with Python


Media Rental System

Design and implement a Java program to complete a Java assignment as follows:

1) Media hierarchy:

  • Create Media, EBook, MovieDVD, and MusicCD
  • Add an attribute to the Media class to store indications when the media object is rented versus available. Add code to a constructor and create get and set methods as appropriate.
  • Add any additional constructors and methods needed to support the below functionality

2) Design and implement Manager class which:

  • stores a list of Media objects
  • has the functionality to load Media objects from files
  • creates/updates Media files
  • has the functionality to add new Media objects to its Media list
  • has the functionality to find all media objects for a specific title and returns that list
  • has the functionality to rent Media based on id (updates rental status on media, updates file, returns rental fee)

3) Design and implement MediaRentalSystem which has the following functionality:

  • the user interface which is either menu-driven through console commands or GUI buttons or menus. Look at the bottom of this project file for a sample look and feel.
  • selection to load Media files from a given directory (user supplies directory)
  • selection to find a media object for a specific title value (user supplies title and should display to the user the media information once it finds it - should find all media with that title)
  • selection to rent a media object based on its id value (user supplies id and should display rental fee value to the user)
  • selection to exit the program

4) Program should throw and catch Java built-in and user-defined exceptions as appropriate

5) Your classes must be coded with correct encapsulation: private/protected attributes, get methods and set methods, and value validation

6) There should be appropriate polymorphism: overloading, overriding methods, and dynamic binding

7) Program should take advantage of the inheritance properties as appropriate

Solution:

1.

Media:

import java.util.HashMap; import java.util.Map; public abstract class Media { private final int id; private final String title; private final int year; private boolean available; public Media(int id, String title, int year) { this.id = id; this.title = title; this.year = year; this.available = true; } public int getId() { return id; } public String getTitle() { return title; } public int getYear() { return year; } public boolean isAvailable() { return available; } public void setAvailable(boolean available) { this.available = available; } @Override public String toString() { return "Media{" + "id=" + id + ", title='" + title + '\'' + ", year=" + year + ", available=" + available + '}'; } public abstract String serialized(); public static Media parse(String line) { String[] parts = line.split(";"); Media media; switch (parts[0]) { case "EBook": media = new EBook(Integer.parseInt(parts[1]), parts[2], Integer.parseInt(parts[3]), Integer.parseInt(parts[4])); break; case "MovieDVD": media = new MovieDVD(Integer.parseInt(parts[1]), parts[2], Integer.parseInt(parts[3]), Double.parseDouble(parts[4])); break; case "MusicCD": media = new MusicCD(Integer.parseInt(parts[1]), parts[2], Integer.parseInt(parts[3]), parts[4]); break; default: return null; } media.setAvailable(Boolean.parseBoolean(parts[5])); return media; } }

E-Book:

public class EBook extends Media { private final int chapters; public EBook(int id, String title, int year, int chapters) { super(id, title, year); this.chapters = chapters; } public int getChapters() { return chapters; } @Override public String serialized() { return "EBook;" + getId() + ";" + getTitle() + ";" + getYear() + ";" + chapters + ";" + isAvailable(); } @Override public String toString() { return "EBook [" + "id=" + getId() + ", title=" + getTitle() + ", year=" + getYear() + ", chapters=" + chapters + ", available=" + isAvailable() + ']'; } }

Movie DVD:

public class MovieDVD extends Media { private final double size; public MovieDVD(int id, String title, int year, double size) { super(id, title, year); this.size = size; } public double getSize() { return size; } @Override public String toString() { return "MovieDVD [" + "id=" + getId() + ", title=" + getTitle() + ", year=" + getYear() + ", size=" + size + "MB" + ", available=" + isAvailable() + ']'; } @Override public String serialized() { return "MovieDVD;" + getId() + ";" + getTitle() + ";" + getYear() + ";" + size + ";" + isAvailable(); } }

Music CD:

public class MusicCD extends Media { private final String duration; public MusicCD(int id, String title, int year, String duration) { super(id, title, year); this.duration = duration; } public String getDuration() { return duration; } @Override public String toString() { return "MusicCD [" + "id=" + getId() + ", title=" + getTitle() + ", year=" + getYear() + ", duration=" + duration + ", available=" + isAvailable() + ']'; } @Override public String serialized() { return "MusicCD;" + getId() + ";" + getTitle() + ";" + getYear() + ";" + duration + ";" + isAvailable(); } }

2.

Manager:

import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Manager { private List mediaList; public Manager() { mediaList = new ArrayList<>(); } public void loadFromFile(String fileName) throws IOException { try(Scanner scanner = new Scanner(new File(fileName))) { mediaList.clear(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); Media media = Media.parse(line); if (media != null) { mediaList.add(media); } else { System.err.println("Can not parse media from line: " + line); } } } } public List findByTitle(String title) { return mediaList.stream().filter(m -> m.getTitle().equals(title)).collect(Collectors.toList()); } public Boolean tryRentById(int id) { Media media = mediaList.stream().filter(m -> m.getId() == id).findAny().orElse(null); if (media == null) { return null; } if (media.isAvailable()) { media.setAvailable(false); return true; } else { return false; } } }

3.

Media Rental System:

import java.io.IOException; import java.util.*; /** * Main driver application class, which is responsible for user interaction with user via console * @author * @date */ public class MediaRentalSystem { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { boolean isOver = false; Manager manager = new Manager(); while (!isOver) { int choice = getMenuOption(scanner); System.out.println(); switch (choice) { case 1: try { String filename = getStringChoice(scanner, "Enter path (directory) where to load from: "); manager.loadFromFile(filename); } catch (IOException e) { System.out.println("File cannot be opened: Could not load, no such directory"); } break; case 2: String title = getStringChoice(scanner, "Enter the title: "); List collection = manager.findByTitle(title); if (collection.isEmpty()) { System.out.println("There is no media with this title: " + title); } else { for (Media media : manager.findByTitle(title)) { System.out.println(media); } } break; case 3: int id = getIntChoice(scanner, "Enter the id: ", null); Boolean rent = manager.tryRentById(id); if (rent == null) { System.out.println("The media object id=" + id + " is not found"); } else { if (rent) { System.out.println("Media was successfully rented. Rental fee = $2.00"); } else { System.out.println("Media is already rented"); } } break; case 9: isOver = true; break; default: throw new IllegalStateException(); } System.out.println(); } } System.out.println("Thank you for using the program. Goodbye!"); } private static int getMenuOption(Scanner scanner) { String builder = "Welcome to Media Rental System" + System.lineSeparator() + "1: Load Media objects..." + System.lineSeparator() + "2: Find Media object..." + System.lineSeparator() + "3: Rent Media object..." + System.lineSeparator() + "9: Quit" + System.lineSeparator(); System.out.println(builder); return getIntChoice(scanner, "Enter your selection: ", Arrays.asList(1, 2, 3, 9)); } private static int getIntChoice(Scanner scanner, String prompt, Collection options) { while (true) { System.out.print(prompt); try { int choice = Integer.parseInt(scanner.nextLine()); if (options == null || options.contains(choice)) { return choice; } else throw new Exception(); } catch (Exception ignored) { System.out.println("Invalid input. Please, try again."); System.out.println(); } } } private static String getStringChoice(Scanner scanner, String prompt) { while (true) { System.out.print(prompt); String choice = scanner.nextLine().trim(); if (!choice.isEmpty()) { return choice; } System.out.println("Invalid input. Please, try again."); System.out.println(); } } }