Error searching/searching for attribute in a vector

Asked

Viewed 55 times

1

In this program with you insert the object peacefully in my vector with the method (Adder), but after I cannot fetch the string "plate" that the user typed... program always shows "Vehicle not found in the system!".

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

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: Tcarro

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;
    }
}

2 answers

1


The setters of your Tcarro class are wrong. You do so plate=this.plate, it should be so this.plate = plate. The other setters should be corrected as well. The Tcarro class should look as follows:

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;
}

}

  • Man, thank you so much! ...

  • Display, mark thread as solved.

0

As quoted in the previous answer, the resolution was in its getters and setters, and to no longer have this problem, there is an easy way to solve. inserir a descrição da imagem aqui

Using Eclipse, when 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, pressing Alt+Insert will open a "Generate" tab and one of the options will be the Getters and Setters. To make your class better organized, the two IDE’s do it for you.

inserir a descrição da imagem aqui

  • Vlw for the tip my friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.