Media Rental System
Design and implement Java program 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
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