0% found this document useful (0 votes)
2 views10 pages

Message 21

The document is a Java program that implements a movie database system, including classes for movies, actors, directors, genres, and error handling. It parses CSV files containing data about these entities and reports any errors encountered during parsing. The main method initializes the program, reads the files, and displays error information if any issues arise.

Uploaded by

ahgold81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Message 21

The document is a Java program that implements a movie database system, including classes for movies, actors, directors, genres, and error handling. It parses CSV files containing data about these entities and reports any errors encountered during parsing. The main method initializes the program, reads the files, and displays error information if any issues arise.

Uploaded by

ahgold81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package [Link].

deisimdb;

import [Link].*;
import [Link].*;
import [Link];

enum TipoEntidade {
FILME,
ATOR,
REALIZADOR,
GENERO_CINEMATOGRAFICO,
INPUT_INVALIDO
}

class Movie {
int id;
String title;
Date releaseDate;
int duration;
double budget;

public Movie(int id, String title, Date releaseDate, int duration, double
budget) {
[Link] = id;
[Link] = title;
[Link] = releaseDate;
[Link] = duration;
[Link] = budget;
}

@Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String result = id + " | " + title + " | " +
[Link](releaseDate);

if (id < 1000) {


int actorCount = 0;
for (Actor actor : [Link]) {
if ([Link] == [Link]) {
actorCount++;
}
}
result += " | " + actorCount;
}

return result;
}
}

class Actor {
int id;
String name;
char gender;
int movieId;

public Actor(int id, String name, char gender, int movieId) {


[Link] = id;
[Link] = name;
[Link] = gender;
[Link] = movieId;
}

@Override
public String toString() {
String genderString = (gender == 'M') ? "Masculino" : "Feminino";
return id + " | " + name + " | " + genderString + " | " + movieId;
}
}

class Director {
int id;
String name;
int movieId;

public Director(int id, String name, int movieId) {


[Link] = id;
[Link] = name;
[Link] = movieId;
}

@Override
public String toString() {
return id + " | " + name + " | " + movieId;
}
}

class Genre {
int id;
String name;

public Genre(int id, String name) {


[Link] = id;
[Link] = name;
}

@Override
public String toString() {
return id + " |" + " " + ([Link](0) == ' ' ? [Link](1) :
name);
}
}

class ErrorInfo {
String fileName;
int validLines;
int invalidLines;
int firstErrorLine;

public ErrorInfo(String fileName, int validLines, int invalidLines, int


firstErrorLine) {
[Link] = fileName;
[Link] = validLines;
[Link] = invalidLines;
[Link] = firstErrorLine;
}

@Override
public String toString() {
return fileName + " | " + validLines + " | " + invalidLines + " | " +
firstErrorLine;
}
}

public class Main {

static ArrayList<Movie> movies = new ArrayList<>();


static ArrayList<Actor> actors = new ArrayList<>();
static ArrayList<Director> directors = new ArrayList<>();
static ArrayList<Genre> genres = new ArrayList<>();
static ArrayList<ErrorInfo> errorInfoList = new ArrayList<>();

public static boolean parseFiles(File folder) {


movies = new ArrayList<>();
actors = new ArrayList<>();
directors = new ArrayList<>();
genres = new ArrayList<>();
errorInfoList = new ArrayList<>();

if (![Link]() || ![Link]()) {
return false;
}

String[] fileNames = {
"[Link]",
"[Link]",
"[Link]",
"[Link]",
"genres_movies.csv",
"movie_votes.csv"
};

boolean allFilesExist = true;

for (String fileName : fileNames) {


File file = new File(folder, fileName);
if (![Link]() || ![Link]() || ![Link](".csv")) {
allFilesExist = false;
break;
}
}

if (!allFilesExist) {
return false;
}

parseMovies(new File(folder, "[Link]"));


parseActors(new File(folder, "[Link]"));
parseDirectors(new File(folder, "[Link]"));
parseGenres(new File(folder, "[Link]"));
parseGenresMovies(new File(folder, "genres_movies.csv"));
parseMoviesVotes(new File(folder, "movie_votes.csv"));

return true;
}

static void parseMovies(File file) {


int validLines = 0;
int invalidLines = 0;
int firstErrorLine = -1;

try {
Scanner scanner = new Scanner(file);
int lineNumber = 0;

if ([Link]()) {
[Link]();
}

while ([Link]()) {
lineNumber++;
String line = [Link]();

if ([Link]()) {
continue;
}

try {
String[] parts = [Link](",");

if ([Link] < 5) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
continue;
}

int id = [Link](parts[0]);
String title = parts[1];

int duration;
try {
duration = [Link](parts[2]);
} catch (NumberFormatException e) {
duration = (int) [Link](parts[2]);
}

double budget = [Link](parts[3]);

String dateStr = parts[4];


SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MM-
yyyy");
Date releaseDate = [Link](dateStr);

boolean isDuplicate = false;


for (Movie movie : movies) {
if ([Link] == id) {
isDuplicate = true;
break;
}
}

if (!isDuplicate) {
[Link](new Movie(id, title, releaseDate, duration,
budget));
}

validLines++;
} catch (Exception e) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
}
}
[Link]();
} catch (FileNotFoundException e) {
invalidLines = 1;
firstErrorLine = 1;
}

if (invalidLines == 0) {
firstErrorLine = -1;
}

[Link](new ErrorInfo("[Link]", validLines, invalidLines,


firstErrorLine));
}
static void parseActors(File file) {
int validLines = 0;
int invalidLines = 0;
int firstErrorLine = -1;

try {
Scanner scanner = new Scanner(file);
int lineNumber = 0;

if ([Link]()) {
[Link]();
}

while ([Link]()) {
lineNumber++;
String line = [Link]();

if ([Link]()) {
continue;
}

try {
String[] parts = [Link](",");

if ([Link] != 4 || parts[0].isEmpty() ||
parts[1].isEmpty() ||
parts[2].isEmpty() || parts[3].isEmpty()) {

invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
continue;
}

int id = [Link](parts[0]);
String name = parts[1];
char gender = parts[2].charAt(0);
int movieId = [Link](parts[3]);

[Link](new Actor(id, name, gender, movieId));


validLines++;
} catch (Exception e) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
}
}
[Link]();
} catch (FileNotFoundException e) {
invalidLines = 1;
firstErrorLine = 1;
}

if (invalidLines == 0) {
firstErrorLine = -1;
}

[Link](new ErrorInfo("[Link]", validLines, invalidLines,


firstErrorLine));
}

static void parseDirectors(File file) {


int validLines = 0;
int invalidLines = 0;
int firstErrorLine = -1;

try {
Scanner scanner = new Scanner(file);

if ([Link]()) {
[Link]();
}

int lineNumber = 0;

while ([Link]()) {
lineNumber++;
String line = [Link]();

if ([Link]()) {
continue;
}

try {
String[] parts = [Link](",");

if ([Link] != 3 ||
parts[0].isEmpty() || parts[1].isEmpty() ||
parts[2].isEmpty()) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
continue;
}

int id = [Link](parts[0]);
String name = parts[1];
int movieId = [Link](parts[2]);

[Link](new Director(id, name, movieId));


validLines++;
} catch (Exception e) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
}
}
[Link]();
} catch (FileNotFoundException e) {
invalidLines = 1;
firstErrorLine = 1;
}

if (invalidLines == 0) {
firstErrorLine = -1;
}

[Link](new ErrorInfo("[Link]", validLines, invalidLines,


firstErrorLine));
}

static void parseGenres(File file) {


int validLines = 0;
int invalidLines = 0;
int firstErrorLine = -1;
try {
Scanner scanner = new Scanner(file);
int lineNumber = 0;
if ([Link]()) {
[Link]();
}
while ([Link]()) {
lineNumber++;
String line = [Link]();
if ([Link]()) {
continue;
}
try {
String[] initialParts = [Link](",", 2);

if ([Link] < 2 || initialParts[0].isEmpty() ||


initialParts[1].isEmpty()) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
continue;
}
String[] allParts = [Link](",");
if ([Link] > 2) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
continue;
}

int id = [Link](initialParts[0]);
String name = initialParts[1];
[Link](new Genre(id, name));
validLines++;
} catch (Exception e) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
}
}
[Link]();
} catch (FileNotFoundException e) {
invalidLines = 1;
firstErrorLine = 1;
}
if (invalidLines == 0) {
firstErrorLine = -1;
}
[Link](new ErrorInfo("[Link]", validLines, invalidLines,
firstErrorLine));
}
static void parseGenresMovies(File file) {
int validLines = 0;
int invalidLines = 0;
int firstErrorLine = -1;

try {
Scanner scanner = new Scanner(file);
int lineNumber = 0;

if ([Link]()) {
[Link]();
lineNumber++;
}

while ([Link]()) {
lineNumber++;
String line = [Link]();
try {
String[] parts = [Link](",");
if ([Link] < 2) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
continue;
}
validLines++;
} catch (Exception e) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
}
}
[Link]();
} catch (FileNotFoundException e) {
invalidLines = 1;
firstErrorLine = 1;
}

[Link](new ErrorInfo("genres_movies.csv", validLines,


invalidLines, firstErrorLine));
}

static void parseMoviesVotes(File file) {


int validLines = 0;
int invalidLines = 0;
int firstErrorLine = -1;

try {
Scanner scanner = new Scanner(file);
int lineNumber = 0;

if ([Link]()) {
[Link]();
lineNumber++;
}

while ([Link]()) {
lineNumber++;
String line = [Link]();
try {
String[] parts = [Link](",");
if ([Link] < 2) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
continue;
}

validLines++;
} catch (Exception e) {
invalidLines++;
if (firstErrorLine == -1) {
firstErrorLine = lineNumber;
}
}
}
[Link]();
} catch (FileNotFoundException e) {
invalidLines = 1;
firstErrorLine = 1;
}
[Link](new ErrorInfo("movie_votes.csv", validLines,
invalidLines, firstErrorLine));
}

public static ArrayList getObjects(TipoEntidade tipo) {


switch (tipo) {
case FILME:
return new ArrayList<>(movies);
case ATOR:
return new ArrayList<>(actors);
case REALIZADOR:
return new ArrayList<>(directors);
case GENERO_CINEMATOGRAFICO:
return new ArrayList<>(genres);
case INPUT_INVALIDO:
return new ArrayList<>(errorInfoList);
default:
return new ArrayList<>();
}
}

public static void main(String[] args) {


[Link]("Bem-vindo ao doisIMDB");
long start = [Link]();
boolean parseOk = parseFiles(new File("test-files"));
if (!parseOk) {
[Link]("Erro na leitura dos ficheiros");
return;
}

long end = [Link]();


[Link]("Ficheiros lidos com sucesso em " + (end - start) + "
ms");

ArrayList inputsInvalidos = getObjects(TipoEntidade.INPUT_INVALIDO);


[Link]([Link](0));
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
[Link]([Link](4));
[Link]([Link](5));
}
}

You might also like