Overflow error

Asked

Viewed 250 times

0

Good afternoon, I’m having a problem with overflow in my Location class, I have no idea why it’s happening. I am beginner in the area, I still have not much notion of these mistakes, someone could help?

Follow the classes below.

public class Cliente
{
    private int cpf, tel;
    private String nome,email;
    private Locacao[]locacao;
    public Cliente(int cpf, String nome, int tel, String email)
    {
        this.cpf=cpf;
        this.nome=nome;
        this.tel=tel;
        this.email=email;
        this.locacao=new Locacao[5];
    }

    public void setcpf(int cpf){
        this.cpf=cpf;
    }

    public int getcpf(){
        return this.cpf;
    }

    public void setnome(String nome){
        this.nome=nome;
    }

    public String getnome(){
        return this.nome;
    }

    public void settel(int tel){
        this.tel=tel;
    }

    public int gettel(){
        return this.tel;
    }
    public void setemail(String email){
        this.email=email;
    }
    public String getemail(){
        return this.email;
    }
    public float location(Locacao nova){
        float valor=0;
        int periodo=0;
        for (int i=0;i<locacao.length;i++){
            if(locacao[i]==null){
            locacao[i]=nova;
            periodo=(nova.getdiaTermino()-nova.getdiaInicio())+((nova.getmesTermino()*30)-(nova.getmesInicio()*30));
                if (nova.getcategoria().equals("Básico")){
                    valor=periodo*Locacao.basico;
                    break;
                }
                if(nova.getcategoria().equals("Luxo")){
                    valor=periodo*Locacao.luxo;
                    break;
                }
                if(nova.getcategoria().equals("Super luxo")){
                    valor=periodo*Locacao.spLuxo;
                    break;
                }    
            }
        }
        return valor;
    }
    public void checkSubst(Carros novo){
        for (int i=0; i<locacao.length;i++)
            if(locacao[i]!=null)
                if((locacao[i].getcategoria().equals(novo.getcategoria()))&&(novo.getano()<=locacao[i].getano())){
                    locacao[i].setcarro(novo);
                    break;
                }
    }
    public float checkCost(){
        float valor=0;
        int periodo=0;
        for(int i=0; i<locacao.length;i++)
            if(locacao[i]!=null){
              periodo=(locacao[i].getdiaTermino()-locacao[i].getdiaInicio())+((locacao[i].getmesTermino()*30)-(locacao[i].getmesInicio()*30));
                if(locacao[i].getcategoria().equals("Básico"))
                    valor=periodo*Locacao.basico;

                if(locacao[i].getcategoria().equals("Luxo"))
                    valor=periodo*Locacao.luxo;

                if(locacao[i].getcategoria().equals("Super luxo"))
                    valor=periodo*Locacao.spLuxo;
              break;
            }
        return valor;
    }
    public float checkTotalCost(){
        float valor=0;
        int periodo=0;
        for(int i=0; i<locacao.length;i++)
            if(locacao[i]!=null){
                if(locacao[i].getcategoria().equals("Básico")){
                    periodo=((locacao[i].getdiaTermino()-locacao[i].getdiaInicio())+((locacao[i].getmesTermino()*30)-(locacao[i].getmesInicio()*30)));
                    valor+=periodo*Locacao.basico;
                }
                if(locacao[i].getcategoria().equals("Luxo")){
                    periodo=((locacao[i].getdiaTermino()-locacao[i].getdiaInicio())+((locacao[i].getmesTermino()*30)-(locacao[i].getmesInicio()*30)));
                    valor+=periodo*Locacao.luxo;
                }
                if(locacao[i].getcategoria().equals("Super luxo")){
                    periodo=((locacao[i].getdiaTermino()-locacao[i].getdiaInicio())+((locacao[i].getmesTermino()*30)-(locacao[i].getmesInicio()*30)));
                    valor+=periodo*Locacao.spLuxo;
                }
            }
        return valor;
    }
    public int checkCar(String categoria){
        int qtd=0;
        for(int i=0;i<locacao.length;i++)
            if(locacao[i]!=null)
                if(locacao[i].getcategoria().equals(categoria))
                qtd++;
        return qtd;
    }
    public void imprimir(){
     System.out.println("Nome:"+nome);
     System.out.println("Cpf:"+cpf);
     System.out.println("Telefone:"+tel);
     System.out.println("Email:"+email);
     for(int i=0;i<locacao.length;i++)
        if(locacao[i]!=null){
            System.out.println("Dia de início:"+locacao[i].getdiaInicio()+"Mês de inicio:"+locacao[i].getmesInicio());
            System.out.println("Dia de término:"+locacao[i].getdiaTermino()+"Mês de término:"+locacao[i].getmesTermino());
            System.out.println("Modelo do carro:"+locacao[i].getmodelo());
            System.out.println("Placa do carro:"+locacao[i].getPlaca());
            System.out.println("Valor da locação:"+checkCost());
        }
    }
}


public class Locacao
{   
    private int diaInicio, mesInicio, diaTermino, mesTermino;
    private Carros carro;
    static int basico=120, luxo=160, spLuxo=210;


    public Locacao(int diaInicio, int mesInicio, int diaTermino, int mesTermino, Carros carro)
    {
        this.diaInicio=diaInicio;
        this.mesInicio=mesInicio;
        this.diaTermino=diaTermino;
        this.mesTermino=mesTermino;
        this.carro=carro;
    }
    public void setdiaInicio(int diaInicio){
        this.diaInicio=diaInicio;
    }
    public int getdiaInicio(){
        return this.diaInicio;
    }
    public void setmesInicio(int mesInicio){
        this.mesInicio=mesInicio;
    }
    public int getmesInicio(){
        return this.mesInicio;
    }
    public void setdiaTermino(int diaTermino){
        this.diaTermino=diaTermino;
    }
    public int getdiaTermino(){
        return this.diaTermino;
    }
    public void setmesTermino(int mesTermino){
        this.mesTermino=mesTermino;
    }
    public int getmesTermino(){
        return this.mesTermino;
    }
    public Carros getcategoria(){
        return this.getcategoria();
    }
    public int getano(){
        return this.getano();
    }
    public Carros getcarro(){
        return this.carro;
    }
    public void setcarro(Carros novo){
        this.carro=novo;
    }
    public String getmodelo(){
        return this.getmodelo();
    }
    public String getPlaca(){
        return this.getPlaca();
    }
}


public class Carros
{
    private String placa, modelo, cor, categoria;
    private int ano;
    public Carros(String placa, String modelo, String cor, int ano, String categoria)
    {
        this.placa=placa;
        this.modelo=modelo;
        this.cor=cor;
        this.ano=ano;
        this.categoria=categoria;
    }
    public Carros(String placa, String modelo, String cor, int ano)
    {
        this(placa, modelo, cor, ano, "Básico");
    }
    public String getPlaca(){
        return this.placa;
    }
    public void setPlaca(String placa){
        this.placa=placa;
    }
    public String getmodelo(){
        return this.modelo=modelo;
    }
    public void setmodelo(String modelo){
        this.modelo=modelo;
    }
    public String getcor(){
        return this.cor;
    }
    public void setcor(String cor){
       this.cor=cor;
    }
    public int getano(){
        return this.ano;
    }
    public void setano(int ano){
        this.ano=ano;
    }
    public String getcategoria(){
        return this.categoria;
    }
    public void setcategoria(String categoria){
        this.categoria=categoria;
    }
}
  • Post the error stacktrace, it indicates where the error is happening.

  • Is that what appears in the window when it fails? is this one: java.lang.Stackoverflowerror At locacao.getcategoria(Locacao.java:42) and repeats two million times. I use bluej.

  • That same, it indicates where the mistake started happening

  • I had asked in the getcategoria method of rental the return in type car, but it was String, however, the error is not only that :(

1 answer

1


The error is in the Location class in the getCategory function:

public Carros getcategoria(){
  return this.getcategoria();
}

I think what you wanted to do is this:

public String getcategoria(){
  return this.carro.getcategoria();
}
  • This method was to return a String, just now I said there, was lack of attention anyway. Thanks for finding the mistake! I never program with kkkk sleep again.

  • 2

    it is always good to write your get setters and methods with uppercase and lowercase letters e.g.: getCategory() so it will be much more readable

Browser other questions tagged

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