I want this program in Java please ADVD movie consists of a title, a rating (e.g
ID: 3739808 • Letter: I
Question
I want this program in Java please
ADVD movie consists of a title, a rating (e.g. PG, R, etc.), and a running time in minutes In this project, you will create an application to allow the user to maintain a collection of DVD movies using an array. When the application starts up, it will read in the DVD data from a text file to initialize the array, if the file is available. If the file is not there, then the program starts with an empty array. If the file is corrupted (has an invalid or missing value), then the program stops its initialization at the point of the error The data file should contain one DVD per line, with title followed by rating followed by running time in minutes, all separated by commas. You may assume titles are all in uppercase and do not contain commas. For example ANGELS AND DEMONS, PG-13,138 STAR TREK, R,127 UP, PG, 96 The titles may not be in alphabetical order, but the DVDs should be inserted into the array in alphabetical order The application will then allow its user to perform the following operations * ADD OR MODIFY DVD - The user will be prompted to enter the title, rating and running time (in minutes) for a DVD. If the title is already in the array, then the rating and running time are updated to the supplied values. If the title is not in the array, a DVD is added to the array so that the array is sorted by title. Convert all titles to uppercase only. Titles have to match exactly (in uppercase). If the user enters an invalid rating, the collection should not change. (Valid ratings are G, PG, PG-13, R, and NC-17.) If the user enters an invalid running time (non-positive or non- integer), the collection should not change. REMOVE DVD - The user will be prompted to enter the title of a DVD If the title is in the array, then the DVD is removed, shifting subsequentExplanation / Answer
import java.util.*;
public class DVDManager {
public static void main(String[] args) {
DVDUserInterface dlInterface;
DVDCollection dl = new DVDCollection();
Scanner scan = new Scanner(System.in);
System.out.println("Enter name of data file to load:");
String filename = scan.nextLine();
dl.loadData(filename);
System.out.println("Input interface type: C=Console, G=GUI");
String interfaceType = scan.nextLine();
if (interfaceType.equals("C")) {
dlInterface = new DVDConsoleUI(dl);
dlInterface.processCommands();
} else if (interfaceType.equals("G")) {
dlInterface = new DVDGUI(dl);
dlInterface.processCommands();
} else {
System.out.println("Unrecognized interface type. Program exiting.");
System.exit(0);
}
}
}
-------------------------------------------------------------------------------------------------
public class DVD {
private String title;
private String rating;
private int runningTime;
public DVD(String dvdTitle, String dvdRating, int dvdRunningTime)
{
title = dvdTitle;
rating = dvdRating;
runningTime = dvdRunningTime;
}
public String getTitle()
{
return title;
}
public String getRating()
{
return rating;
}
public int getRunningTime()
{
return runningTime;
}
public void setTitle(String newTitle)
{
title = newTitle;
}
public void setRating(String newRating)
{
rating = newRating;
}
public void setRunningTime(int newRunningTime)
{
runningTime = newRunningTime;
}
public String toString()
{
String result = title + " / " + rating + " / " + runningTime;
return result;
}
}
--------------------------------------------------------------------------------
import java.io.*;
import java.util.Scanner;
public class DVDCollection {
//The current number of DVDs in the array
private int numdvds;
//The array to contain the DVDs
private DVD[] dvdArray;
//The name of the data file that contains dvd data
private String sourceName;
private boolean modified;
public DVDCollection() {
numdvds = 0;
dvdArray = new DVD[7];
}
public String toString() {
String result = "numdvds = " + numdvds + " ";
result += "dvdArray.length = " + dvdArray.length + " ";
for (int i = 0; i < numdvds; i++)
{
result += "dvdArray[" + i + "] = " + dvdArray[i].toString() + " ";
}
return result;
}
public void addOrModifyDVD(String title, String rating, String runningTime)
{
boolean valid = true; //A variable that shows if the typed Rating and Running Time have correct typed values
for (int i = 0; i < runningTime.length() && valid; i++)
{
//This loop checks whether the 'runningTime' string is correct (numeric)
if (runningTime.charAt(i) < '0' || runningTime.charAt(i) > '9') //If one of its symbols is not a digit
{
valid = false;
}
}
//If running time is a non-positive number, that is also an incorrect value
if (Integer.parseInt(runningTime) < 1)
{
valid = false;
}
if (!rating.equals("G") && !rating.equals("PG") && !rating.equals("PG-13")
&& !rating.equals("R") && !rating.equals("NC-17"))
{
//This if-statement checks whether the 'rating' is one of the allowed values
valid = false;
}
if (valid) //If values are valid - continue with modifying/adding
{
boolean add = true; //This variable shows whether we should add a new DVD or not (if not - modify)
int index = 0; //'index' is used to show the suitable index of a new element or of an already saved one
for (int i = 0; i < numdvds && add; i++)
{
//This loop does a check for the value of 'add'
if (dvdArray[i].getTitle().equals(title))
{
//Once an element with the same title is found, 'add' becomes 'false', loop is over
add = false;
//and 'index' shows where our DVD is
index = i;
}
}
if (add) //If 'add' is still 'true', it means the title was not found at the array and we have to add it
{
if (numdvds == dvdArray.length) //This check shows if we have to resize the array because it is full
{
//A new array is created with twice the size of the old one
DVD[] newDvdArray = new DVD[dvdArray.length * 2];
//Save all the values from the old array at the new one
for (int i = 0; i < numdvds; i++)
{
newDvdArray[i] = dvdArray[i];
}
//Now the old array is the same as before but with twice bigger size
dvdArray = newDvdArray;
}
//Now we have to find the correct place of the new DVD in the sorted collection
while (index < numdvds && title.compareTo(dvdArray[index].getTitle()) >= 0)
{
//Every time we go through a title lexicographically less than ours, index increases
index++;
}
//Now we shift all the elements from the needed index to the end with 1 place
for (int i = numdvds; i > index; i--)
{
dvdArray[i] = dvdArray[i - 1];
}
//The new DVD is being positioned at the correct place
dvdArray[index] = new DVD(title, rating, Integer.parseInt(runningTime));
//The number of the DVDs in the collection is increased
numdvds++;
}
else //If the title was found in the array and we just have to modify its rating and running time
{
dvdArray[index].setRating(rating);
dvdArray[index].setRunningTime(Integer.parseInt(runningTime));
}
}
modified = true;
}
public void removeDVD(String title)
{
int index = 0; //This 'index' is used to find what is the index of the element, we need to remove
boolean found = false; //'found' stands for showing if the collection contains the title we are looking for
while (!found && index < numdvds) //Until all the elements are gone through or the title is found
{
if (dvdArray[index].getTitle().equals(title)) //If title is found
{
found = true;
}
else //If that index' title is not what we are looking for
{
index++;
}
}
if (found) //If the title was found in the collection - remove it
{
dvdArray[index] = null; //Removing the element
if (index < numdvds - 1) //If it is not the last element, we have to shift the other 1 place forward
{
int i = index;
while (dvdArray[i] != null || i == index)
{
dvdArray[i] = dvdArray[++i];
}
}
numdvds--; //Decreasing the number of DVDs
}
}
//This method goes through the whole array and saves into a string variable all the DVDs with that rating.
public String getDVDsByRating(String rating)
{
String result = "";
for (int i = 0; i < numdvds; i++) //Here we create a loop for going through all the DVDs
{
if (dvdArray[i].getRating().equals(rating)) //If one with the suitable rating is found, we add it to the string
{
result += dvdArray[i].toString() + " ";
}
}
return result;
}
//This method calculates all the DVDs' running time in one variable.
public int getTotalRunningTime()
{
int total = 0; //An integer for the sum of running times of all DVDs
for (int i = 0; i < numdvds; i++)
{
total += dvdArray[i].getRunningTime();
}
return total;
}
public void loadData(String filename)
{
sourceName = filename;
try
{
BufferedReader in = new BufferedReader(new FileReader("input.txt")); //An object to open the file
Scanner scan = new Scanner(in).useDelimiter(",| | "); //A scanner for better reading the file
String title, rating, runningTime; //Local helper variables for easier saving each DVD
while (scan.hasNext()) //While the file still has any unread text left
{
title = scan.next(); //Reading the title
rating = scan.next(); //Reading the rating
runningTime = scan.next(); //Reading the running time
scan.next(); //Reading the symbols left to the next title
addOrModifyDVD(title, rating, runningTime); //Add or modify this DVD
}
in.close();
scan.close();
}
//Handling the exceptions
catch (FileNotFoundException e)
{
return;
}
catch (IOException e)
{
System.out.println("Load of directory failed.");
e.printStackTrace();
System.exit(1);
return;
}
}
//This method saves the new content of the array to the txt file, if the array was modified.
public void save()
{
if (modified) //If the array was modified
{
try
{
//Create PrintWriter for saving the txt file
PrintWriter out = new PrintWriter(new FileWriter(sourceName));
//Saving each element of the array in a specific format
for (int i = 0; i < numdvds; i++)
{
out.println(dvdArray[i].getTitle() + "," + dvdArray[i].getRating()
+ "," + dvdArray[i].getRunningTime());
}
//Close the file and reset modified
out.close();
modified = false;
}
//Handling the exception
catch (Exception e)
{
System.err.println("Save of directory failed");
e.printStackTrace();
System.exit(1);
}
}
}
}
--------------------------------------------------------------------------------
import java.util.InputMismatchException;
import java.util.Scanner;
public class DVDConsoleUI implements DVDUserInterface {
private DVDCollection dvdlist;
private Scanner scan;
public DVDConsoleUI(DVDCollection dl) {
dvdlist = dl;
scan = new Scanner(System.in);
}
public void processCommands()
{
String[] commands = {"Add/Modify DVD",
"Remove DVD",
"Get DVDs By Rating",
"Get Total Running Time",
"Save and Exit"};
int choice;
do {
for (int i = 0; i < commands.length; i++) {
System.out.println("Select " + i + ": " + commands[i]);
}
try {
choice = scan.nextInt();
scan.nextLine();
switch (choice) {
case 0: doAddOrModifyDVD(); break;
case 1: doRemoveDVD(); break;
case 2: doGetDVDsByRating(); break;
case 3: doGetTotalRunningTime(); break;
case 4: doSave(); break;
default: System.out.println("INVALID CHOICE - TRY AGAIN");
}
} catch (InputMismatchException e) {
System.out.println("INVALID CHOICE - TRY AGAIN");
scan.nextLine();
choice = -1;
}
} while (choice != commands.length-1);
}
private void doAddOrModifyDVD() {
// Request the title
System.out.println("Enter title");
String title = scan.nextLine();
if (title.equals("")) {
return; // input was cancelled
}
title = title.toUpperCase();
// Request the rating
System.out.println("Enter rating");
String rating = scan.nextLine();
if (rating.equals("")) {
return; // input was cancelled
}
rating = rating.toUpperCase();
// Request the running time
System.out.println("Enter running time");
String time = scan.nextLine(); // NOTE: time read in as a String!
if (time.equals("")) {
return; // input was cancelled
}
// Add or modify the DVD (assuming the rating and time are valid)
dvdlist.addOrModifyDVD(title, rating, time);
// Display current collection to the console for debugging
System.out.println("Adding/Modifying: " + title + "," + rating + "," + time);
System.out.println(dvdlist);
}
private void doRemoveDVD() {
// Request the title
System.out.println("Enter title");
String title = scan.nextLine();
if (title.equals("")) {
return; // dialog was cancelled
}
title = title.toUpperCase();
// Remove the matching DVD if found
dvdlist.removeDVD(title);
// Display current collection to the console for debugging
System.out.println("Removing: " + title);
System.out.println(dvdlist);
}
private void doGetDVDsByRating() {
// Request the rating
System.out.println("Enter rating");
String rating = scan.nextLine();
if (rating.equals("")) {
return; // dialog was cancelled
}
rating = rating.toUpperCase();
String results = dvdlist.getDVDsByRating(rating);
System.out.println("DVDs with rating " + rating);
System.out.println(results);
}
private void doGetTotalRunningTime() {
int total = dvdlist.getTotalRunningTime();
System.out.println("Total Running Time of DVDs: ");
System.out.println(total);
}
private void doSave() {
dvdlist.save();
}
}
------------------------------------------------------------------------
import java.awt.Dialog.ModalityType;
import javax.swing.*;
public class DVDGUI implements DVDUserInterface {
private DVDCollection dvdlist;
public DVDGUI(DVDCollection dl)
{
dvdlist = dl;
}
public void processCommands()
{
String[] commands = {"Add/Modify DVD",
"Remove DVD",
"Get DVDs By Rating",
"Get Total Running Time",
"Exit and Save"};
int choice;
JFrame frame = null; //A JFrame object added to help bringing the showOptionDialog on top
do {
if (frame == null)
{
frame = new JFrame();
}
frame.setVisible(true);
frame.setLocation(800, 500);
frame.setAlwaysOnTop(true);
choice = JOptionPane.showOptionDialog(frame,
"Select a command",
"DVD Collection",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
commands,
commands[commands.length - 1]);
switch (choice) {
case 0: doAddOrModifyDVD(); break;
case 1: doRemoveDVD(); break;
case 2: doGetDVDsByRating(); break;
case 3: doGetTotalRunningTime(); break;
case 4: doSave(); break;
default: // do nothing
}
} while (choice != commands.length-1);
System.exit(0);
}
private void doAddOrModifyDVD() {
// Request the title
String title = JOptionPane.showInputDialog("Enter title");
if (title == null) {
return; // dialog was cancelled
}
title = title.toUpperCase();
// Request the rating
String rating = JOptionPane.showInputDialog("Enter rating for " + title);
if (rating == null) {
return; // dialog was cancelled
}
rating = rating.toUpperCase();
// Request the running time
String time = JOptionPane.showInputDialog("Enter running time for " + title);
if (time == null) {
}
// Add or modify the DVD (assuming the rating and time are valid
dvdlist.addOrModifyDVD(title, rating, time);
// Display current collection to the console for debugging
System.out.println("Adding/Modifying: " + title + "," + rating + "," + time);
System.out.println(dvdlist);
}
private void doRemoveDVD() {
// Request the title
String title = JOptionPane.showInputDialog("Enter title");
if (title == null) {
return; // dialog was cancelled
}
title = title.toUpperCase();
// Remove the matching DVD if found
dvdlist.removeDVD(title);
// Display current collection to the console for debugging
System.out.println("Removing: " + title);
System.out.println(dvdlist);
}
private void doGetDVDsByRating() {
// Request the rating
String rating = JOptionPane.showInputDialog("Enter rating");
if (rating == null) {
return; // dialog was cancelled
}
rating = rating.toUpperCase();
String results = dvdlist.getDVDsByRating(rating);
System.out.println("DVDs with rating " + rating);
System.out.println(results);
}
private void doGetTotalRunningTime() {
int total = dvdlist.getTotalRunningTime();
System.out.println("Total Running Time of DVDs: ");
System.out.println(total);
}
private void doSave() {
dvdlist.save();
}
}
-------------------------------------------------------------------------
public interface DVDUserInterface {
void processCommands();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.