How to access an attribute of an object within an object stack

0

I have several objects of class Vehicle with plate, model and yearFabri as attributes, inside a stack that I created (Stack.java). I need to apply a method in the Stack class, which removes objects from the stack until I find a Vehicle that has the yearFabri less than 2000.

Stack:

public class Pilha implements TAD_Pilha {
    private int topo;
    private int MAX;
    private Object memo[];

    public int getTopo() {return topo;}
    public void setTopo(int topo) {this.topo = topo;}
    public int getMAX() {return MAX;}
    public void setMAX(int MAX) {this.MAX = MAX;}
    public Object[] getMemo() {return memo;}
    public void setMemo(Object[] memo) {this.memo = memo;}

    public Pilha(int qtde){
        topo = -1;
        MAX = qtde;
        memo = new Object[MAX];
    }        

    @Override
    public boolean isEmpty(){ return(topo==-1);}
    @Override
    public boolean isFull(){ return(topo==MAX-1);}

    @Override
    public Object push(Object x){
        if(!isFull() && x != null){
            memo[++topo] = x;
            return x;
        }
        else
            return null;
    }

    @Override
    public Object pop(){
        if(!isEmpty())
            return memo[topo--];
        else
            return null;
    }

    @Override
    public Object top(){
        if(!isEmpty()){
            return memo[topo];
        }
        else
            return null;
    }

    @Override
    public String toString(){
        if(!isEmpty()){
            String msg="";
            for(int i=0; i<=topo; i++){
                msg += memo[i].toString();  
                if(i != topo) msg += ", ";
            }
            return ("Pilha = [" + msg + " ]");
        }
        else
            return "Pilha vazia!";
    }
}

Vehicle:

public class Veiculo {
    private String placa;
    private String modelo;
    private int anoFabri;    

    public Veiculo() {
    }

    public Veiculo(String placa, String modelo, int anoFabri) {
        this.placa = placa;
        this.modelo = modelo;
        this.anoFabri = anoFabri;
    }

    public String getPlaca() {
        return placa;
    }

    public void setPlaca(String placa) {
        this.placa = placa;
    }

    public String getModelo() {
        return modelo;
    }

    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    public int getAnoFabri() {
        return anoFabri;
    }

    public void setAnoFabri(int anoFabri) {
        this.anoFabri = anoFabri;
    }

    @Override
    public String toString() {
        return placa + " " + modelo + " " + anoFabri; //To change body of generated methods, choose Tools | Templates.
    }
}
    
asked by anonymous 04.06.2017 / 20:06

1 answer

0

You could do something like this:

public void RemoveVeiculosAnoFabriMaior1999() {
    while (topo > 0) {
        Veiculo veiculo = (Veiculo) memo[topo];
        if (veiculo.getAnoFabri() < 2000) break;
        memo[topo--] = null;
    }
}
    
04.06.2017 / 23:06