In: Computer Science
Problem Write a movie management system using object-oriented design principles. The program will read from the supplied data file into a single array list. The data file (movies.txt) contains information about the movies. Each movie record has the following attributes: - Duration (in minutes) - Title - Year of release Each record in the movies.txt file is formatted as follows: - Duration,Title,Year - e.g.: 91,Gravity,2013 Specifically, you have to create an interactive menu driven application that gives the user the following options: 1. Add a new movie and save. o The user will be prompted to enter the duration in minutes, title of the new movie, and the year the movie was released. Before the movie is added, the inputs provided by the user should be validated: The duration and year of the movie should not be zero and the title of the movie should not be empty. o When you add a new movie, the program should update the data file by saving the new movie at the end of the movie list. 2. Generate list of movies released in a year. o The user will input a year and the program will display a list of all the movies released in that year along with the duration (in minutes) of all the movies. o The list of movies does not have to be sorted. 3. Generate list of random movies. o The user will input the number of movies and the program will display a list containing the amount of random movies along with the duration (in minutes) of all the movies. o There is no minimum or maximum duration for the movies in the list. o You can use Collections.shuffle in the java.utils package to randomize the movie list. 4. Exit the program. - Save the list of movies back into the data file “movies.txt” using the above format (Duration,Title,Year). Notes: To follow the object-oriented principles, your project should contain ONLY the following classes and methods in their respective package. Package Class Methods sait.mms.application AppDriver main sait.mms.managers MovieManagementSystem displayMenu, addMovie,generateMovieInYear, generateRandomMovie, loadMovie sait.mms.problemdomain Movie Accessor methods, toString You cannot use parallel and/or nested arrays in this assignment (you can use ArrayList). Sample runs: An example of an added and saved a new movie (where input is shown in bold underline): Movie Management system 1 Add New Movie and Save 2 Generate List of Movies Released in a Year 3 Generate List of Random Movies 4 Exit Enter an option: 1 Enter duration: 100 Enter movie title: We are Gold Enter year: 2019 Saving movies... Added movie to the data file. An example of a generated list of movies released in a year (where input is shown in bold underline): Movie Management system 1 Add New Movie and Save 2 Generate List of Movies Released in a Year 3 Generate List of Random Movies 4 Exit Enter an option: 2 Enter in year: 1996 Movie List Duration Year Title 103 1996 DragonHeart 93 1996 Trainspotting 145 1996 Independence Day Total duration: 341 minutes An example of invalid option (where input is shown in bold underline): Movie Management system 1 Add New Movie and Save 2 Generate List of Movies Released in a Year 3 Generate List of Random Movies 4 Exit Enter an option: 5 Invalid option! An example of a generated list of random movies (where input is shown in bold underline): Movie Management system 1 Add New Movie and Save 2 Generate List of Movies Released in a Year 3 Generate List of Random Movies 4 Exit Enter an option: 3 Enter number of movies: 5 Movie List Duration Year Title 129 2016 Now You See Me 2 139 1999 Fight Club 136 2014 Captain America: The Winter Soldier 81 1995 Toy Story 103 2017 Life Total duration: 588 minutes
Please find your solution below and if any doubt or need changes comment and do uovote.
testMovie.java
import java.io.File;
import java.io.FileWriter;
import java.util.*;
public class testMovie {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(new File("movies.txt"));
ArrayList<Movie>list=new ArrayList<Movie>();
String title;
int duration;
int year;
while(sc.hasNext()){
String s=sc.nextLine();
String a[]=s.split(",");
Movie mObj=new Movie(Integer.parseInt(a[0]),a[1],Integer.parseInt(a[2]));
list.add(mObj);
}
sc.close();
sc=new Scanner(System.in);
while(true) {
System.out.println("Movie Management system");
System.out.println("1.Add New Movie and Save");
System.out.println("2.Generate List of Movies Released in a Year");
System.out.println("3.Generate List of Random Movies");
System.out.println("4.Exit the program");
int ch;
System.out.println("Enter an option: ");
ch=sc.nextInt();
switch(ch) {
case 1:
{
while(true) {
System.out.print("Enter duration:");
duration=sc.nextInt();
if(duration<=0) {
System.out.println("Duration must be greater than 0");
}
else {
break;
}
}
sc.nextLine();
do {
System.out.print("Enter Movie Title :");
title=sc.nextLine();
if(title.isEmpty())
{
System.out.println("Title must not be empty");
}
}while(title.isEmpty());
while(true) {
System.out.print("Enter year:");
year=sc.nextInt();
if(year<=0) {
System.out.println("Year must be positive");
}
else {
break;
}
}
Movie obj=new Movie(duration,title,year);
list.add(obj);
System.out.println("Saving movies... Added movie to the data file");
break;
}
case 2:
{
System.out.print("Enter year:");
year=sc.nextInt();
System.out.printf("%25s%n","Movie List");
System.out.printf("%12s","Duration");
System.out.printf("%12s","Year");
System.out.printf("%20s","Title");
System.out.println();
for(int i=0;i<list.size();i++) {
if(list.get(i).getYear()==year) {
System.out.printf("%12d",list.get(i).getDuration());
System.out.printf("%12d",list.get(i).getYear());
System.out.printf("%20s",list.get(i).getTitle());
System.out.println();
}
}
break;
}
case 3:
{
Random ran=new Random();
int n;
while(true) {
System.out.print("Enter number of movies:");
n=sc.nextInt();
if(list.size()<n) {
System.out.println("Number of movies should be less the list.");
}
else {
break;
}
}
System.out.printf("%25s%n","Movie List");
System.out.printf("%12s","Duration");
System.out.printf("%12s","Year");
System.out.printf("%20s","Title");
System.out.println();
for(int i=0;i<n;i++) {
Collections.shuffle(list);
int rand=ran.nextInt(list.size());
System.out.printf("%12d",list.get(rand).getDuration());
System.out.printf("%12d",list.get(rand).getYear());
System.out.printf("%20s",list.get(rand).getTitle());
System.out.println();
}
break;
}
case 4:
{
FileWriter wrt=new FileWriter(new File("movies.txt"));
for(int i=0;i<list.size();i++) {
wrt.write(list.get(i).getDuration()+","+list.get(i).getTitle()+","+list.get(i).getYear()+"\n");
}
wrt.close();
System.exit(0);
}
default:
System.out.println("Invalid Option");
break;
}
}
}
}
Movie.java
public class Movie {
private int duration;
private String title;
private int year;
/**
* @param duration
* @param title
* @param year
*/
public Movie(int duration, String title, int year) {
this.duration = duration;
this.title = title;
this.year = year;
}
/**
* @return the duration
*/
public int getDuration() {
return duration;
}
/**
* @param duration
* the duration to set
*/
public void setDuration(int duration) {
this.duration = duration;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param year
* the year to set
*/
public void setYear(int year) {
this.year = year;
}
}
movies.txt
120,Saving Pirate Ryan,1998
110,Tenet,2008
120,Titanic,1996
102,The Big Bang Theory,2008
132,Thin Red Line,2002
121,The Lion king,2008
155,Avatar,2010
129,Henkok,2008
110,Mela,2011
140,Aliens,1988
120,Rock on the tent,1999
70,The Kobra of Amazon,2008
91,Gravity,2013
90,Koyla,2008
130,Terminator,1994
135,Enemy At the gates,2001