Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this program that my professor assigned to us. He has not taught us how t

ID: 3638282 • Letter: I

Question

I have this program that my professor assigned to us. He has not taught us how to save files and how to do arrays as complicated as this. I need help finding how to do the code required for this. If only you can help me find the correct code to use and how to set the attributes. I need like a general code that can work as i will be imputing the movies as elements. Thank you, hope you can help me in this time of need...

what he is asking is:
User Interface: You will develop a program to maintain an user's movie queue. The interface should have two sections: Movies Library and Movies Queue. In the movies library the user will be presented with a collection of movies (which you should pre-populate) and the option to add them to the queue. This movies list should be persistent and extensible (that is, one should be able to add more movies), which means the list should be kept on a file. Movies will have the following attributes:
Title
Actors
Director
Genre
The Movie Library should be searchable and sortable per each of these attributes.

this is just text based, no gui. It has to support input from the user.

Explanation / Answer


import java.io.*;
import java.util.*;

public class MovieLibrary {

    java.util.ArrayList movieList;
    String moviesListFile;

    MovieLibrary(String fileName) {
        this.moviesListFile = fileName;
        loadMovieTitles();
        inputQueue();
    }

    // Note : Data in file must use comma delimmiter to seperate data in each column.
    void loadMovieTitles() {

        try {
            // Create FileReader Object
            FileInputStream inputFileReader = new FileInputStream(moviesListFile);

            DataInputStream in = new DataInputStream(inputFileReader);
            BufferedReader inputStream = new BufferedReader(
                    new InputStreamReader(in));

            String inLine = null;
            while ((inLine = inputStream.readLine()) != null) {
                // If just seperated by space replace delimitter with prefered below.
                StringTokenizer token = new StringTokenizer(inLine, ";");
                
                String title = token.nextToken();
                String actors = token.nextToken();
                String director = token.nextToken();
                String genre = token.nextToken();
                if (movieList == null) {
                    movieList = new java.util.ArrayList();
                }
               
                movieList.add(Movie.addMovieByFile(title, actors, director, genre));
            }
        catch (Exception e) {
            System.out.println(" Errors reading Library file : " + e.getMessage());
        }
        displayQueue();
    }

    void displayQueue() {
        java.util.Iterator it = movieList.iterator();
        int index = 0;
        System.out.println(" Stored Movie Titles from File "+moviesListFile);
        System.out.println("=================================================");
        while (it.hasNext()) {
            Movie m = (Movieit.next();
            index++;
            System.out.println(index
                    "  " + m.title + " " " " + m.actors
                    " " + m.director + " " + m.genre);

        }
    }

    void updateLibrary() {
        try {
            FileWriter fstream = new FileWriter(moviesListFile);
            BufferedWriter out = new BufferedWriter(fstream);
            java.util.Iterator it = movieList.iterator();
            while (it.hasNext()) {
                      Movie m = (Movieit.next();
                      out.write(m.title+";"+m.actors+";"+m.director+";"+m.genre);
                      out.newLine();
            }
            out.close();
        catch (Exception e) {
            System.out.println("Library storage Exception : " + e.getMessage());
        }
    }

    void LibraryMenu() {
        System.out.println();
        System.out.println("Menu---");
        System.out.println("1. Movies Library.");
        System.out.println("2. Movies Queue.");
        System.out.println("3. Exit. ");
        System.out.print("Enter User Option : ");
    }

    public void inputQueue() {
        java.util.Scanner input = new java.util.Scanner(System.in);
        LibraryMenu();
        String userInput = "";
        while (!userInput.equals("3")) {
            userInput = input.next();

            if (userInput.equals("1")) {
                displayQueue();
            else if (userInput.equals("2")) {
                System.out.println("  Add Movie : ");
                if (movieList == null) {
                    movieList = new java.util.ArrayList();
                }
                movieList.add(Movie.addMovie());
                updateLibrary();
                displayQueue();
            }
            LibraryMenu();
        }
    }

    private static class Movie {

        private String title;
        private String actors;
        private String director;
        private String genre;

        Movie(String title, String actors, String director, String genre) {
            this.title = title;
            this.actors = actors;
            this.director = director;
            this.genre = genre;
        }

        static Movie addMovieByFile(String title, String actors, String director, String genre) {
            return new Movie(title, actors, director, genre);
        }

        static Movie addMovie() {
            java.util.Scanner input = new java.util.Scanner(System.in);
            String title=null, actors=null, director=null, genre=null;
            System.out.print("Enter Title : ");
            title = input.nextLine();
            System.out.print("Enter Actors : ");
            actors = input.nextLine();
            System.out.print("Enter Director : ");
            director = input.nextLine();
            System.out.print("Enter Genre : ");
            genre = input.nextLine();
            
            if(title.length()==0)
                title="-";
            if(actors.length()==0)
                actors="-";
            if(director.length()==0)
                director="-";
            if(genre.length()==0)
                genre="-";            
            return new Movie(title, actors, director, genre);
        }
    }

    public static void main(String[] args) {
        MovieLibrary library = new MovieLibrary("movies.txt");
    }
}  
import java.io.*;
import java.util.*;

public class MovieLibrary {

    java.util.ArrayList movieList;
    String moviesListFile;

    MovieLibrary(String fileName) {
        this.moviesListFile = fileName;
        loadMovieTitles();
        inputQueue();
    }

    // Note : Data in file must use comma delimmiter to seperate data in each column.
    void loadMovieTitles() {

        try {
            // Create FileReader Object
            FileInputStream inputFileReader = new FileInputStream(moviesListFile);

            DataInputStream in = new DataInputStream(inputFileReader);
            BufferedReader inputStream = new BufferedReader(
                    new InputStreamReader(in));

            String inLine = null;
            while ((inLine = inputStream.readLine()) != null) {
                // If just seperated by space replace delimitter with prefered below.
                StringTokenizer token = new StringTokenizer(inLine, ";");
                
                String title = token.nextToken();
                String actors = token.nextToken();
                String director = token.nextToken();
                String genre = token.nextToken();
                if (movieList == null) {
                    movieList = new java.util.ArrayList();
                }
               
                movieList.add(Movie.addMovieByFile(title, actors, director, genre));
            }
        catch (Exception e) {
            System.out.println(" Errors reading Library file : " + e.getMessage());
        }
        displayQueue();
    }

    void displayQueue() {
        java.util.Iterator it = movieList.iterator();
        int index = 0;
        System.out.println(" Stored Movie Titles from File "+moviesListFile);
        System.out.println("=================================================");
        while (it.hasNext()) {
            Movie m = (Movieit.next();
            index++;
            System.out.println(index
                    "  " + m.title + " " " " + m.actors
                    " " + m.director + " " + m.genre);

        }
    }

    void updateLibrary() {
        try {
            FileWriter fstream = new FileWriter(moviesListFile);
            BufferedWriter out = new BufferedWriter(fstream);
            java.util.Iterator it = movieList.iterator();
            while (it.hasNext()) {
                      Movie m = (Movieit.next();
                      out.write(m.title+";"+m.actors+";"+m.director+";"+m.genre);
                      out.newLine();
            }
            out.close();
        catch (Exception e) {
            System.out.println("Library storage Exception : " + e.getMessage());
        }
    }

    void LibraryMenu() {
        System.out.println();
        System.out.println("Menu---");
        System.out.println("1. Movies Library.");
        System.out.println("2. Movies Queue.");
        System.out.println("3. Exit. ");
        System.out.print("Enter User Option : ");
    }

    public void inputQueue() {
        java.util.Scanner input = new java.util.Scanner(System.in);
        LibraryMenu();
        String userInput = "";
        while (!userInput.equals("3")) {
            userInput = input.next();

            if (userInput.equals("1")) {
                displayQueue();
            else if (userInput.equals("2")) {
                System.out.println("  Add Movie : ");
                if (movieList == null) {
                    movieList = new java.util.ArrayList();
                }
                movieList.add(Movie.addMovie());
                updateLibrary();
                displayQueue();
            }
            LibraryMenu();
        }
    }

    private static class Movie {

        private String title;
        private String actors;
        private String director;
        private String genre;

        Movie(String title, String actors, String director, String genre) {
            this.title = title;
            this.actors = actors;
            this.director = director;
            this.genre = genre;
        }

        static Movie addMovieByFile(String title, String actors, String director, String genre) {
            return new Movie(title, actors, director, genre);
        }

        static Movie addMovie() {
            java.util.Scanner input = new java.util.Scanner(System.in);
            String title=null, actors=null, director=null, genre=null;
            System.out.print("Enter Title : ");
            title = input.nextLine();
            System.out.print("Enter Actors : ");
            actors = input.nextLine();
            System.out.print("Enter Director : ");
            director = input.nextLine();
            System.out.print("Enter Genre : ");
            genre = input.nextLine();
            
            if(title.length()==0)
                title="-";
            if(actors.length()==0)
                actors="-";
            if(director.length()==0)
                director="-";
            if(genre.length()==0)
                genre="-";            
            return new Movie(title, actors, director, genre);
        }
    }

    public static void main(String[] args) {
        MovieLibrary library = new MovieLibrary("movies.txt");
    }
}