Error fetching / searching attribute on a vector

In this program I can Insert the object quietly in my vector with the method (adder), but after I can not fetch the string "plate" that the user typed... program always shows " vehicle not found in the system!".

Note: the problem is in the method (searcher).

Class: Lessor

import javax.swing.*;
public class Locadora{
    public static TCarro[] vetor=new TCarro[50];
    public static void main(String[]args){
        for(int i=0;i<vetor.length;i++){
            vetor[i]=new TCarro();
        }
        menu();
    }
    public static void menu(){
        switch(Integer.parseInt(JOptionPane.showInputDialog("#MENU\n1.INCLUIR\n2.PESQUISAR\n"))){
            case 1:
                adder();
            case 2:
                searcher();
            default:
                break;
        }
        menu();
    }
    public static void adder(){
        for(int i=0;i<vetor.length;i++){
            vetor[i].setPlaca(JOptionPane.showInputDialog("Insira a placa:"));
            vetor[i].setCnh(Integer.parseInt(JOptionPane.showInputDialog("Insira a CNH:")));
            vetor[i].setMarca(JOptionPane.showInputDialog("Insira a marca:"));
            vetor[i].setModelo(JOptionPane.showInputDialog("Insira o modelo:"));
            vetor[i].setLocal(JOptionPane.showInputDialog("Insira locatario:"));
            vetor[i].setSit("DISPONIVEL");
            switch(Integer.parseInt(JOptionPane.showInputDialog("#DESEJA ADICIONAR MAIS?\n1.SIM\n2.NAO\n"))){
                case 2:
                    menu();
                default:
                    break;
            }
        }
    }
    public static void searcher(){
        String placa=JOptionPane.showInputDialog("Insira a placa:");
        for(int i=0;i<vetor.length;i++){
            if(placa.equals(vetor[i].getPlaca())){
                JOptionPane.showMessageDialog(null,"#DETALHES DO CARRO"+
                                            "\nPLACA:"+vetor[i].getPlaca()+
                                            "\nCNH:"+vetor[i].getCnh()+
                                            "\nMARCA:"+vetor[i].getMarca()+
                                            "\nMODELO:"+vetor[i].getModelo()+
                                            "\nLOCATARIO:"+vetor[i].getLocal()+
                                            "\nSITUACAO:"+vetor[i].getSit()+"\n");
            }
        }
        JOptionPane.showMessageDialog(null,"Veiculo não encontrado no sistema!\n");
    }
}

Class: TCAR

public class TCarro{
    String placa;
    int CNH;
    String marca;
    String modelo;
    String locatario;
    String situacao;

    public void setPlaca(String placa){
        placa=this.placa;
    }
    public String getPlaca(){
        return placa;
    }
    public void setCnh(int CNH){
        CNH=this.CNH;
    }
    public int getCnh(){
        return CNH;
    }
    public void setMarca(String marca){
        marca=this.marca;
    }
    public String getMarca(){
        return marca;
    }
    public void setModelo(String modelo){
        modelo=this.modelo;
    }
    public String getModelo(){
        return modelo;
    }
    public void setLocal(String local){
        locatario=local;
    }
    public String getLocal(){
        return locatario;
    }
    public void setSit(String sit){
        situacao=sit;
    }
    public String getSit(){
        return situacao;
    }
}
Author: rnd_rss, 2019-05-25

2 answers

The setters of your TCAR class are wrong. You do so plate = this.plate, it should be like this.plate = Plate. The other setters should be fixed as well. The Clase TCAR should look like this:

public class TCarro {
String placa;
int CNH;
String marca;
String modelo;
String locatario;
String situacao;

public void setPlaca(String placa){
    this.placa = placa;
}
public String getPlaca(){
    return placa;
}
public void setCnh(int CNH){
    this.CNH = CNH;
}
public int getCnh(){
    return CNH;
}
public void setMarca(String marca){
    this.marca = marca;
}
public String getMarca(){
    return marca;
}
public void setModelo(String modelo){
    this.modelo = modelo;
}
public String getModelo(){
    return modelo;
}
public void setLocal(String local){
    locatario=local;
}
public String getLocal(){
    return locatario;
}
public void setSit(String sit){
    situacao=sit;
}
public String getSit(){
    return situacao;
}

}

 1
Author: Mychell Teixeira, 2019-05-26 14:26:47

As quoted in the previous answer, the resolution was in your getters and setters, and to no longer have this problem, it has an easy way to solve. insert the description of the image here

Using Eclipse, by right-clicking on your class, you will have the option " Generate Getters and Setters..."and Eclipse will create all methods automatically.

If you are using NetBeans, if you press Alt + Insert it will open a "Generate" tab and one of the options will be the Getters and Setters. To make your class better organized, both Ides do this for you.

insert the description of the image here

 0
Author: Braian Freitas, 2019-05-28 02:56:35