Question

Help! Not sure how to create this java program to run efficiently. Current code and assignment...

Help! Not sure how to create this java program to run efficiently. Current code and assignment attached.

Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option.

----------------------------------------------------------

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


public class Movies

{

String movieTitle;

String movieGenre;

double movieTime;

   public Movies (String movieTitle, double mTime, String movieGenre)

   {

   super();

   this.movieTitle = movieTitle;

   this.movieGenre = movieGenre;

   movieTime = mTime;

   }



   public String getMovieTitle()

   {

   return movieTitle;

   }

   public void setMovieTitle(String movieTitle)

   {

   this.movieTitle = movieTitle;

   }

   public String getMovieGenre()

   {

   return movieGenre;

   }

   public void setMovieGenre(String movieGenre)

{

   this.movieGenre = movieGenre;

   }

   public double getMovieTime()

   {

   return movieTime;

   }

   public void setMovieTime(double mTime)

   {

   movieTime = mTime;

   }


   public static Comparator<Movies> getDescriptionComparator()
{
return new Comparator<Movies>()
{
public int compare(Movies movie1, Movies movie2)
{
return (movie1.getMovieTitle().compareTo(movie2.getMovieTitle()));
}
};
}


public static Comparator<Movies> getPriceComparator()
{
return new Comparator<Movies>()
{
public int compare(Movies movie1, Movies movie2)
{
return (movie1.getMovieTime() - movie2.getMovieTime());
}
};
}


   public static Comparator<Movies> getDescriptionComparator()
{
return new Comparator<Movies>()
{
public int compare(Movies movie1, Movies movie2)
{
return (movie1.getMovieGenre().compareTo(movie2.getMovieGenre()));
}
};
}
}




ERROR from this code! -

Movies.java:104: error: method getDescriptionComparator() is already defined in class Movies
ÏÏ§Ï public static Comparator<Movies> getDescriptionComparator()
ÏÏ§Ï ^
ϼ§ÏMovies.java:98: error: incompatible types: possible lossy conversion from double to int
ÏÏ§Ï return (movie1.getMovieTime() - movie2.getMovieTime());
ÏÏ§Ï ^

------------------------------------

import java.util.*;
import java.text.*;
import javax.swing.*;
public class MovieSelection
{
  
private static ArrayList<Movies> services = new ArrayList<Movies>();
private static JOptionPane pane;
public static void main(String[] args)
{
  

   String title;

   String genre;

   String time;

   Movies mv = null;

   ArrayList<Movies> arl = new ArrayList<Movies>();

   File inFile = new File("movieinfo1");

   try {

   FileInputStream in = new FileInputStream(inFile);

   BufferedReader reader = new BufferedReader(new InputStreamReader(in));

   String line = null;

   while ((line = reader.readLine()) != null) {



   String[] words= line.split(",");

   title = words[0];



   genre = words[2];

  



   mv = new Movies(title, genre, time);

arl.add(mv);

   }

   } catch (IOException x) {

   System.err.println(x);

   }

   Scanner sc = new Scanner(System.in);

   int choice;

pane = new JOptionPane();
boolean quit = false;
String input = "";
int choice = 0;

do
{
  
input = "" + JOptionPane.showInputDialog(null, "How to sort?\n1) Title 2) Time 3) Genre 0) quit");
input = input.trim();


if (input.equals("null"))
{
break;
}
else
{
try
{

choice = Integer.parseInt(input);


switch (choice)
{
  
case 0:
quit = true;
break;


case 1:
sortByTitle();
displayMovies();
break;

  
case 2:
sortByTime();
displayMovies();
break;

  
case 3:
sortByGenre();
displayMovies();
break;


default:
pane.showMessageDialog(null, "Invalid entry.\nPlease enter a number between 0-3.");
break;
}
}
catch (Exception e)
{
pane.showMessageDialog(null, "Invalid entry.\nPlease enter a number between 0-3.");
}
}
}
while (!quit);
}


private static void sortByTitle()
{
Collections.sort(movie, Movies.getDescriptionComparator());
}


private static void sortByTime()
{
Collections.sort(movie, Movies.getPriceComparator());
}


private static void sortByGenre()
{
Collections.sort(movie, Movies.getDurationComparator());
}


private static void displayMovies()
{
String message = "";
for (Movies movies : movies);
{
message += Movies.getMovieTitle() + " - " + Movies.getMovieTime() + Movies.getMovieGenre() + "\n";
}
pane.showMessageDialog(null, message);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have added the working code to sort the movies based on title,genre and time, have fixed your code by setting proper comparator and other changes into movieSelection.java

Don't forget to upvote if you like my solution. Cheers :)

Movie Selection .java

import java.util.*;
import java.text.*;
import javax.swing.*;
import java.io.*;
public class MovieSelection
{
    public static ArrayList<Movies> arl = new ArrayList<Movies>();
    //private static ArrayList<Movies> services = new ArrayList<Movies>();
    private static JOptionPane pane;
    public static void main(String[] args)
    {


        String title;

        String genre;

        Double time;

        Movies mv = null;


        File inFile = new File("movieinfo");

        try {

            FileInputStream in = new FileInputStream(inFile);

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line = null;

            while ((line = reader.readLine()) != null) {



                String[] words= line.split(",");

                title = words[0];
                genre = words[1];
                time = Double.parseDouble(words[2]);
                mv = new Movies(title, genre, time);
                arl.add(mv);

            }

        } catch (IOException x) {

            System.err.println(x);

        }

        Scanner sc = new Scanner(System.in);

        int choice;

        pane = new JOptionPane();
        boolean quit = false;
        String input = "";
         choice = 0;

        do
        {

            input = "" + JOptionPane.showInputDialog(null, "How to sort?\n1) Title 2) Time  3) Genre  0) quit");
            input = input.trim();


            if (input.equals("null"))
            {
                break;
            }
            else
            {
                try
                {

                    choice = Integer.parseInt(input);


                    switch (choice)
                    {

                        case 0:
                            quit = true;
                            break;


                        case 1:
                            sortByTitle();
                            displayMovies();
                            break;


                        case 2:
                            sortByTime();
                            displayMovies();
                            break;


                        case 3:
                            sortByGenre();
                            displayMovies();
                            break;


                        default:
                            pane.showMessageDialog(null, "Invalid entry.\nPlease enter a number between 0-3.");
                            break;
                    }
                }
                catch (Exception e)
                {
                    pane.showMessageDialog(null, "Invalid entry.\nPlease enter a number between 0-3.");
                }
            }
        }
        while (!quit);
    }


    private static void sortByTitle()
    {
        Collections.sort(arl, Movies.getDescriptionComparator());
    }


    private static void sortByTime()
    {
        Collections.sort(arl, Movies.getPriceComparator());
    }


    private static void sortByGenre()
    {
        Collections.sort(arl, Movies.getGenreComparator());
    }


    private static void displayMovies()
    {
        String message = "";
        for (Movies movie : arl)
        {
            message += "Title -" + movie.getMovieTitle() + " Time - " + movie.getMovieTime() + " Genre -" +   movie.getMovieGenre() + "\n";
        }
        pane.showMessageDialog(null, message);
    }
}

Movies.java

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


public class Movies

{

    String movieTitle;

    String movieGenre;

    Double movieTime;

    public Movies (String movieTitle,  String movieGenre,Double mTime)

    {

        super();

        this.movieTitle = movieTitle;

        this.movieGenre = movieGenre;

        movieTime = mTime;

    }



    public String getMovieTitle()

    {

        return movieTitle;

    }

    public void setMovieTitle(String movieTitle)

    {

        this.movieTitle = movieTitle;

    }

    public String getMovieGenre()

    {

        return movieGenre;

    }

    public void setMovieGenre(String movieGenre)

    {

        this.movieGenre = movieGenre;

    }

    public Double getMovieTime()

    {

        return movieTime;

    }

    public void setMovieTime(double mTime)

    {

        movieTime = mTime;

    }


    public static Comparator<Movies> getDescriptionComparator()
    {
        return new Comparator<Movies>()
        {
            public int compare(Movies movie1, Movies movie2)
            {
                return (movie1.getMovieTitle().compareTo(movie2.getMovieTitle()));
            }
        };
    }


    public static Comparator<Movies> getPriceComparator()
    {
        return new Comparator<Movies>()
        {
            public int compare(Movies movie1, Movies movie2)
            {
                return (movie1.getMovieTime().compareTo(movie2.getMovieTime()));
            }
        };
    }


    public static Comparator<Movies> getGenreComparator()
    {
        return new Comparator<Movies>()
        {
            public int compare(Movies movie1, Movies movie2)
            {
                return (movie1.getMovieGenre().compareTo(movie2.getMovieGenre()));
            }
        };
    }
}
 

 
Add a comment
Know the answer?
Add Answer to:
Help! Not sure how to create this java program to run efficiently. Current code and assignment...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class...

    PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • *JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before...

    *JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before the one entered. I need help correcting my code. I'm unsure why I am getting a name, year, and genre in my output. Input: Jericho Output: Similar title finder. Enter a movie name.\n Here are the 3 movies that are listed before the one you entered\n Jeremy Paxman Interviews...\n Jeremy Taylor\n Jeremy Vine Meets...\n Current output: Similar title finder. Enter a movie name.\n Here...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Hey guys I am having trouble getting my table to work with a java GUI if...

    Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT