Question

Implement a Java application for the following: 1.) Keep track of a movie collection. 2.) Each...

Implement a Java application for the following:

1.) Keep track of a movie collection.

2.) Each movie in the collection will contain: Title, Genre, Year (4 digits) and Runtime (double - ex. 2.1 (hrs)).

3.) Program will read movies from a local text file named movies.txt in the current project directory in Eclipse.

4.) Each line in the text file contains one movie, containing each field, per line.separated by commas.

5.) Read the text file and load the movies into a Collection (class).

6.) Implement a Comparator for the Movie class that can sort the movies in ascending order of Title+Year. Sort the movies read from the text file (movies.txt) in ascending order of Title+Year and write the sorted Collection to a file called sortedmovies.txt in the same directory as movies.txt.

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

If you have any problem with the code feel free to comment.

Program

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

class Movie {
   // instance variables
   private String title, genre;
   private int year;
   private double runtime;

   // constructor
   public Movie(String title, String genre, int year, double runtime) {
       this.title = title;
       this.genre = genre;
       this.year = year;
       this.runtime = runtime;
   }

   // all getters and setters
   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getGenre() {
       return genre;
   }

   public void setGenre(String genre) {
       this.genre = genre;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }

   public double getRuntime() {
       return runtime;
   }

   public void setRuntime(double runtime) {
       this.runtime = runtime;
   }

   @Override // string representation of movie object
   public String toString() {
       return title + "," + genre + "," + year + "," + runtime;
   }

}

public class Test {
   public static void main(String[] args) throws IOException {
//change the name and location of the text file accordingly
       Scanner sc = new Scanner(new File("data.txt"));
       BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));

       List<Movie> moviesList = new ArrayList<Movie>();

//reading the movies
       while (sc.hasNext()) {
           String[] str = sc.nextLine().split(",");
           int year = Integer.parseInt(str[2].trim());
           double runtime = Double.parseDouble(str[3]);
           moviesList.add(new Movie(str[0], str[1], year, runtime));
       }
       sc.close();

       Comparator<Movie> comp = (i, j) -> {// using lamda expression
           int com = i.getTitle().compareTo(j.getTitle());// first comparing by title

           if (com == 0)// if title are same compare it with year
               com = (i.getYear() > j.getYear()) ? 1 : -1;

           return com;// comparing the title and year
       };

       Collections.sort(moviesList, comp);// sorting the list using comaparator

//writing the movies
       for (Movie i : moviesList) {
           bw.write(i + "");
           bw.newLine();
       }
       bw.close();
   }

}

Input

Output

1 Toy Story, Comedy, 1995 2 Sprited Away, Drama, 2001 3 The Dark Knight, Science Fiction, 2008 4 Rocky, Sports, 1976 5 King Kong, Adventure, 1993|

1 King Kong, Adventure, 1993 2 Rocky, Sports, 1976 B Sprited Away, Drama, 2001 4 The Dark Knight, Science Fiction, 2008 5 Toy Story, Comedy, 1995

Add a comment
Know the answer?
Add Answer to:
Implement a Java application for the following: 1.) Keep track of a movie collection. 2.) Each...
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
  • Implement a Java application for the following: We want to keep track of a Movie Collection....

    Implement a Java application for the following: We want to keep track of a Movie Collection. Each Movie in the collection will contain: Title, Genre, Year (4 digits) and Runtime (double - ex. 2.1 (hrs)). Your program will read movies from a local text file named movies.txt in the current project directory in Eclipse. Each line in the text file contains one Movie with the fields separated by commas. Read the text file and load the movies in to a...

  • LAB 7-Movie List Program Goali Your assignment is to write a C++program to implement the ADT...

    LAB 7-Movie List Program Goali Your assignment is to write a C++program to implement the ADT List by creating a list of movies. This assignment will help you practice: multiple file programming, classes, pablic and private methods, dynamie memory, constructors and destructors, arrays and files Implementation the required classes This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of There are two classes to implement: Movie and MovieList. As you can see...

  • 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;...

  • Implement all the method bodies using JAVA. Collection Selector Problem Overview This assignment focuses on implementing...

    Implement all the method bodies using JAVA. Collection Selector Problem Overview This assignment focuses on implementing the methods of a class much like java.util.collections. The Selector java file defines a class with static methods that implement polymorphic algorithms that operate on and/or return Collections. Each method of Selector is very clearly specified, is independent of the other methods in the class, and is designed to provide relatively simple functionality. So, this is a great context for practicing what we've been...

  • This homework problem has me pulling my hair out. We are working with text files in Java using Ne...

    This homework problem has me pulling my hair out. We are working with text files in Java using NetBeans GUI application. We're suppose to make a movie list and be able to pull up movies already in the text file (which I can do) and also be able to add and delete movies to and from the file (which is what I'm having trouble with). I've attached the specifications and the movie list if you need it. Any help would...

  • 1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...

    1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods. 2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory. 3) Write a Java program to...

  • 1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...

    1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods. 2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory. 3) Write a Java program to...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director,...

    Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director, Genre, Year Released, Running Time) into a vector of structures and perform the following operations: Search for a specific title Search for movies by a specific director Search for movies by a specific genre Search for movies released in a certain time period Search for movies within a range for running time Display the movies on the screen in a nice, easy to read...

  • *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...

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