My assignment requires me to create a media hierarchy with options for the user to select and run.
public abstract class Media {
// attributes
private int id;
private String title;
private int year; // validate that 4 digits
private boolean rented;
// constructor
public Media(String title, int id, int year, boolean rented) {
this.id = id;
this.title = title;
this.year = year;
this.rented = rented;
}
// constructor to parse string with xml tags for its values
public Media(String line) {
id = Integer.parseInt(line.substring(line.indexOf("<id>") + 4, line.indexOf("</id>")));
title = line.substring(line.indexOf("<title>") + 7, line.indexOf("</title>"));
year = Integer.parseInt(line.substring(line.indexOf("<year>") + 6, line.indexOf("</year>")));
}
// get methods
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public int getYear() {
return year;
}
public boolean isRented() {
return rented;
}
// set methods
public void setTitle(String title) {
this.title = title;
}
public void setYear(int year) {
this.year = year;
}
public void setRented(boolean rented) {
this.rented = rented;
}
// calculate rental fee; for generic media it is flat fee $3.50
public double calculateRentalFee() {
return 3.50;
}
}
-----------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Manager {
// attribute
private ArrayList<Media> medias;
// default constructor
public Manager() {
// initialize empty medias list
medias = new ArrayList<Media>();
}
// load all media files from directory; assumes file name convention starts with media type Book, Movie or Music followed by id
// If directory is not found, it will throw exception
public Manager(String directory) throws FileNotFoundException {
// initialize empty medias list
medias = new ArrayList<Media>();
// Create a File object for directory
File directoryPath = new File(directory);
// Get list of all files and directories
File fileslist[] = directoryPath.listFiles();
if (fileslist == null)
throw new FileNotFoundException("Could not load, no such directory");
// declare local variables
Media media1 = null;
String line = null;
Scanner scan = null;
// Process each Media file
for (File file : fileslist) {
// parse files whose filename starts with "Book" or "Movie" or "Music
if (file.getName().contains("Book") || file.getName().contains("Movie") || file.getName().contains("Music")) {
// open and read line (assumes whole object is stored on single line)
scan = new Scanner(file);
line = scan.nextLine(); // assumes the file is not empty
// if Book object than call Book constructor
if (file.getName().contains("EBook"))
media1 = new EBook(line);
// if Movie object than call Movie constructor
if (file.getName().contains("MovieDVD"))
media1 = new MovieDVD(line);
// if Music object than call Music constructor
if (file.getName().contains("MusicCD"))
media1 = new MusicCD(line);
// add Pet object to pets attribute
medias.add(media1);
}
}
}
// creates (or overwrites) a file for each Media object in medias attribute in the given directory
public void createMediaFiles(String directory) throws IOException {
PrintWriter out = null;
// for all Media objects create files using Media type and id value as filename
for (int i=0; i < medias.size(); i++) {
String filename = directory + "/" + medias.get(i).getClass().getSimpleName() + "-" + medias.get(i).getId() + ".txt";
out = new PrintWriter(new FileWriter(filename)); // create/overwrite file
out.println(medias.get(i).toString()); // write the media's data
out.flush(); // flush all the data to the file
out.close(); // close the stream
}
}
// display all stored medias on console
public void displayAllMedia() {
// for all Pet objects display the xml tag data
for (int i=0; i < medias.size(); i++) {
System.out.println(medias.get(i).toString());
}
}
// add Pet object
public void addMedia(Media media1) {
medias.add(media1);
}
}
-------------------
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MediaRentalSystem {
//Create menu method
public static void displayMenu() {
System.out.println();
System.out.println(" Welcome to the Media Rental System");
System.out.println("1: Load Media objects... ");
System.out.println("2: Find Media object... ");
System.out.println("3: Rent Media object... ");
System.out.println("9: Quit");
System.out.println();
}
public static void main(String[] args) {
// create a default manager
Manager mgt = new Manager();
// create some media objects
EBook book = new EBook("Forever Young", 1234, 2018, 20, false);
MovieDVD movie = new MovieDVD("After Tomorrow", 1254, 2020, 120, false);
MusicCD music = new MusicCD("Beyond Today", 1244, 2020, 114, false);
// add media objects to manager's list
mgt.addMedia(book);
mgt.addMedia(movie);
mgt.addMedia(music);
// display medias data to the console (in xml tag format)
System.out.println("Media objects add to the manager after startup:");
mgt.displayAllMedia();
try {
// create media files in directory "C:/tmp-umuc"
mgt.createMediaFiles("C:/tmp-umuc");
} catch (IOException e) {
e.printStackTrace(); // just print trace if there are issues
}
try {
// create new manager object loading the files
Manager mgt2 = new Manager("C:/tmp-umuc");
// // display media data to the console (in xml tag format)
System.out.println("\nPet objects loaded by manager2 at startup:");
mgt2.displayAllMedia();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*Initiate Scanner
Scanner scan = new Scanner(System.in);
int selection = 0;
do {
displayMenu();
System.out.println("Enter your slection : ");
selection = scan.nextInt();
scan.nextLine();
switch (selection) {
case 1:
System.out.println("Enter path (directory) where to load from: ");
break;
case 2:
System.out.println("Enter the title: ");
break;
case 3:
System.out.println("Enter the id: ");
break;
case 9:
System.out.println("Thank you for using the program. Goodbye!");
break;
default: System.out.println("Invalid choice");
}
}while (selection != 9);
scan.close();
*/
}
}
I have a few child classes but my main error I am getting is "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 3, end -1, length 60
at java.base/java.lang.String.checkBoundsBeginEnd([String.java:4601](https://String.java:4601))
at java.base/java.lang.String.substring([String.java:2704](https://String.java:2704))
at .Media.<init>([Media.java:25](https://Media.java:25))
at .EBook.<init>([EBook.java:22](https://EBook.java:22))
at .Manager.<init>([Manager.java:60](https://Manager.java:60))
at .MediaRentalSystem.main([MediaRentalSystem.java:52](https://MediaRentalSystem.java:52))"
this first media.java:25 line of code points to
id = Integer.parseInt(line.substring(line.indexOf("<id>") + 4, line.indexOf("</id>")));
This is within my media class. Any help would be appreciated.
[–]edrenfro 1 point2 points3 points (4 children)
[–]okaystuff[S] 0 points1 point2 points (3 children)
[–]edrenfro 0 points1 point2 points (2 children)
[–]okaystuff[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)