Boa Noite amigos…
Estou com uma duvida, sendo que a entrega do trabalho ta em cima do prazo … Se puderem me ajudar…
Tenho 19 arquivos referentes as corridas de formula1 do ano passado. Os arquivos estao em formato .csv, consigo ler eles numa boa, mas preciso imprimir a classificacao final, somando os pontos dos pilotos. Ai que esta minha dificuldade… Não consigo fazer essa soma… Os campos do arquivo sao os mesmos do meu construtor da classe Piloto …
.Segue abaixo o que tenho ate agora …
[code]import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main (String [] args){
List v1 = leFile ();
int pA[] = new int[190];
int pontosEx = 0;
for (int i = 0; i<v1.size(); i++){
Piloto p = (Piloto) v1.get(i);
int nNum = (Integer.parseInt(p.numero));
pA[i] = (Integer.parseInt(p.numero));
//pontosEx = p.getPontos();
//System.out.println(p.getPiloto());
//System.out.println(p.pontos + pontosEx);
//if(pA[i] == nNum)
//pontosEx = p.getPontos();
//p.pontos = p.pontos + pA[i];
// System.out.println(nNum);
System.out.println();
p.print(); //Chama metodo da classe Piloto "print" ;
System.out.println();
}
}
public static List leFile () {
File arquivos[]; //CRIA ARRAY, OS ARQUIVOS .CSV ESTA ARMAZENADOS TODOS EM UM DIRETORIO
File diretorio = new File("C:\\Henrique_Arquivos"); //DIRETORIO ONDE ESTAO ARMAZENADOS OS ARQUIVOS .CSV
arquivos = diretorio.listFiles();
Temporada2010 temp = new Temporada2010(30); // Cria uma instancia da classe Temporada2010
List v2 = new ArrayList<Piloto>();
try {
for ( int i = 0; i < arquivos.length ; i++){ //Le todos os arquivos .csv
FileReader fr = new FileReader (arquivos[i]);
BufferedReader in = new BufferedReader (fr);
in.readLine();
String line;
while ((line=in.readLine())!= null){ // Le arquivo .csv enquanto linha for diferente de null
Provas provas = new Provas(24);
Piloto p = AchaTokens (line); // Chama metodo acha tokens
boolean t = v2.contains(p.getPiloto());
if (p.getPontos() != 0 & t == false){
v2.add (p);// adiciona piloto no array list
Collections.sort (v2, new PorPontos());
}
}
in.close (); //fecha arquivo
}
}
catch (IOException e) {
System.out.println ("Erro na leitura do arquivo "+diretorio);
}
return v2;
}
private static Piloto AchaTokens (String line) {
Piloto p;
String tk[] = line.split(";");
p = new Piloto (tk[0], tk[1], tk[2], tk[3], tk[4],tk[5],Integer.parseInt(tk[6]));
return p;
}
}[/code]
[code]public class Piloto implements Comparable{
protected String numero, piloto, time, voltas, tempo, grid;
protected int pontos;
public Piloto(String numero, String piloto, String time, String voltas, String tempo, String grid, int pontos) {
this.numero = numero;
this.piloto = piloto;
this.time = time;
this.voltas = voltas;
this.tempo = tempo;
this.grid = grid;
this.pontos = pontos;
}
public Piloto(){
}
public void print(){
System.out.println("Numero do piloto: " +numero);
System.out.println("Nome do piloto: " +piloto);
System.out.println("Equipe: " +time);
System.out.println("Voltas: " +voltas);
System.out.println("Tempo: " +tempo);
System.out.println("Grid: " +grid);
System.out.println("Pontuação: " +pontos);
}
public int compareTo (Object obj){
if (this.pontos > pontos)
return 1;
else if(this.pontos < pontos)
return -1;
else
return 0;
}
/**
* @return the numero
*/
public String getNumero() {
return numero;
}
/**
* @param numero the numero to set
*/
public void setNumero(String numero) {
this.numero = numero;
}
/**
* @return the piloto
*/
public String getPiloto() {
return piloto;
}
/**
* @param piloto the piloto to set
*/
public void setPiloto(String piloto) {
this.piloto = piloto;
}
/**
* @return the time
*/
public String getTime() {
return time;
}
/**
* @param time the time to set
*/
public void setTime(String time) {
this.time = time;
}
/**
* @return the voltas
*/
public String getVoltas() {
return voltas;
}
/**
* @param voltas the voltas to set
*/
public void setVoltas(String voltas) {
this.voltas = voltas;
}
/**
* @return the tempo
*/
public String getTempo() {
return tempo;
}
/**
* @param tempo the tempo to set
*/
public void setTempo(String tempo) {
this.tempo = tempo;
}
/**
* @return the grid
*/
public String getGrid() {
return grid;
}
/**
* @param grid the grid to set
*/
public void setGrid(String grid) {
this.grid = grid;
}
/**
* @return the pontos
*/
public int getPontos() {
return pontos;
}
/**
* @param pontos the pontos to set
*/
public void setPontos(int pontos) {
this.pontos = pontos;
}
}[/code]
[code]
import java.util.Arrays;
public class Provas {
public Piloto piloto [] ;
public Provas(int size){
piloto = new Piloto[size];
}
public void inserePiloto(Piloto p){
for (int i=0; i<1;i++)
piloto[i] = p;
}
public void classifica(){
Arrays.sort(piloto);
}
}[/code]
[code]
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
public class Temporada2010 {
Piloto osQuePontuaram[];
private int last;
public Temporada2010(int size){
osQuePontuaram = new Piloto[size];
last = -1;
}
public void inserePilotosQuePontuaram(Piloto p){
for (int i=0; i<osQuePontuaram.length;i++){
if(p.getPontos() != 0){
last++;
osQuePontuaram[last] = p;
}
for (int j=0; j<osQuePontuaram.length;j++){
if(osQuePontuaram[i].equals(p))
p.pontos +=p.pontos;
}
}
}
public void ordenaPorNome(){
Arrays.sort(osQuePontuaram,new PorNome());
}
public void ordenaPorPontos(){
Arrays.sort(osQuePontuaram,new PorPontos());
}
public void gravar(File arquivo)throws IOException{
PrintWriter gravador = new PrintWriter(new FileWriter(arquivo));
for (int i=0;i<osQuePontuaram.length;i++)
gravador.print(osQuePontuaram[i]);
gravador.close();
}
}[/code]
[code]
import java.util.Comparator;
public class PorNome implements Comparator {
public int compare (Object obj1, Object obj2){
Piloto p1 = (Piloto) obj1;
Piloto p2 = (Piloto) obj2;
return p1.getPiloto().compareTo(p2.getPiloto());
}
}[/code]
[code]
import java.util.Comparator;
public class PorPontos implements Comparator {
public int compare(Object obj1, Object obj2){
Piloto p1 = (Piloto) obj1;
Piloto p2 = (Piloto) obj2;
if (p1.getPontos() == p2.getPontos())
return 0;
else if (p1.getPontos() > p2.getPontos())
return -1;
else
return 1;
}
}[/code]