Association of various classes in Java

Asked

Viewed 225 times

1

The project consists of a management system for a carrier, where an administrator manages travel, and these trips have drivers and trucks.

I used association to relate the classes, but I’m having difficulties to finalize a method of checking the availability of trucks and drivers.

How do I receive this information in a class method Viagem? How can I "tie" the data from Motorista and Caminhao on a journey?

public class Administrador extends Funcionario{

    private static int codAdministrador=0;
    private String login;
    private String senha;
    int contViagem=0;
    private Viagem []viagem = new Viagem[contViagem];

    public Administrador(String nome, String cpf, String email, double salario, String endereco, String rg,
            int codAdministrador, String login, String senha, int contViagem) {
        super(nome, cpf, email, salario, endereco, rg);
        codAdministrador++;
        this.login = login;
        this.senha = senha;
        this.contViagem=contViagem;
    }   

    public int getCodAdministrador() {
        return codAdministrador;
    }

    public String getLogin() {
        return login;
    }

    public String getSenha() {
        return senha;
    }


    public Viagem planejarViagem(Viagem viagem, Caminhao veiculo, Motorista piloto){
        if(viagem.verificaStatus(veiculo, piloto) == true){
            return viagem;
        }
        return null;
    }

    /*public boolean verificaDisponibilidadeCaminhao(Caminhao veiculo){

        if(veiculo.getStatus() != true){
            return true;
        }
        else{
            return false;
        }

    }

    public boolean verificaDisponibilidadeMotorista(Motorista piloto){
        if(piloto instanceof Motorista){
            Motorista aux = (Motorista) piloto;
            if(aux.getStatus() != true)
                return true;
            else
                return false;
        }
        return false;       
    }*/

    @Override
    public String toString(){
        StringBuilder info = new StringBuilder();

        info.append("\nRelatório de Viagem")
            .append("\nNome do Gerente que emitiu o relatório: "+getNome())
            .append("\nDados da viagem"+viagem.toString());

        return info.toString();
    }

}

public class Motorista extends Funcionario{

    private int codMotorista;
    private boolean status;


    public Motorista(String nome, String cpf, String email, double salario, String endereco, String rg,
            int codMotorista, boolean status) {
        super(nome, cpf, email, salario, endereco, rg);
        this.codMotorista = codMotorista;
        this.status = status;
    }

    public int getCodMotorista() {
        return codMotorista;
    }

    public boolean getStatus() {
        return status;
    }

    @Override
    public String toString() {
        StringBuilder info = new StringBuilder();

        info.append("\nCargo: Motorista")
            .append("\nCodigo do Motorista: "+codMotorista)
            .append("\nStatus: "+status);

        return info.toString();
    }



}


public class Caminhao {

    private String marca, modelo, placa;
    private int ano, contaManutencao = 0;
    private boolean status;
    private double quilometragemInicial;
    private double quilometragemFinal;  
    private Manutencao manutencaoPreventiva;

    public Caminhao(String marca, String modelo, String placa, int ano, boolean status, double quilometragemInicial, double quilometragemFinal) {
        super();
        this.marca = marca;
        this.modelo = modelo;
        this.placa = placa;
        this.ano = ano;
        this.status=status;
        this.quilometragemInicial = quilometragemInicial;
        this.quilometragemFinal = quilometragemFinal;

    }


    public String getMarca() {
        return marca;
    }

    public String getModelo() {
        return modelo;
    }

    public String getPlaca() {
        return placa;
    }

    public int getAno() {
        return ano;
    }

    public double getQuilometragemInicial() {
        return quilometragemInicial;
    }

    public double getQuilometragemFinal() {
        return quilometragemFinal;
    }

    public boolean getStatus() {
        return status;      
    }


    @Override
    public String toString() {

        StringBuilder info = new StringBuilder();

        info.append("Dados do Caminhao: ")
            .append("\nMarca: "+marca)
            .append("\nModelo: "+modelo)
            .append("\nPlaca: "+placa)
            .append("\nAno: "+ano)
            .append("\nConta Manutencao: "+contaManutencao)
            .append("\nStatus Caminhao: "+status)
            .append("\nQuilometragem Inicial: "+quilometragemInicial)
            .append("\nQuilometragem Final: "+quilometragemFinal)
            .append("ManutencaoPreventiva: "+manutencaoPreventiva);
        return info.toString();
    }




}

import java.util.Arrays;

public class Viagem{

    private String horaSaida, horaChegada, dataChegada, dataSaida, destino, origem;
    private int codViagem;
    int indiceGeral=0;
    private Manutencao revisao;
    private Caminhao []veiculo = new Caminhao[20];
    private Motorista[]piloto = new Motorista[15];


    public Viagem(String horaSaida, String horaChegada, String dataChegada, String dataSaida, String destino,
            String origem, int codViagem) {
        super();
        this.horaSaida = horaSaida;
        this.horaChegada = horaChegada;
        this.dataChegada = dataChegada;
        this.dataSaida = dataSaida;
        this.destino = destino;
        this.origem = origem;
        this.codViagem = codViagem;
    }

    public String getHoraSaida() {
        return horaSaida;
    }

    public String getHoraChegada() {
        return horaChegada;
    }

    public String getDataChegada() {
        return dataChegada;
    }

    public String getDataSaida() {
        return dataSaida;
    }

    public String getDestino() {
        return destino;
    }

    public String getOrigem() {
        return origem;
    }

    public int getCodViagem() {
        return codViagem;
    }



    public boolean verificaStatus(Caminhao veiculo, Motorista piloto){

        Caminhao auxC = (Caminhao) veiculo;
        Motorista aux = (Motorista) piloto;

        if(auxC.getStatus() != true && aux.getStatus() != true){
            return true;
        }
        else{
            return false;
        }

    }

    public Viagem planejarViagem(Viagem []viagem, Caminhao []veiculo, Motorista []piloto){
        if(viagem[indiceGeral].verificaStatus(veiculo[indiceGeral], piloto[indiceGeral]) == true){
            indiceGeral++;
            return viagem[indiceGeral];
        }
        return null;
    }

    public double calculaKm(double kmInicial, double kmFinal){
        return kmFinal-kmInicial;
    }

    @Override
    public String toString() {
        StringBuilder info = new StringBuilder();

        info.append("Viagem \nHora Saida: "+horaSaida+" hrs")
            .append("\nHora Chegada: "+horaChegada+" hrs")
            .append("\nData Chegada: "+dataChegada)
            .append("\nData Saida: "+dataSaida)
            .append("\nDestino: "+destino)
            .append("\nOrigem: "+origem)
            .append("\nCodigo da Viagem "+codViagem)
            .append("\nViagens realizadas: "+indiceGeral)
            .append("\nRevisao: "+revisao)
            .append("\nDados do Veículo: "+veiculo.toString())
            .append("\nQuilometragem percorrida: "+calculaKm(kmInicial, kmFinal))
            .append("\n\nPiloto que realizou a viagem: "+piloto.toString());

        return info.toString();
    }





}
  • 1

    A trip can have 20 trucks and 15 drivers? For what? What is this super() in the builder? The journey can exist without a truck and associated drivers? What is the intention of the verificaStstus()? Is there any reason for using the tag [tag:array-associative]? You know what this is about?

  • 1

    A trip has only one truck and one driver, the super() is the default automatically generated by Eclipse, and refers to the Object class. The journey can only exist if there are drivers and associated trucks. And the verified method Tatus makes the control of drivers and trucks, in case they are busy (traveling), it will not be possible to make the trip. As for the array-associative tag, I must have been mistaken at the time of typing the tags and ended up entering unintentionally...

  • 1

    The trip can be created with truck and driver busy, but prevented from taking place, or when this occurs, the trip can not even be created?

  • 1

    Attributes of type Boolean by default are started as false, so the method verifiStatus with your if does the following: if the status of a truck or driver is false, it is not busy, so the method returns true, saying that it is possible to perform the journey. Otherwise, the return indicates false, as they are busy and the journey is prevented from being performed...

  • 1

    Did the answer solve your problem? Do you think you can accept it? If you don’t know how you do it, check out [tour]. This would help a lot to indicate that the solution was useful to you and to give an indication that there was a satisfactory solution. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

3

Although I have strange things I will answer according to what is described in the question and in the comments. If the requirements are wrong it is clear that the solution will go wrong, but then the problem is not one of programming. Perhaps the difficulty in coding is because of ambiguous or conflicting requirements.

First the array truck and driver since it can only have one on each trip:

private Caminhao veiculo;
private Motorista piloto;

We set up the contractor. We took the call to super() that doesn’t make any sense (if Eclipse does this, it’s a very bad IDE). And we put a parameter to fill these fields, after all the trip cannot exist without this data. That’s what builders are for.

public Viagem(String horaSaida, String horaChegada, String dataChegada, String dataSaida, String destino, String origem, int codViagem, Caminhao, caminhao, Motorista, motorista) {
    this.horaSaida = horaSaida;
    this.horaChegada = horaChegada;
    this.dataChegada = dataChegada;
    this.dataSaida = dataSaida;
    this.destino = destino;
    this.origem = origem;
    this.codViagem = codViagem;
    this.caminhao = caminhao;
    this.piloto = motorista;
}

A method can be greatly simplified:

public boolean verificaStatus(Caminhao veiculo, Motorista piloto) {
    return !veiculo.getStatus() && !piloto.getStatus();
}

I didn’t even say that you’re probably exaggerating the use of getters and setters.

I put in the Github for future reference.

I can’t understand the method planejarViagem() placed later. It does not seem to make the slightest sense and is at odds with the description of the problem. There may be a widespread error in code and class planning, but I can’t say without knowing the right requirements. Maybe this method indicates that the requirements are wrong. But I can only work with what was expressed.

In fact you need to learn more how boolean expressions work. It is used redundantly there too.

Usually the toString() is for internal code use. I’m not saying it’s wrong, but listing all of the object’s information isn’t usually right. When you need it it is best to have a more appropriate method. It seems to be resource abuse.

It would also be good to maintain a standard of naming things. I find it strange to have seats that the driver happens to be called the pilot and the vehicle truck. Vehicle seems to me more correct always unless there is somewhere that needs the exact discrimination of the type of vehicle, which does not seem to be the case, there is no structure for this.

Browser other questions tagged

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