I need some help with my Java homework! Can someone help me? ___________________
ID: 3702401 • Letter: I
Question
I need some help with my Java homework! Can someone help me?
_________________________________________________________________________________________________
A data file (NetflixUSA_Oct15_cleaned.txt) is included on eLearn that lists about 10,500 items from a totally hypothetical media streaming service. Each line is formatted like this:
Title (Year) | Average User Rating, Length
The length is expressed in a format like 53m or 1hr 47m for a movie, or in a number of episodes/seasons/volumes/etc. for a series. Note that for this assignment, we will assume that a “movie” is anything that’s not a series. This includes things like documentaries, comedy specials, etc.
In case you’re curious, the file lists all of Netflix’s USA offerings as of Oct. 15, 2012. If you’re wondering why this isn’t more recent, it’s surprisingly difficult to find a plain text file of Netflix’s current offerings. The older data is a bit more interesting anyway, since Netflix recently abolished their star rating system in favor of a simpler “thumbs-up, thumbs-down” system.
_________________________________________________________________________________________________
The Assignment
In this project you’ll be writing some software that parses (reads) the data file and allows the user to apply and remove filters to customize the results. That should make it easier to pick from the huge selection of options available! For example, the user may want to see a list of all the TV series after 1999 that are rated higher than 4.0 stars. This would be expressed using the following three filters:
genre = series
year > 1999
rating > 4.0
The user should be able to add as many filters as s/he desires, and the software should allow the user to display a list of the items from the data file that match all the filters. The user should also be able to remove filters at will. Every time a filter is added or removed, the software should indicate how many items from the data file match the filter list.
At minimum, your software must support the following filter formats:
genre = movie
genre = series
title = ____ (exact title match)
year > ____
year < ____
rating > ____
rating < ____
However, feel free to implement other filters as well! Some suggestions:
title contains ____ (partial title match)
title > ____ (alphabetical comparison)
title < ____ (alphabetical comparison)
Anything involving the length (for example, length < 2hr)
_________________________________________________________________________________________________
Class Design
Your project must include the following classes:
An abstract Media class, with concrete Movie and Series subclasses. These classes should be designed to hold all the relevant information about an item that can be obtained from the data file.
A DataFileReader class that handles reading the provided data file. For each line of the file, a new Mediaobject (either Movie or Series) should be created and added to a list of Media objects. This class should include a method that returns the finished list.
A Filter class that represents a single filter. (You might be tempted to store the filters as plain strings, but making a separate class will allow you to more cleanly access the separate parts of each filter. Note that all the filters are in this format: field relation target) Every time the user adds a filter, a new Filter object should be created and placed in a list. Removing a filter should remove it from this list.
A client program with a main method to run.
Here’s an outline of what the client program needs to do:
Read the data file and create a new Media object for each line in the file. Store these objects in a list (call this masterList).
Allow the user to specify any number of filters. These filters will be stored in a separate list (call thisfilterList).
Each time the user adds a new filter or removes an existing filter, your code needs to search the masterListto find only the objects that match all the filters in the filterList. Copy those objects into another list (call this currentList). You can simply list the items in the currentList to see which items match all filters.
_________________________________________________________________________________________________
Other Requirements
Implement error checking on all user inputs, including exception handling where appropriate. Your software should never crash due to user input! Examples of error checking that should be implemented:
Ensuring the user cannot enter an invalid filter
Ensuring the user cannot remove a filter that doesn’t exist
Ensuring the user cannot enter an invalid entry after any menu
Use Java’s built-in classes (either java.util.ArrayList or java.util.LinkedList) to store the lists of Mediaobjects and the list of Filter objects.
Create a menu-based system that will allow user to select and type in the filter directly.
_________________________________________________________________________________________________
Hints
The file reading part is not super difficult, but it is not trivial. Get started on this part early, since you need it for the rest of the project! You should be able to complete the vast majority of this part without any knowledge of lists.
In some IDEs, Scanner sometimes stops prematurely when trying to read large text files. A workaround is to use a FileReader object in the Scanner constructor instead of a File object. So instead of
Scanner longLiveSloths = new Scanner(new File(yourFileName));
do this:
Scanner longLiveSloths = new Scanner(new FileReader(yourFileName));
Refer to the String class in the Java API for some helpful methods. I used charAt, equals, split, andsubstring very often in my solution.
Strings cannot be cast to primitive types. However, you can use the static methods Double.parseDoubleand Integer.parseInt can be used to convert strings into double and int, respectively. These methods throw a NumberFormatException if you try passing an argument that can’t be read as that type.
_________________________________________________________________________________________________
Here's a small excerpt of the NetflixUSA_Oct15_cleaned.txt file (it's ~10,500 lines long so I excluded most of it for brevity's sake):
!Women Art Revolution (2010) | 3 stars, 1hr 22m
#1 Cheerleader Camp (2010) | 3 stars, 1hr 35m
$5 a Day (2008) | 3.4 stars, 1hr 37m
'night, Mother (1986) | 3.7 stars, 1hr 36m
'Til Death (2006-2009) | 3.7 stars, 4 Seasons
@Suicide Room (2011) | 3.4 stars, 1hr 51m
... And God Spoke (1993) | 2.8 stars, 1hr 22m
.Com for Murder (2002) | 2.8 stars, 1hr 37m
1 a Minute (2010) | 2.4 stars, 1hr 29m
1 Love (2003) | 3.5 stars, 1hr 33m
10:30 P.M. Summer (1966) | 2.7 stars, 1hr 24m
10 Dead Men (2008) | 2.5 stars, 1hr 30m
10 Items or Less (2006-2009) | 2.9 stars, 3 Seasons
10 mph (2007) | 3.2 stars, 1hr 33m
10 Questions for the Dalai Lama (2006) | 3.8 stars, 1hr 26m
10 to Midnight (1983) | 3.5 stars, 1hr 41m
10 Yards: Fantasy Football (2008) | 2.5 stars, 1hr 30m
10.5 (2004) | 3.7 stars, 2 Episodes
100 Million BC (2008) | 2.8 stars, 1hr 25m
100 Tears (2007) | 2.5 stars, 1hr 31m
100 Voices: A Journey Home (2010) | 2.7 stars, 1hr 32m
100 Women (2002) | 3.2 stars, 1hr 34m
1000 Ways to Die (2008-2009) | 3.6 stars, 2 Seasons
11/11/11 (2011) | 2.7 stars, 1hr 27m
11/4/08 (2010) | 2.4 stars, 1hr 10m
11-11-11: The Prophecy (2011) | 3 stars, 1hr 22m
12 Dates of Christmas (2011) | 3.9 stars, 1hr 27m
13 Assassins (2011) | 4 stars, 2hr 5m
13 Moons (2002) | 2.8 stars, 1hr 34m
1313: Actor Slash Model (2011) | 1.7 stars, 1hr 12m
1313: Bermuda Triangle (2011) | 1.7 stars, 1hr 14m
1313: Bigfoot Island (2011) | 1.8 stars, 1hr 16m
1313: Billy the Kid (2012) | 1.9 stars, 1hr 12m
1313: Cougar Cult (2012) | 1.7 stars, 1hr 13m
1313: Frankenqueen (2012) | 1.5 stars, 1hr 16m
1313: Hercules Unbound (2012) | 1.5 stars, 1hr 12m
1313: Night of the Widow (2012) | 1.6 stars, 1hr 16m
1313: UFO Invasion (2011) | 1.6 stars, 1hr 24m
1313: Wicked Stepbrother (2011) | 1.9 stars, 1hr 15m
13B (2009) | 2.9 stars, 2hr 8m
13Teen (2005) | 2.8 stars, 1hr 22m
16 Fathoms Deep (1948) | 2.5 stars, 1hr 18m
16 to Life (2009) | 3.4 stars, 1hr 29m
16-Love (2012) | 3.7 stars, 1hr 28m
18 to Life (2010-2011) | 3.7 stars, 2 Seasons
180° South (2010) | 4.1 stars, 1hr 26m
1911 (2011) | 3.4 stars, 1hr 39m
1981 (2009) | 2.8 stars, 1hr 42m
1984 (1984) | 3.5 stars, 1hr 50m
2 Become 1 (2006) | 2.9 stars, 1hr 37m
2 Days (2003) | 3 stars, 1hr 31m
2 Days in Paris (2007) | 2.9 stars, 1hr 40m
2 Months, $2 Million (2009) | 3.5 stars, 1 Season
2-Headed Shark Attack (2012) | 2.8 stars, 1hr 27m
20 Years After (2008) | 2.9 stars, 1hr 35m
200 Motels (1971) | 3.1 stars, 1hr 39m
200 mph (2011) | 3 stars, 1hr 32m
2000 A.D. (2000) | 2.9 stars, 1hr 44m
2010: Moby Dic (2010) | 2.5 stars, 1hr 27m
2012: An Awakening (2009) | 3.1 stars, 1hr 8m
2012: Ice Age (2011) | 2.8 stars, 1hr 31m
2012: Mayan Prophecy and the Shift of the Ages (2009) | 3 stars, 1hr 16m
2012: Science or Superstition (2008) | 3.2 stars, 1hr 18m
2012: Time for Change (2010) | 3.7 stars, 1hr 25m
2012: Zombie Apocalypse (2011) | 2.9 stars, 1hr 27m
21 Grams (2003) | 3.5 stars, 2hr 4m
21 Hours at Munich (1976) | 3.3 stars, 1hr 41m
21 Up (1977) | 3.9 stars, 1hr 39m
21 Up South Africa: Mandela's Children (2006) | 3.5 stars, 1hr 14m
24 (2001-2010) | 3.9 stars, 8 Seasons
2501 Migrants: A Journey (2009) | 3.1 stars, 54m
26 Years: The Dewey Bozella Story (2012) | 3.7 stars, 49m
28 Up (1985) | 3.9 stars, 2hr 53m
2nd Take (2011) | , 1hr 42m
3 (2010) | 3 stars, 1hr 59m
3:15 (1986) | 2.9 stars, 1hr 25m
3 Magic Words (2010) | 3.3 stars, 1hr 10m
3 Musketeers (2011) | 2.9 stars, 1hr 27m
3 Steps to Heaven (1995) | 2.2 stars, 1hr 31m
3 Women (1977) | 3.3 stars, 2hr 3m
3-Day Weekend (2008) | 2.9 stars, 1hr 23m
30,000 Leagues Under the Sea (2007) | 2.6 stars, 1hr 25m
30 Days (2005-2008) | 3.7 stars, 3 Seasons
30 for 30: Catching Hell (2011) | 4 stars, 1hr 48m
30 for 30: Charismatic (2011) | 3 stars, 51m
30 for 30: Fernando Nation (2010) | 3.8 stars, 53m
30 for 30: Four Days in October (2010) | 4.1 stars, 52m
30 for 30: Guru of Go (2010) | 3.8 stars, 53m
30 for 30: Into the Wind (2010) | 3.8 stars, 56m
30 for 30: Jordan Rides the Bus (2010) | 3.8 stars, 54m
30 for 30: June 17th, 1994 (2010) | 3.6 stars, 52m
30 for 30: Kings Ransom (2009) | 3.6 stars, 52m
30 for 30: Little Big Men (2010) | 3.4 stars, 52m
30 for 30: Marion Jones: Press Pause (2010) | 3.5 stars, 53m
30 for 30: Muhammad and Larry (2009) | 3.6 stars, 53m
30 for 30: No Crossover: The Trial of Allen Iverson (2010) | 3.7 stars, 1hr 22m
30 for 30: Once Brothers (2010) | 4.1 stars, 1hr 22m
30 for 30: Pony Excess (2010) | 4 stars, 1hr 46m
30 for 30: Renée (2011) | 2.9 stars, 1hr 18m
30 for 30: Roll Tide / War Eagle (2011) | 3.9 stars, 53m
30 for 30: Run Ricky Run (2010) | 3.9 stars, 53m
30 for 30: Silly Little Game (2010) | 3.4 stars, 52m
30 for 30: Small Potatoes: Who Killed the USFL? (2009) | 3.7 stars, 53m
30 for 30: The 16th Man (2010) | 3.8 stars, 53m
30 for 30: The Band That Wouldn't Die (2009) | 3.4 stars, 54m
30 for 30: The Best That Never Was (2010) | 4 stars, 1hr 41m
30 for 30: The Birth of Big Air (2010) | 4 stars, 54m
30 for 30: The Dotted Line (2011) | 3.6 stars, 56m
30 for 30: The House of Steinbrenner (2010) | 3.4 stars, 53m
30 for 30: The Real Rocky (2011) | 3.8 stars, 57m
30 for 30: The Two Escobars (2010) | 4.2 stars, 1hr 45m
30 for 30: The U (2010) | 4.1 stars, 1hr 43m
30 for 30: Tim Richmond: To The Limit (2010) | 3.6 stars, 53m
30 for 30: Unguarded (2011) | 4.2 stars, 1hr 20m
30 for 30: Unmatched (2010) | 3.2 stars, 53m
30 for 30: Winning Time: Reggie Miller vs. The New York Knicks (2010) | 4.2 stars, 1hr 10m
30 for 30: Without Bias (2009) | 3.9 stars, 52m
...
Foreign Parts (2010) | 2.1 stars, 1hr 20m
Foreign Student (1994) | 2.7 stars, 1hr 35m
Foreigner: Alive & Rockin' (2007) | 3.5 stars, 1hr 8m
Forensics: You Decide (2009) | 3.9 stars, 1 Season
Forest of Bandi: Fireflies (2011) | , 30m
Forever Female (1954) | 3.4 stars, 1hr 33m
Forever Lulu (2000) | 2.8 stars, 1hr 42m
Forever Plaid: The Movie (2008) | 3.3 stars, 1hr 25m
Forever Strong (2008) | 4.2 stars, 1hr 49m
Forever Young (1992) | 3.5 stars, 1hr 41m
Forget Me Not (2009) | 3.4 stars, 1hr 43m
Forgiven (2011) | 3.4 stars, 1hr 35m
Forgiving Dr. Mengele (2006) | 3.6 stars, 1hr 19m
Forks Over Knives (2011) | 4.2 stars, 1hr 36m
Forks Over Knives Presents: The Engine 2 Kitchen Rescue (2011) | 3.8 stars, 52m
Fort Dodge Stampede (1951) | 3.3 stars, 59m
Fort Massacre (1958) | 3.4 stars, 1hr 20m
Fort Utah (1967) | 3.4 stars, 1hr 23m
Four Days (1999) | 2.8 stars, 1hr 28m
Four Days in November (1964) | 3.7 stars, 2hr 2m
Four Faces West (1948) | 3.5 stars, 1hr 30m
Four Lions (2010) | 3.4 stars, 1hr 41m
Four Rooms (1995) | 3.6 stars, 1hr 37m
Foxfire Light (1982) | 3.5 stars, 1hr 37m
Foxy Brown (1974) | 3.4 stars, 1hr 31m
Fräulein Doktor (1969) | 2.9 stars, 1hr 41m
Fraggle Rock (1983-1986) | 3.7 stars, 5 Seasons
Fragile (2005) | 3.4 stars, 1hr 41m
Fragments (2008) | 3 stars, 1hr 35m
Frame 313: The JFK Assassination Theories (2008) | 3.7 stars, 1hr 54m
Frances: Bedtime for Frances (2009) | 3.2 stars, 46m
Frank & Jesse (1995) | 3.6 stars, 1hr 46m
Frank Caliendo: All Over the Place (2007) | 3.5 stars, 1hr 9m
Frank Lloyd Wright (1998) | 3.8 stars, 2hr 36m
Frank Zappa: Apostrophe /Over-Nite Sensation (2007) | 3.6 stars, 49m
Frankensteer (2006) | 3.7 stars, 44m
Frankenstein (2004) | 3.1 stars, 1hr 27m
Frankenstein (2004) | 3.3 stars, 2 Episodes
Franny's Feet (2003) | 3.5 stars, 18 Episodes
Frasier (1993-2003) | 3.8 stars, 11 Seasons
Fraternity House (2008) | 2.4 stars, 1hr 21m
Frauds (1993) | 2.1 stars, 1hr 33m
Fraulein (2006) | 3.2 stars, 1hr 15m
Freak Encounters (2010) | 3.3 stars, 1 Season
Freakonomics (2010) | 3.7 stars, 1hr 33m
Freaks and Geeks (1999) | 4.6 stars, 18 Episodes
Freakshow (2007) | 2.7 stars, 1hr 25m
Freaky Eaters (2010) | , 1 Season
Freaky Faron (2006) | 2.9 stars, 1hr 16m
Fred: The Movie (2010) | 3 stars, 1hr 22m
Fred 2: Night of the Living Fred (2011) | 3.4 stars, 1hr 22m
Fred 3: Camp Fred (2012) | 3.4 stars, 1hr 20m
Freddy Frogface (2011) | 3.3 stars, 1hr 24m
Free Money (1998) | 3 stars, 1hr 33m
Free Radicals (2003) | 2.5 stars, 1hr 54m
Free Willy (1993) | 3.3 stars, 1hr 51m
Free Willy 2: The Adventure Home (1995) | 3.5 stars, 1hr 37m
Free Zone (2005) | 2.6 stars, 1hr 30m
Freedom Fries: And Other Stupidity We'll Have to Explain to Our Grandchildren (2005) | 3.3 stars, 51m
Freedom Writers (2007) | 4 stars, 2hr 2m
...
Young Buffalo Bill (1940) | 3.2 stars, 53m
Young Cesar (2007) | 3.5 stars, 1hr 18m
Young Fury (1965) | 3.1 stars, 1hr 19m
Young Goethe in Love (2010) | 3.6 stars, 1hr 44m
Young Hercules (1997) | 3.2 stars, 1hr 32m
Youngblood (1978) | 3.6 stars, 1hr 26m
Yours, Mine and Ours (2005) | 3.5 stars, 1hr 28m
ZA: Zombies Anonymous (2005) | 2.6 stars, 1hr 48m
Zach Galifianakis: Live at the Purple Onion (2006) | 3.7 stars, 1hr 1m
Zack and Reba (1998) | 2.7 stars, 1hr 31m
Zeitgeist: The Movie (2007) | 4.1 stars, 1hr 56m
Zeke's Pad (2008) | , 1 Season
Zen of Sword (1992) | 3 stars, 1hr 28m
Zenith (2010) | 2.9 stars, 1hr 32m
Zero Bridge (2008) | , 1hr 37m
Zero Day (2003) | 3.5 stars, 1hr 31m
Zero Kelvin (1995) | 3.1 stars, 1hr 54m
Zero to Sixty (1978) | 2.7 stars, 1hr 41m
Zodiac: The Race Begins (2006) | 2.8 stars, 1hr 24m
Zombie Farm (2009) | 2.4 stars, 1hr 29m
Zombie Girl: The Movie (2009) | 3 stars, 1hr 31m
Zombie Nation (2004) | 2 stars, 1hr 21m
Zombie Town (2007) | 2.6 stars, 1hr 27m
Zombie Women of Satan (2009) | 2.2 stars, 1hr 24m
Zombies of Mass Destruction (2009) | 2.8 stars, 1hr 29m
Zombies Zombies Zombies (2008) | 2.5 stars, 1hr 22m
Zombiethon (1986) | 2.6 stars, 1hr 13m
Zonad (2009) | 2.4 stars, 1hr 15m
Zone Troopers (1985) | 3 stars, 1hr 25m
Zoo Baby (1960) | 2.2 stars, 59m
Zu Warriors (2001) | 3.2 stars, 1hr 20m
Zulu (1964) | 3.8 stars, 2hr 18m
Zulu Dawn (1979) | 3.5 stars, 1hr 57m
ZZ Top: Live from Texas (2008) | 3.7 stars, 1hr 25m
Explanation / Answer
Hello, I have already answered a similar question once, so I’m referring my own solution for you. Implemented everything as per the requirements. Defined following things in this answer.
Created abstract class Media with all the basic attributes and methods
Extended Movie class from Media
Extended Series class from Media
Created a class Filter for filters
Created DataFileReader class
Loaded data from a file called ‘NetflixUSA_Oct15_cleaned.txt’
Created a main class Client, displayed a menu, prompted the user, and did everything as per the requirements
If you have any doubts, feel free to ask, Thanks
EDIT: I’m getting troubles submitting the answer without losing the format. Showing character limit exceeded error. So I have to paste it as a plain text, which will cause the loss of code formatting and indentations. Sorry for the trouble. If you are using eclipse ,copy the code and press ctrl+shift+F to format the code
// Client.java
import java.io.FileNotFoundException;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.Scanner;
public class Client {
static Scanner scanner;
public static void main(String[] args) {
try {
DataFileReader dataFileReader = new DataFileReader(
"NetflixUSA_Oct15_cleaned.txt");
/**
* reading data from input file
*/
dataFileReader.loadDataFromFile();
scanner = new Scanner(System.in);
int choice = -1;
/**
* Looping until user chooses to quit
*/
while (choice != 4) {
printMenu();
try {
choice = Integer.parseInt(scanner.nextLine());
if (choice == 1) {
// displaying the list
dataFileReader.printList();
} else if (choice == 2) {
// adding filter
System.out.println("Enter the filter to be added");
String fltr = scanner.nextLine();
/**
* splitting the input using whitespaces into string
* array
*/
String[] splittedValues = fltr.split(" ");
/**
* verifying the input has 3 fields
*/
if (splittedValues.length == 3) {
String field = splittedValues[0]; // ex: rating
String relation = splittedValues[1]; // ex: >
String target = splittedValues[2]; // ex: 3
/**
* validating and creating the filter
*/
Filter filter = validateFilter(field, relation,
target);
if (filter == null) {
System.out.println("filter not added");
} else {
/**
* applying the filter
*/
dataFileReader.applyFilter(filter);
System.out.println("Filter applied");
}
} else {
System.out.println("Invalid filter");
}
} else if (choice == 3) {
// removing filter
System.out.println("Enter the filter to be removed");
String fltr = scanner.nextLine();
String[] splittedValues = fltr.split(" ");
if (splittedValues.length == 3) {
String field = splittedValues[0];
String relation = splittedValues[1];
String target = splittedValues[2];
Filter filter = validateFilter(field, relation,
target);
if (filter == null) {
System.out.println("filter not removed");
} else {
if (dataFileReader.removeFilter(filter)) {
System.out.println("Filter removed");
} else {
System.out.println("Filter not found!");
}
}
} else {
System.out.println("Invalid filter");
}
}
} catch (Exception e) {
System.out.println("Enter a valid choice..");
}
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
/**
* method to validate a filter entered by the user
*
* @param field
* - which field
* @param relation
* - type of relation (=,>,<)
* @param target
* - target value
* @return a Filter object if the filter is valid, else null
*/
public static Filter validateFilter(String field, String relation,
String target) {
Filter filter = null;
if (field.equalsIgnoreCase("year")) {
if (relation.equals("<") || relation.equals(">")
|| relation.equals("=")) {
try {
int year = Integer.parseInt(target);
if (year < 0) {
throw new Exception();
} else {
filter = new Filter(field, relation, target);
}
} catch (Exception e) {
System.out.println("Invalid year");
}
} else {
System.out.println("Invalid relation");
}
} else if (field.equalsIgnoreCase("rating")) {
if (relation.equals("<") || relation.equals(">")
|| relation.equals("=")) {
try {
double rating = Double.parseDouble(target);
if (rating < 0) {
throw new Exception();
} else {
filter = new Filter(field, relation, target);
// dataFileReader.applyFilter(filter);
}
} catch (Exception e) {
System.out.println("Invalid rating");
}
} else {
System.out.println("Invalid relation");
}
} else if (field.equalsIgnoreCase("genre")) {
if (relation.equals("=")) {
if (target.equalsIgnoreCase("movie")
|| target.equalsIgnoreCase("series")) {
filter = new Filter(field, relation, target);
} else {
System.out.println("Invalid genre");
}
} else {
System.out.println("Invalid relation");
}
} else if (field.equalsIgnoreCase("title")) {
if (relation.equals("=")) {
filter = new Filter(field, relation, target);
} else {
System.out.println("Invalid relation");
}
}
return filter;
}
/**
* method to display the menu
*/
public static void printMenu() {
System.out.println("1. Display the list");
System.out.println("2. Apply a filter");
System.out.println("3. Remove a filter");
System.out.println("4. Quit");
System.out.println(" Enter your choice");
}
}
// DataFileReader.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class DataFileReader {
Scanner scanner;
ArrayList<Media> mediaList;
ArrayList<Filter> filterList;
public DataFileReader(String filename) throws FileNotFoundException {
File file = new File(filename);
scanner = new Scanner(new FileReader(file));
mediaList = new ArrayList<Media>();
filterList = new ArrayList<Filter>();
}
/**
* method to load data from a file
*/
public void loadDataFromFile() {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] cols = line.split("\|");
String title = cols[0].trim();
String attrs[] = cols[1].split(",");
double rating = parseRatingFromString(attrs[0]);
if (isMovie(attrs[1])) {
double length = parseLengthFromString(attrs[1]);
int year = stripYearsFromString(title)[0];
Movie movie = new Movie(title, rating, year, length);
mediaList.add(movie);
} else {
Scanner scanner = new Scanner(attrs[1]);
int num = scanner.nextInt();
String type = scanner.next();
int[] years = stripYearsFromString(title);
Series series = new Series(title, rating, years[0], num, type,
years[1]);
mediaList.add(series);
}
}
}
/**
* method to parse rating from a string file
*/
public double parseRatingFromString(String str) {
double r = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
r = Double.parseDouble(str.charAt(i) + "");
if (i + 2 < str.length()) {
if (str.charAt(i + 1) == '.') {
if (Character.isDigit(str.charAt(i + 2))) {
r += Double.parseDouble("0." + str.charAt(i + 2));
return r;
}
} else {
return r;
}
}
}
}
return r;
}
/**
* Method to add a filter to the list
* @param filter
*/
public void applyFilter(Filter filter) {
if (!filterList.contains(filter)) {
filterList.add(filter);
}
}
public boolean removeFilter(Filter filter){
if(filterList.contains(filter)){
filterList.remove(filterList.indexOf(filter));
return true;
}else {
return false;
}
}
/**
* method to check if it is a movie or series,from a string
*/
public boolean isMovie(String str) {
if (str.toLowerCase().contains("seasons")
|| str.toLowerCase().contains("episodes")) {
return false;
}
return true;
}
/**
* method to strip a movie duration from string
*/
public double parseLengthFromString(String str) {
double length = 0;
if (str.contains("hr")) {
int i = str.indexOf("hr");
if (i - 1 >= 0) {
length = Double.parseDouble(str.charAt(i - 1) + "");
}
}
if (str.contains("m")) {
int i = str.indexOf("m");
if (i - 2 >= 0) {
if (Character.isDigit(str.charAt(i - 2))
&& Character.isDigit(str.charAt(i - 1))) {
length += Double.parseDouble("0." + str.charAt(i - 2)
+ str.charAt(i - 1));
} else if (Character.isDigit(str.charAt(i - 1))) {
length += Double.parseDouble("0." + str.charAt(i - 1));
}
}
}
return length;
}
/**
* Method to print the list
*/
public void printList() {
ArrayList<Media> filteredList = new ArrayList<Media>();
for(Media m: mediaList){
boolean objection=false;
for (Filter f : filterList) {
if (f.getField().equals("year")) {
/**
* Year wise filter
*/
char c = f.getRelation().charAt(0);
if (c == '>') {
int y = Integer.parseInt(f.getTarget());
if(m.getYear()<y){
objection=true;
}
}else if (c == '<') {
int y = Integer.parseInt(f.getTarget());
if(m.getYear()>=y){
objection=true;
}
}else if (c == '=') {
int y = Integer.parseInt(f.getTarget());
if(m.getYear()!=y){
objection=true;
}
}
}else if (f.getField().equals("rating")) {
/**
* applying rating wise filter
*/
char c = f.getRelation().charAt(0);
if (c == '>') {
double y = Double.parseDouble(f.getTarget());
if (m.getRating() < y) {
objection=true;
}
}else if (c == '<') {
double y = Double.parseDouble(f.getTarget());
if (m.getRating() >= y) {
objection=true;
}
}else if (c == '=') {
double y = Double.parseDouble(f.getTarget());
if (m.getRating() != y) {
objection=true;
}
}
}else if (f.getField().equals("genre")) {
/**
* Applying genre filter
*/
String type=f.getTarget();
if (type.equalsIgnoreCase("movie")) {
if(! (m instanceof Movie) ){
objection=true;
}
}else if (type.equalsIgnoreCase("series")) {
if(! (m instanceof Series) ){
objection=true;
}
}
}
}
if(!objection){
filteredList.add(m);
}
}
/**
* Displaying the filtered list
*/
for(Media m:filteredList){
System.out.println(m.toString());
}
}
/**method to strip the years from a string */
public int[] stripYearsFromString(String str) {
int[] years = new int[2];
int i = str.lastIndexOf("(");
int year1 = Integer.parseInt(str.substring(i + 1, i + 5));
years[0] = year1;
if (str.charAt(i + 5) == '-') {
int year2 = Integer.parseInt(str.substring(i + 6, i + 10));
years[1] = year2;
}
return years;
}
}
// Filter.java
/**
* class to represent a Filter
*/
public class Filter {
String field, relation, target;
// constructor with arguments
public Filter(String field, String relation, String target) {
this.field = field;
this.relation = relation;
this.target = target;
}
// constructor with no arguments
public Filter() {
this.field = "";
this.relation = "";
this.target = "";
}
@Override
public boolean equals(Object o) {
if (o instanceof Filter) {
Filter f = (Filter) o;
if (f.field.equals(this.field) && f.relation.equals(this.relation)
&& f.target.equals(target)) {
//filters are equal
return true;
}
}
return false;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getRelation() {
return relation;
}
public void setRelation(String relation) {
this.relation = relation;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}
// Media.java
public abstract class Media {
private String title;
private double rating;
private int year;
// def constructor
public Media() {
title = "";
rating = 0;
year = 0;
}
// constructor with arguments
public Media(String title, double rating, int year) {
this.title = title;
this.rating = rating;
this.year = year;
}
// getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "Title: " + title + ", Rating: " + rating;
}
}
// Movie.java
public class Movie extends Media{
private double length;
// constructor with no arguments
public Movie() {
super();
}
// constructor with arguments
public Movie(String title, double rating,int year,double length) {
super(title, rating,year);
this.length=length;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public String toString() {
return super.toString()+", Length: "+String.format("%.2f h", length);
}
}
// Series.java
public class Series extends Media {
private int num;
private String type;
private int endYear;
// constructor with no arguments
public Series() {
super();
}
// constructor with arguments
public Series(String title, double rating,int year, int seasons, String type,int end) {
super(title, rating,year);
this.num=seasons;
this.type=type;
endYear=end;
}
public int getNum() {
return num;
}
public void setNum(int seasons) {
this.num = seasons;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getEndYear() {
return endYear;
}
public void setEndYear(int endYear) {
this.endYear = endYear;
}
@Override
public String toString() {
return super.toString()+", "+type+": "+num;
}
}
/*OUTPUT*/
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
1
Title: !Women Art Revolution (2010), Rating: 3.0, Length: 1.22 h
Title: #1 Cheerleader Camp (2010), Rating: 3.0, Length: 1.35 h
Title: $5 a Day (2008), Rating: 3.4, Length: 1.37 h
Title: 'night, Mother (1986), Rating: 3.7, Length: 1.36 h
Title: 'Til Death (2006-2009), Rating: 3.7, Seasons: 4
Title: @Suicide Room (2011), Rating: 3.4, Length: 1.51 h
Title: ... And God Spoke (1993), Rating: 2.8, Length: 1.22 h
Title: .Com for Murder (2002), Rating: 2.8, Length: 1.37 h
Title: 1 a Minute (2010), Rating: 2.4, Length: 1.29 h
Title: 1 Love (2003), Rating: 3.5, Length: 1.33 h
Title: 10:30 P.M. Summer (1966), Rating: 2.7, Length: 1.24 h
Title: 10 Dead Men (2008), Rating: 2.5, Length: 1.30 h
Title: 10 Items or Less (2006-2009), Rating: 2.9, Seasons: 3
Title: 10 mph (2007), Rating: 3.2, Length: 1.33 h
Title: 10 Questions for the Dalai Lama (2006), Rating: 3.8, Length: 1.26 h
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
2
Enter the filter to be added
rating > 3
Filter applied
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
1
Title: !Women Art Revolution (2010), Rating: 3.0, Length: 1.22 h
Title: #1 Cheerleader Camp (2010), Rating: 3.0, Length: 1.35 h
Title: $5 a Day (2008), Rating: 3.4, Length: 1.37 h
Title: 'night, Mother (1986), Rating: 3.7, Length: 1.36 h
Title: 'Til Death (2006-2009), Rating: 3.7, Seasons: 4
Title: @Suicide Room (2011), Rating: 3.4, Length: 1.51 h
Title: 1 Love (2003), Rating: 3.5, Length: 1.33 h
Title: 10 mph (2007), Rating: 3.2, Length: 1.33 h
Title: 10 Questions for the Dalai Lama (2006), Rating: 3.8, Length: 1.26 h
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
2
Enter the filter to be added
genre = movies
Invalid genre
filter not added
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
2
Enter the filter to be added
genre = movie
Filter applied
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
1
Title: !Women Art Revolution (2010), Rating: 3.0, Length: 1.22 h
Title: #1 Cheerleader Camp (2010), Rating: 3.0, Length: 1.35 h
Title: $5 a Day (2008), Rating: 3.4, Length: 1.37 h
Title: 'night, Mother (1986), Rating: 3.7, Length: 1.36 h
Title: @Suicide Room (2011), Rating: 3.4, Length: 1.51 h
Title: 1 Love (2003), Rating: 3.5, Length: 1.33 h
Title: 10 mph (2007), Rating: 3.2, Length: 1.33 h
Title: 10 Questions for the Dalai Lama (2006), Rating: 3.8, Length: 1.26 h
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
3
Enter the filter to be removed
rating > 3
Filter removed
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
1
Title: !Women Art Revolution (2010), Rating: 3.0, Length: 1.22 h
Title: #1 Cheerleader Camp (2010), Rating: 3.0, Length: 1.35 h
Title: $5 a Day (2008), Rating: 3.4, Length: 1.37 h
Title: 'night, Mother (1986), Rating: 3.7, Length: 1.36 h
Title: @Suicide Room (2011), Rating: 3.4, Length: 1.51 h
Title: ... And God Spoke (1993), Rating: 2.8, Length: 1.22 h
Title: .Com for Murder (2002), Rating: 2.8, Length: 1.37 h
Title: 1 a Minute (2010), Rating: 2.4, Length: 1.29 h
Title: 1 Love (2003), Rating: 3.5, Length: 1.33 h
Title: 10:30 P.M. Summer (1966), Rating: 2.7, Length: 1.24 h
Title: 10 Dead Men (2008), Rating: 2.5, Length: 1.30 h
Title: 10 mph (2007), Rating: 3.2, Length: 1.33 h
Title: 10 Questions for the Dalai Lama (2006), Rating: 3.8, Length: 1.26 h
1. Display the list
2. Apply a filter
3. Remove a filter
4. Quit
Enter your choice
4