代写Java Lab 16: Collections代写Java编程

2025-03-03 代写Java Lab 16: Collections代写Java编程

Java Lab 16: Collections

This lab uses Collections. Create a project named Lab16.

Problem statement: The application will let users display a list of movies in several orderings and search for specific movies by several criteria.

The tab-delimited file "movies.tsv" has information about movies, one per line. The data's format is

id#movie nameyearcountriesgenre1genre2 …

where the genre list is some number of tab-delimited strings. You'll probably want to strip the strings of spaces; some of the movie names and countries are surrounded by quotes, which should be replaced with ""; there may be multiple countries listed for a movie, but treat it as one string – we won't be looking for specific countries; and finally, for some movies, the table display's formats may not be large enough, but you can ignore that – just use the given formats.

Your program will present these choices in a menu: sort by ID, sort by name, sort by year, sort by year (reverse), search by name, search by year, search by genre, quit. The sort options will use the method sortBy(String), where the parameter is either "ID", "Name", "Year", or "Reverse Year", where the last one sorts from largest (most recent year) to smallest. The search options will use methods searchByName(String), searchByYear(int), and searchByGenre(String).

These methods return at most 5 movies. The first returns a Movie objects that match exactly on name, the second matches on year, and the third matches on genre. Each returns an ArrayList; use displayMovies() to display the results. For consistency, call sortBy("ID") before searching – otherwise, your results may differ.

To do the sorting, you need three things. First, Movie implements the interface Comparable; you'll need to implement its compareTo() method to compare movies on movieID.  Second, use an anonymous inner class (recall that this is similar to an EventHandler in JavaFX) that implements Comparator; you'll need to implement its compare() method to compare movies on movieName. Third, create a class named SortByYear that also implements Comparator; you'll again need to implement its compare() method, but this time, compare movies on the year field. You should try out (not required) Eclipse's wizard: select New->Class, name it, then in the Interface box, click Add, then find Comparator and click OK, then change T to Movie and click Finish. Here's the code for sorting by year in reverse order (from the most recent year), assuming you've written the SortByYear code:

Comparator c = Collections.reverseOrder(new SortByYear());

Collections.sort(movieList, c);

Screen shots (not all output shown for long results):

Solution Design:

Steps

Download

Lab16 instructions

Lab16Main.java, Movie.java, movies.tsv

1.    Complete all classes listed below. Check output with movies.tsv.

2.    Write your name and Andrew Id at the top in all Java files to be submitted

3.    Zip all the java files in a zip-file named as your Andrew ID. Submit it on Canvas.

Class

Member variables

Methods

Lab16Main

movieList: ArrayListe>

main(): Creates a Lab16Main object, calls its readMovies() method  with  "movies.tsv";  calls  getMenuChoice()  in a loop; calls Lab4Main methods to do the other work. Partially coded.

public static final int

static int getMenuChoice(): display the menu, get a valid user choice. Already coded.

void readMovies(String filename): open the movie file,  read each line and split it on \t, call toMovie( ) to parse the Strings, store the new movie in movieList. Partially coded.

Movie  toMovie(String[]):  get the   movieID (convert to   int), movieName (strip it and replace "\"" with ""), year (convert to int), country (strip it and replace "\"" with ""), and the genres (split again on ",", add the genre strings to an ArrayList). New  up a  new  Movie object from these  parts  and  return  it.

Already coded.

private void displayMovies(ArrayList): display the parameter as a table. This is parameterized so it can be used with any ArrayList argument. Already coded.

void sortBy(String): use Collections.sort() to sort the movie on the appropriate field, as indicated by the parameter (Name, ID, Year, or  ReverseYear).   See  the  code above for  ReverseYear.

Partially coded.

ArrrayList searchByName(String name): returns up to

5 moves whose name match the parameter, or null if not found.

Already coded.

ArrayList searchByYear(int year): returns up to 5 movies from that year. Use displayMovies() to display what it returns.

ArrayList searchByGenre(String genre): returns up to 5 movies matching that genre. Use displayMovies() to display what it returns.

ArrayList     getList(): returns     movieList,      breaks

encapsulation, but okay for now. Partially coded.

Movie implements Comparable

int movieID:

String movieName

int year

String country

ArrayList<String> genres

Overloaded constructor, toString(), all getters. Already coded.

int compareTo(Movie): compares this movie to the parameter on the movieID field

SortByYear implements

Comparator

public   static   int   compare(Movie, Movie): compares   two movies on year