Help! Not sure how to create this java program to run efficiently. Current code
ID: 3832153 • Letter: H
Question
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? 1) 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. Please enter a number between 0-3.");
break;
}
}
catch (Exception e)
{
pane.showMessageDialog(null, "Invalid entry. Please 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() + " ";
}
pane.showMessageDialog(null, message);
}
}
Explanation / Answer
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? 1) 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. Please enter a number between 0-3.");
break;
}
}
catch (Exception e)
{
pane.showMessageDialog(null, "Invalid entry. Please 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() + " ";
}
pane.showMessageDialog(null, message);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.