From 17827f8b8a37604ffbb213803b92704b3aeabbde Mon Sep 17 00:00:00 2001 From: Pedro Lopes Date: Tue, 24 Oct 2023 08:41:14 -0300 Subject: [PATCH] remove: old tp2 class --- tps/gabaritos/Personagem.java | 177 ----------------- tps/gabaritos/Player.c | 357 ---------------------------------- tps/gabaritos/Player.java | 209 -------------------- 3 files changed, 743 deletions(-) delete mode 100644 tps/gabaritos/Personagem.java delete mode 100644 tps/gabaritos/Player.c delete mode 100644 tps/gabaritos/Player.java diff --git a/tps/gabaritos/Personagem.java b/tps/gabaritos/Personagem.java deleted file mode 100644 index 60009de..0000000 --- a/tps/gabaritos/Personagem.java +++ /dev/null @@ -1,177 +0,0 @@ -import java.io.BufferedReader; -import java.io.FileReader; -import java.text.DecimalFormat; - -public class Personagem { - - private String nome; - private int altura; - private double peso; - private String corDoCabelo; - private String corDaPele; - private String corDosOlhos; - private String anoNascimento; - private String genero; - private String homeworld; - - public String getNome() { - return nome; - } - - public void setNome(String nome) { - this.nome = nome; - } - - public int getAltura() { - return altura; - } - - public void setAltura(int altura) { - this.altura = altura; - } - - public double getPeso() { - return peso; - } - - public void setPeso(double peso) { - this.peso = peso; - } - - public String getCorDoCabelo() { - return corDoCabelo; - } - - public void setCorDoCabelo(String corDoCabelo) { - this.corDoCabelo = corDoCabelo; - } - - public String getCorDaPele() { - return corDaPele; - } - - public void setCorDaPele(String corDaPele) { - this.corDaPele = corDaPele; - } - - public String getCorDosOlhos() { - return corDosOlhos; - } - - public void setCorDosOlhos(String corDosOlhos) { - this.corDosOlhos = corDosOlhos; - } - - public String getAnoNascimento() { - return anoNascimento; - } - - public void setAnoNascimento(String anoNascimento) { - this.anoNascimento = anoNascimento; - } - - public String getGenero() { - return genero; - } - - public void setGenero(String genero) { - this.genero = genero; - } - - public String getHomeworld() { - return homeworld; - } - - public void setHomeworld(String homeworld) { - this.homeworld = homeworld; - } - - protected Personagem clone() throws CloneNotSupportedException { - Personagem novo = new Personagem(); - novo.nome = this.nome; - novo.altura = this.altura; - novo.corDoCabelo = this.corDoCabelo; - novo.corDaPele = this.corDaPele; - novo.corDosOlhos = this.corDosOlhos; - novo.anoNascimento = this.anoNascimento; - novo.genero = this.genero; - novo.homeworld = this.homeworld; - return novo; - } - - public void ler(String nomeArquivo) throws Exception { - FileReader file = new FileReader(nomeArquivo); - BufferedReader buffer = new BufferedReader(file); - String json = ""; - String line = buffer.readLine(); - while (line != null) { - json += line; - line = buffer.readLine(); - } - - buffer.close(); - file.close(); - - String temp; - temp = json.substring(json.indexOf("name") + 8); - temp = temp.substring(0, temp.indexOf("',")); - this.nome = temp; - - temp = json.substring(json.indexOf("height") + 10); - temp = temp.substring(0, temp.indexOf("',")); - if (temp.equals("unknown")) - this.altura = 0; - else - this.altura = Integer.parseInt(temp); - - temp = json.substring(json.indexOf("mass") + 8); - temp = temp.substring(0, temp.indexOf("',")); - if (temp.equals("unknown")) - this.peso = 0; - else - this.peso = Double.parseDouble(temp.replace(",", "")); - - temp = json.substring(json.indexOf("hair_color") + 14); - temp = temp.substring(0, temp.indexOf("',")); - this.corDoCabelo = temp; - - temp = json.substring(json.indexOf("skin_color") + 14); - temp = temp.substring(0, temp.indexOf("',")); - this.corDaPele = temp; - - temp = json.substring(json.indexOf("eye_color") + 13); - temp = temp.substring(0, temp.indexOf("',")); - this.corDosOlhos = temp; - - temp = json.substring(json.indexOf("birth_year") + 14); - temp = temp.substring(0, temp.indexOf("',")); - this.anoNascimento = temp; - - temp = json.substring(json.indexOf("gender") + 10); - temp = temp.substring(0, temp.indexOf("',")); - this.genero = temp; - - temp = json.substring(json.indexOf("homeworld") + 13); - temp = temp.substring(0, temp.indexOf("',")); - this.homeworld = temp; - } - - public void imprimir() { - System.out.println(toString()); - } - - public String toString() { - DecimalFormat df = new DecimalFormat("#0.##"); - String resp = " ## " + nome + " ## " + altura + " ## "; - resp += df.format(peso) + " ## " + corDoCabelo + " ## "; - resp += corDaPele + " ## " + corDosOlhos + " ## "; - resp += anoNascimento + " ## " + genero + " ## "; - resp += homeworld + " ## "; - return resp; - } - - public void imprimirNome() { - System.out.println(nome); - } - -} diff --git a/tps/gabaritos/Player.c b/tps/gabaritos/Player.c deleted file mode 100644 index 530464f..0000000 --- a/tps/gabaritos/Player.c +++ /dev/null @@ -1,357 +0,0 @@ -/** - * @path TP02Q02 - Classe em C/Player.c - * @description C file that implements the Player class. - * @author Pedro Lopes - github.com/httpspedroh - * @version 1.0 - * @update 2023-09-27 - */ - -// ---------------------------------------------------------------------------------------------------- // - -// Includes -#include -#include -#include -#include - -// ---------------------------------------------------------------------------------------------------- // - -// Constants -#define MAX_PLAYERS 4000 -#define FILE_PATH "/tmp/players.csv" - -#define MAX_NAME_SIZE 40 -#define MAX_COLLEGE_SIZE 60 -#define MAX_BIRTH_CITY_SIZE 40 -#define MAX_BIRTH_STATE_SIZE 40 - -#define MAX_LINE_SIZE 300 -#define MAX_ATTRIBUTE_SIZE 100 - -// ---------------------------------------------------------------------------------------------------- // - -// Structs -typedef struct Player { - int id; - char *name; - int height; - int weight; - int birthYear; - char *birthCity; - char *birthState; - char *college; -} Player; - -// ---------------------------------------------------------------------------------------------------- // - -// Global variables -Player players[MAX_PLAYERS]; -int playersLength = 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) { - - // Skip first comma - if(!isFirstAttribute) { - - if(*substringEnd != NULL) *substringStart = *substringEnd + 1; - else *substringStart = *substringEnd; - } - - // Get next comma - *substringEnd = strchr(*substringStart, ','); - - // Get substring - if(*substringEnd) substring(attribute, *substringStart, *substringEnd - *substringStart); - 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, "nao informado"); - - // Clean \n from the end of the string - if(attribute[strlen(attribute) - 1] == '\n') attribute[strlen(attribute) - 1] = '\0'; -} - -// ---------------------------------------------------------------------------------------------------- // - -// Methods signatures - -// Class -Player player_newBlank(); -Player player_new(int id, char *name, int height, int weight, int birthYear, char *birthCity, char *birthState, char *college); -Player *player_clone(Player *player); -void player_print(Player *player); -Player player_read(char *line); -Player *player_searchById(int id); - -// Gets -int player_getId(Player *player); -char *player_getName(Player *player); -int player_getHeight(Player *player); -int player_getWeight(Player *player); -char *player_getCollege(Player *player); -int player_getBirthYear(Player *player); -char *player_getBirthCity(Player *player); -char *player_getBirthState(Player *player); - -// Sets -void player_setId(Player *player, int id); -void player_setName(Player *player, char *name); -void player_setHeight(Player *player, int height); -void player_setWeight(Player *player, int weight); -void player_setCollege(Player *player, char *college); -void player_setBirthYear(Player *player, int birthYear); -void player_setBirthCity(Player *player, char *birthCity); -void player_setBirthState(Player *player, char *birthState); - -// General -void startPlayers(); - -// ---------------------------------------------------------------------------------------------------- // - -// Methods implementations - -// Class -Player player_newBlank() { - - Player player; - - player.id = -1; - player.height = -1; - player.weight = -1; - player.birthYear = -1; - - player.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); - strcpy(player.name, ""); - - player.birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char)); - strcpy(player.birthCity, ""); - - player.birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char)); - strcpy(player.birthState, ""); - - player.college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char)); - strcpy(player.college, ""); - - return player; -} - -Player player_new(int id, char *name, int height, int weight, int birthYear, char *birthCity, char *birthState, char *college) { - - Player player; - - player.id = id; - player.height = height; - player.weight = weight; - player.birthYear = birthYear; - - player.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); - strcpy(player.name, name); - - player.birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char)); - strcpy(player.birthCity, birthCity); - - player.birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char)); - strcpy(player.birthState, birthState); - - player.college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char)); - strcpy(player.college, college); - - return player; -} - -Player *player_clone(Player *player) { - - Player *clone = (Player *) malloc(sizeof(Player)); - - clone -> id = player -> id; - clone -> height = player -> height; - clone -> weight = player -> weight; - - clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); - strcpy(clone -> name, player -> name); - - clone -> birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char)); - strcpy(clone -> birthCity, player -> birthCity); - - clone -> birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char)); - strcpy(clone -> birthState, player -> birthState); - - clone -> college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char)); - strcpy(clone -> college, player -> college); - - return clone; -} - -void player_print(Player *player) { - - printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n", - player -> id, - player -> name, - player -> height, - player -> weight, - player -> birthYear, - player -> college, - player -> birthCity, - player -> birthState - ); -} - -Player player_read(char *line) { - - Player player = player_newBlank(); - - char *substringStart = line; - char *substringEnd = NULL; - char attribute[MAX_ATTRIBUTE_SIZE]; - - // Get id - proccess_attribute(attribute, &substringStart, &substringEnd, true); - player_setId(&player, atoi(attribute)); - - // Get name - proccess_attribute(attribute, &substringStart, &substringEnd, false); - player_setName(&player, attribute); - - // Get height - proccess_attribute(attribute, &substringStart, &substringEnd, false); - player_setHeight(&player, atoi(attribute)); - - // Get weight - proccess_attribute(attribute, &substringStart, &substringEnd, false); - player_setWeight(&player, atoi(attribute)); - - // Get college - proccess_attribute(attribute, &substringStart, &substringEnd, false); - player_setCollege(&player, attribute); - - // Get birth year - proccess_attribute(attribute, &substringStart, &substringEnd, false); - player_setBirthYear(&player, atoi(attribute)); - - // Get birth city - proccess_attribute(attribute, &substringStart, &substringEnd, false); - player_setBirthCity(&player, attribute); - - // Get birth state - proccess_attribute(attribute, &substringStart, &substringEnd, false); - player_setBirthState(&player, attribute); - - return player; -} - -Player *player_searchById(int id) { - - for(int i = 0; i < playersLength; i++) { - - if(player_getId(&players[i]) == id) return &players[i]; - } - return NULL; -} - -// Gets -int player_getId(Player *player) { return player -> id; } -char *player_getName(Player *player) { return player -> name; } -int player_getHeight(Player *player) { return player -> height; } -int player_getWeight(Player *player) { return player -> weight; } -char *player_getCollege(Player *player) { return player -> college; } -int player_getBirthYear(Player *player) { return player -> birthYear; } -char *player_getBirthCity(Player *player) { return player -> birthCity; } -char *player_getBirthState(Player *player) { return player -> birthState; } - -// Sets -void player_setId(Player *player, int id) { player -> id = id; } -void player_setName(Player *player, char *name) { strcpy(player -> name, name); } -void player_setHeight(Player *player, int height) { player -> height = height; } -void player_setWeight(Player *player, int weight) { player -> weight = weight; } -void player_setBirthYear(Player *player, int birthYear) { player -> birthYear = birthYear; } -void player_setBirthCity(Player *player, char *birthCity) { strcpy(player -> birthCity, birthCity); } -void player_setBirthState(Player *player, char *birthState) { strcpy(player -> birthState, birthState); } -void player_setCollege(Player *player, char *college) { strcpy(player -> college, college); } - -// General -void startPlayers() { - - // 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 player from line - Player player = player_read(line); - - players[playersLength++] = player; - - if(playersLength >= MAX_PLAYERS) { - - perror("x Max players reached"); - exit(EXIT_FAILURE); - } - } - - // Close file and free memory - fclose(fp); - - if(line) free(line); -} - -// ---------------------------------------------------------------------------------------------------- // - -// Main -int main() { - - // ----------------------------------------------------------------- // - - // #1 - Start - Read all players in CSV file - startPlayers(); - - // ----------------------------------------------------------------- // - - // #2 - Read input and print players from pub.in id entries - char in[5]; - scanf(" %[^\n]s", in); - - while(true) { - - if(isEnd(in)) break; - else { - - int id = atoi(in); - - Player *player = player_searchById(id); - - if(player) player_print(player); - else printf("x Player not found!\n"); - - // ------------------------- // - - scanf(" %[^\n]s", in); - } - } - return 0; -} \ No newline at end of file diff --git a/tps/gabaritos/Player.java b/tps/gabaritos/Player.java deleted file mode 100644 index 4b638ae..0000000 --- a/tps/gabaritos/Player.java +++ /dev/null @@ -1,209 +0,0 @@ -/** - * @path TP02Q01 - Classe em Java/Player.java - * @description Java class of a player from the NBA database - * @author Pedro Lopes - github.com/httpspedroh - * @version 1.0 - * @update 2023-09-27 - */ - -// ---------------------------------------------------------------------------------------------------- // - -// Imports -import java.util.Scanner; -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; - -// ---------------------------------------------------------------------------------------------------- // - -public class Player { - - // Global variables - public static final String FILE_PATH = "/tmp/players.csv"; - public static ArrayList allPlayers = new ArrayList(); - - // -------------------------- // - - // Attributes - private int id; - private String name; - private int height; - private int weight; - private String college; - private int yearOfBirth; - private String birthCity; - private String birthState; - - // Empty constructor - public Player() { - - this.id = 0; - this.name = ""; - this.height = 0; - this.weight = 0; - this.college = ""; - this.yearOfBirth = 0; - this.birthCity = ""; - this.birthState = ""; - } - - // Constructor - public Player(int id, String name, int height, int weight, String college, int yearOfBirth, String birthCity, String birthState) { - - this.id = id; - this.name = name; - this.height = height; - this.weight = weight; - this.college = college; - this.yearOfBirth = yearOfBirth; - this.birthCity = birthCity; - this.birthState = birthState; - } - - // Gets - public int getId() { return this.id; } - public String getName() { return this.name; } - public int getHeight() { return this.height; } - public int getWeight() { return this.weight; } - public String getCollege() { return this.college; } - public int getYearOfBirth() { return this.yearOfBirth; } - public String getBirthCity() { return this.birthCity; } - public String getBirthState() { return this.birthState; } - - // Sets - public void setId(int id) { this.id = id; } - public void setName(String name) { this.name = name; } - public void setHeight(int height) { this.height = height; } - public void setWeight(int weight) { this.weight = weight; } - public void setCollege(String college) { this.college = college; } - public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } - public void setBirthCity(String birthCity) { this.birthCity = birthCity; } - public void setBirthState(String birthState) { this.birthState = birthState; } - - // Clone - public Player clone() { return new Player(this.id, this.name, this.height, this.weight, this.college, this.yearOfBirth, this.birthCity, this.birthState); } - - // Print - public void print() { - - System.out.printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n", - this.id, this.name, this.height, this.weight, this.yearOfBirth, this.college, this.birthCity, this.birthState); - } - - // Read - public void read(String line) { - - // Split line by "," - String[] splitted = line.split(",", -1); - - // Fill empty attributes - for(int i = 0; i < splitted.length; i++) { - - if(splitted[i].equals("")) splitted[i] = "nao informado"; - } - - // Set attributes - this.id = Integer.parseInt(splitted[0]); - this.name = splitted[1]; - this.height = Integer.parseInt(splitted[2]); - this.weight = Integer.parseInt(splitted[3]); - this.college = splitted[4]; - this.yearOfBirth = Integer.parseInt(splitted[5]); - this.birthCity = splitted[6]; - this.birthState = splitted[7]; - } - - // ---------------------------------------------------------------------------------------------------- // - - // Read all players function - public static void startPlayers() { - - // 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 player - Player player = new Player(); - - // Read line - player.read(line); - - // Add player to array - allPlayers.add(player); - } - - // Close CSV file - fstream.close(); - } - catch(IOException e) { e.printStackTrace(); } - } - - // ---------------------------------------------------------------------------------------------------- // - - // Search by id function - public static Player searchById(int id, ArrayList players) { - - // Search for player - for(int i = 0; i < players.size(); i++) { - - if(players.get(i).getId() == id) return players.get(i); - } - return null; - } - - // ---------------------------------------------------------------------------------------------------- // - - public static void main(String[] args) { - - // ----------------------------------------------------------------- // - - // #1 - Start - Read all players in CSV file - startPlayers(); - - // ----------------------------------------------------------------- // - - // #2 - Read input and print players from pub.in id entries - - // Initialize scanner - Scanner inScanner = new Scanner(System.in); - - // Initialize player - Player player = new Player(); - - // Read first line - String line = inScanner.nextLine(); - - // While line is not "FIM" - while(!line.equals("FIM")) { - - // Get id - int id = Integer.parseInt(line); - - // Search for player - player = searchById(id, allPlayers); - - // Print player - if(player != null) player.print(); - else System.out.println("x Player not found!"); - - // Read line - line = inScanner.nextLine(); - } - - // Close scanner - inScanner.close(); - } -} - -// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file