aeds2/tps/fonte/Personagem.java

178 lines
4.0 KiB
Java

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);
}
}