This commit is contained in:
bigheadbh 2024-05-06 22:58:33 -03:00
parent 627cdc8569
commit 1353b02349
2 changed files with 1113 additions and 0 deletions

649
tps/fonte/Character.c Normal file
View File

@ -0,0 +1,649 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class.
* @author Pedro Lopes - github.com/httpspedroh
* @version 2.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters from file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) character_print(character);
else printf("x Character not found!\n");
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
return 0;
}

464
tps/fonte/Character.java Normal file
View File

@ -0,0 +1,464 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga.
* @author Pedro Lopes - github.com/httpspedroh
* @version 2.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Print character
if(character != null) character.print();
else System.out.println("x Character not found!");
// Read line
line = inScanner.nextLine();
}
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //