Good afternoon fellow programmers on duty. I'm developing an application that creates from a formary through the information contained in a tabulato.txt file. I was able to develop all the routine of creation of the formulary, now I must order it, according to the attribute "geopolitical" (neighborhoods). Here is the code:
MAP CREATION CLASS:
package classes.repository;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import classes.model.Clientes;
import classes.model.Comodatos;
import classes.model.Zonas;
public class Tables {
public SortedMap<String, Clientes> clientes = new TreeMap<>();
public List<Zonas> zonas = new ArrayList<>();
public Map<String, Comodatos> comodatos = new LinkedHashMap<>();
public void tables() {
try {
File file = new File("table.txt");
FileReader fReader = new FileReader(file);
BufferedReader bReader = new BufferedReader(fReader);
String leitor = bReader.readLine();
while ((leitor = bReader.readLine()) != null) {
String[] leitorSplit = leitor.split("\t");
clientes.put(leitorSplit[0], new Clientes(leitorSplit)); // leitorSplit[0] == cód do cliente não pode se repetir, por isso utilizei o Map
comodatos.put(leitorSplit[5], (new Comodatos(leitorSplit)));
}
File file = new File("zonasbairros.txt");
FileReader fReader = new FileReader(file);
BufferedReader bReader = new BufferedReader(fReader);
String leitor;
while ((leitor = bReader.readLine()) != null) {
String[] leitorSplit = leitor.split("\t");
zonas.add(new Zonas(leitorSplit));
}
bReader.close();
} catch (IOException e) {
e.getMessage();
}
}
}
I implemented a comparable object class to see if it worked, but it did not work:
package classes.model;
public class Clientes implements Comparable<Clientes> {
private String cod, fantasia, razaoSocial, endereco, geopolitico, documento, codproduto, descricao, qtd;
private String[] linha;
public Clientes() {
}
public Clientes(String[] linha) {
this.cod = linha[0];
this.fantasia = linha[1];
this.razaoSocial = linha[2];
this.endereco = linha[3];
this.geopolitico = linha[4];
}
public String getCod() {
return cod;
}
public void setCod(String cod) {
this.cod = cod;
}
// Getters...Setters..
@Override
public int compareTo(Clientes o) {
return geopolitico.compareTo(o.geopolitico);
}
}
You can easily organize with Collections.sort if you use a List, however the data in the .txt file is repeated and therefore the Map choice. Several heads think better than one. Any light ?? hug!