Read data from java txt file and perform operations on Java

0

I'm doing a rental car in java I need to read from a txt the type of client (char), a range of dates (string) and the amount of passengers (int).

I need to bring this data to be analyzed, for example according to the passenger qtd I make calculations of which car is most appropriate and according to the type of customer I calculate the rates, according to the day of the week has different values.

But I do not know and I did not find any plausible explanation of how do I link the variables in the file to the correct variables in the code can anyone help me?

Method to read the file:

private static void ler() {
            File dir = new File("C:\Arquivos");
            File arq = new File(dir, "LocadoraCarro.txt");

            try {
                FileReader fileReader = new FileReader(arq);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String linha = "";
                while ( ( linha = bufferedReader.readLine() ) != null) {
                System.out.println(linha);
            }

                fileReader.close();
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }               

File Format :     CUSTOMER_TYPE: QUANTITY_PASSENGERS: DATA1, DATA2, DATA3

Example : Normal: 2: 12Abr2018 (Mon), 13Aug2018 (Ter)

    
asked by anonymous 18.02.2018 / 13:28

2 answers

0

This is very quiet to do, what should be disturbing is how you are looking at the problem.

First I used a class to represent the data, I called it Data . This class has nothing much. Only some attributes, setters, getters, and an implementation of the toString() method.

And methods getClientType(String[] split) , getNPassengers(String[] split) e parseDates(String[] split) retrieve each attribute of class Data given a line of the file.

In the readFile(String filePath) method, each line read from the file a new instance of the Data class is created and added to a list. Each element in this list represents one line of the input file.

Complete class:

public class Main {

    private static class Data{
        private String clientType;
        private int nPassagers;
        private String[] dates;

        public Data(String clientType, int nPassagers, String[] dates) {
            this.clientType = clientType;
            this.nPassagers = nPassagers;
            this.dates = dates;
        }

        public String getClientType() {
            return clientType;
        }

        public void setClientType(String clientType) {
            this.clientType = clientType;
        }

        public int getnPassagers() {
            return nPassagers;
        }

        public void setnPassagers(int nPassagers) {
            this.nPassagers = nPassagers;
        }

        public String[] getDates() {
            return dates;
        }

        public void setDates(String[] dates) {
            this.dates = dates;
        }

        @Override
        public String toString() {
            return "Data{" +
                    "clientType='" + clientType + '\'' +
                    ", nPassagers=" + nPassagers +
                    ", dates=" + Arrays.toString(dates) +
                    '}';
        }
    }

    private static List<Data> readFile(String filePath){
        File arq = new File(filePath);

        List<Data> dataList = new ArrayList<>();

        try {
            FileReader fileReader = new FileReader(arq);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String linha = "";
            while ((linha = bufferedReader.readLine()) != null) {
                String[] split = linha.split(":");
                dataList.add(new Data(getClientType(split), getNPassengers(split), parseDates(split)));
            }

            fileReader.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return dataList;
    }

    private static String getClientType(String[] split){
        if (split == null || split.length == 0){
            return null;
        }

        return split[0];
    }

    private static int getNPassengers(String[] split){
        if (split == null || split.length < 2 || split[1] == null){
            return -1;
        }

        return Integer.valueOf(split[1].trim());
    }

    private static String[] parseDates(String[] split){
        if (split == null || split.length < 3 || split[2] == null){
            return null;
        }

        return split[2].trim().split(",");
    }

    public static void main(String[] args){
        List<Data> result = readFile("LocadoraCarro.txt");

        for (Data data : result){
            System.out.println(data.toString());
        }
    }
}
    
19.02.2018 / 00:20
0

First, I'm not sure what your goal is when using a TXT file, but ideally in this situation is to use a database, which already has all the mechanism to handle the data.

But since your case is a TXT, that's fine! Just like in an application that uses a database, we will use object orientation to represent the data in our program.

For this, I suggest that you do a little research on object orientation, for now I'll explain the basics so you can continue afterwards ...

To represent the data of your txt, we will create a class containing the respective attributes that you want to recover from txt

package br.com.cogerh.template.model;

import java.util.List;

public class File {

//ESSE ATRIBUTO IRÁ REPRESENTAR UM CLIENTE
private String cliente;

//LISTA COM TODAS AS DATAS
private List<String> data;

//QUANTIDADE DE PASSAGEIROS
private Integer qtdPassageiros;

public Arquivo(){

}


public Arquivo(String cliente, List<String> data, Integer qtdPassageiros) {
    super();
    this.cliente = cliente;
    this.data = data;
    this.qtdPassageiros = qtdPassageiros;
}

public void implementacaoMetodo1(){
    System.out.println("EXEMPLO DE IMPLEMENTACAO DO METODO 1");
}

public void implementacaoMetodo2(){
    System.out.println("EXEMPLO DE IMPLEMENTACAO DO METODO 2");

}


public String getCliente() {
    return cliente;
}

public void setCliente(String cliente) {
    this.cliente = cliente;
}

public List<String> getData() {
    return data;
}

public void setData(List<String> data) {
    this.data = data;
}

public Integer getQtdPassageiros() {
    return qtdPassageiros;
}

public void setQtdPassageiros(Integer qtdPassageiros) {
    this.qtdPassageiros = qtdPassageiros;
}

}

Once that's done, our class represents our customers! In this class we also have examples of methods that will help you implement your tax calculation rules and so on ...

The next step is to call the class that read the txt from the client class we just created and pass the values through the class constructor or the attribute set methods.

    
20.11.2018 / 04:33