In: Computer Science
Hi there,
I am having a bit of an issue that I just can't seem to figure it out. My code is supposed to read from another file and then have an output of a number for each categorie
////////Required Output///////
Movies in Silent Era: 0\n Movies in Pre-Golden Era: 7\n Movies in Golden Era: 581\n Movies in Change Era: 3445\n Movies in Modern Era: 10165\n Movies in New Millennium Era: 15457\n
My program is:
import java.util.ArrayList; public class MovieReducerEraCount implements MediaReducer { private String result; public String reduce(ArrayList<Media> list, String era) { this.result = result; int counter = 0; for (int year = 0; year < list.size(); year++) { if (list.get(year).getName().equals(era)) { counter++; } } result = "Movies in " + era + ": " + counter; return result; } }
/////////Movie.java//////////
public class Movie extends Media { public Movie(String name, int year, String genre) { super(name, year, genre); } @Override public String getEra() { if (getYear() >= 2000) { return "New Millennium Era"; } else if (getYear() >= 1977) { return "Modern Era"; } else if (getYear() >= 1955) { return "Change Era"; } else if (getYear() >= 1941) { return "Golden Era"; } return "Pre-Golden Era"; } @Override public boolean wasReleasedAfter(Media other) { return getYear() > other.getYear(); } @Override public boolean wasReleasedBeforeOrInSameYear(Media other) { return getYear() <= other.getYear(); } }
///////move_list.txt///////
!Next? 1994 Documentary #1 Single 2006 Reality-TV #ByMySide 2012 Drama #Follow 2011 Mystery #nitTWITS 2011 Comedy $#*! My Dad Says 2010 Comedy $1,000,000 Chance of a Lifetime 1986 Game-Show $100 Makeover 2010 Reality-TV $100 Taxi Ride 2001 Documentary $100,000 Name That Tune 1984 Game-Show $100,000 Name That Tune 1984 Music $2 Bill 2002 Documentary $2 Bill 2002 Music $2 Bill 2002 Music $2 Bill 2002 Music $2 Bill 2002 Music $25 Million Dollar Hoax 2004 Reality-TV $40 a Day 2002 Documentary $5 Cover 2009 Drama $5 Cover: Seattle 2009 Drama $50,000 Letterbox 1980 Game-Show $9.99 2003 Adventure $weepstake$ 1979 Drama ' Horse Trials ' 2011 Sport '80s Videos: A to Z 2009 Music 'Allo 'Allo! 1982 Comedy 'Allo 'Allo! 1982 War 'Conversations with My Wife' 2010 Comedy 'Da Kink in My Hair 2007 Comedy
I've shorten the file.
/***************************MediaReducer.java*****************************/
import java.util.ArrayList;
public interface MediaReducer {
public String reduce(ArrayList<Media> list,
String era);
}
/***********************************MediaReducerEraCount.java*************************************/
import java.util.ArrayList;
public class MovieReducerEraCount implements MediaReducer {
public String reduce(ArrayList<Media> list, String era) {
String result = "";
int counter = 0;
for (Media media : list) {
if (media.getEra().equalsIgnoreCase(era)) {
counter++;
}
}
result = "Movies in " + era + ": "
+ counter;
return result;
}
}
/*******************************************Media.java**********************************/
public abstract class Media {
private String name;
private int year;
private String genre;
public Media(String name, int year, String genre) {
this.name = name;
this.year = year;
this.genre = genre;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
public final int getYear() {
return year;
}
public final void setYear(int year) {
this.year = year;
}
public final String getGenre() {
return genre;
}
public final void setGenre(String genre) {
this.genre = genre;
}
public abstract String getEra();
public abstract boolean wasReleasedAfter(Media other);
public abstract boolean wasReleasedBeforeOrInSameYear(Media other);
}
/********************************************Movie.java***********************************/
public class Movie extends Media {
public Movie(String name, int year, String genre)
{
super(name, year, genre);
}
@Override
public String getEra() {
if (getYear() >= 2000) {
return "New
Millennium Era";
} else if (getYear() >= 1977)
{
return "Modern
Era";
} else if (getYear() >= 1955)
{
return "Change
Era";
} else if (getYear() >= 1941)
{
return "Golden
Era";
}
return "Pre-Golden Era";
}
@Override
public boolean wasReleasedAfter(Media other)
{
return getYear() >
other.getYear();
}
@Override
public boolean wasReleasedBeforeOrInSameYear(Media
other) {
return getYear() <=
other.getYear();
}
}
/*******************************************TestMovie.java**********************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class TestMovie {
public static void main(String[] args) {
ArrayList<Media> list =
new ArrayList<>();
MediaReducer mediaReducer = new
MovieReducerEraCount();
File file = new
File("move_list.txt");
try {
Scanner sc =
new Scanner(file);
// while loop
till file has line
while
(sc.hasNextLine()) {
// read the line
String name = sc.nextLine();
int year =
Integer.parseInt(sc.nextLine());
String genre = sc.nextLine();
list.add(new Movie(name, year, genre));
}
sc.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
System.out.println(mediaReducer.reduce(list, "Silent Era"));
System.out.println(mediaReducer.reduce(list, "Pre-Golden
Era"));
System.out.println(mediaReducer.reduce(list, "Golden Era"));
System.out.println(mediaReducer.reduce(list, "Change Era"));
System.out.println(mediaReducer.reduce(list, "Modern Era"));
System.out.println(mediaReducer.reduce(list, "New Millennium
Era"));
}
}
/*********************************************output***************************/
Movies in Silent Era: 0
Movies in Pre-Golden Era: 0
Movies in Golden Era: 0
Movies in Change Era: 0
Movies in Modern Era: 8
Movies in New Millennium Era: 21
Please let me know if you have any doubt or modify the answer, Thanks :)