-2

I'm writing a school project: a car rent system.

So far we can register a car with a model, color, etc... and an avaible character (Y/N). We are using a .txt file to save the car atributes, and another .txt file to save the clients atributes.

To rent a car we select a client, a car, then we get an unique atribute from each one (the license plate and id), save it to another .txt file, and in the car .txt file we have to change the avaible character from Y to N.

We are using an interface to do this, and we have a table with the list of clients and cars registred. First we select a client, click NEXT, then select a car and click RENT. The avaible character in the table changes from Y to N but I can't make it to save in the cars .txt file also.

I'd like some help with this

This is the code on the "register the car" window

public void persistir(){
    File file = new File("carros.txt");

    for(int i=0; i<qtd; i++){       
        try {

            Veiculo carro = carros[i];

            //verifica se o arquivo já existe e escreve no final do mesmo
            FileWriter fileWriter = new FileWriter("carros.txt", true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            bufferedWriter.write(carro.getMarca() + ";" + carro.getModelo() + ";" + carro.getCor() + ";" + carro.getCategoria() + ";" + carro.getAno() + ";" + carro.getPlaca() + ";" + carro.getDisponivel() + ";");
            bufferedWriter.newLine();
            bufferedWriter.close();

        } catch (HeadlessException | IOException e) {
            System.out.println("Erro: " + e);
        }
    }
}

and this is the code on the RENT button

private void finalizarActionPerformed(java.awt.event.ActionEvent evt) {                                          

    int linha =  tabVeiculos2.getSelectedRow();
    GPLACA = (tabVeiculos2.getValueAt(linha, 5)).toString();
    tabVeiculos2.setValueAt("N", linha, 6);
    Alugar(GRG,GPLACA);
    JOptionPane.showMessageDialog(null, "Locação Realizada com sucesso!");
    System.out.println(GPLACA);

    this.dispose();

}

let me know if you need help translating something, thanks.

Edit: since you all think I just want to get my work done, I'm not. I can't make any progress since this.

Volkswagen;Gol;Preto;Popular;2016;AAA0000;Y; Ford;Fiesta;Prata;Popular;2011;BBB1111;Y;

This is in the cars.txt file and I want to know how I change the Y to N.

  • 2
    Then tell us what exactly is not working out for you. Do not just put up your assignment and your code ... do you really expect us to read all of that, to then figure what exactly your code is not doing? – GhostCat Dec 22 '16 at 19:51
  • @GhostCat can you help me with this http://stackoverflow.com/questions/41031206/androidtextcolor-not-actually-working – Anurag Joshi Dec 22 '16 at 20:01
  • @AnuragJoshi Sorry, but the fact that i am commenting on everything doesn't mean that I know everything. Seriously, although I have set the bronze badge for the Android tag as my next target to go for ... I am not that fluent with Android java. Thus, sorry, I can't help with that ;-( – GhostCat Dec 22 '16 at 20:05
  • that's alright :P @GhostCat – Anurag Joshi Dec 22 '16 at 20:09
  • Volkswagen;Gol;Preto;Popular;2016;AAA0000;Y; Ford;Fiesta;Prata;Popular;2011;BBB1111;Y; This is in the cars.txt file and I want to know how I change the Y to N. – Eduardo Vieira Dec 22 '16 at 20:12
  • Make sure that code in a question is given in English please. – weston Dec 22 '16 at 20:34

1 Answers1

1

It would be easiest to rewrite the whole file. That is, read and parse the file, change the data and write the file out again.

You should also consider using a standard structured file format for text files, such as xml or json. Using one of these will allow you to use libraries to read and write the data with less coding effort.

weston
  • 54,145
  • 21
  • 145
  • 203
  • I think this is what my professor sugested. I'm going to research a bit on this, but if you have any links that I can look into... thanks – Eduardo Vieira Dec 22 '16 at 21:12
  • You could search the documentation topics on SO documentation: e.g. http://stackoverflow.com/documentation/search?tag=java&query=json – weston Dec 22 '16 at 23:30