3 java classes, one calling the other, how to call first-to-last methods? (bluej)

Asked

Viewed 579 times

0

I have 3 classes, they are: rental, cars, and customer. Rental contains cars, and customer contains rental. If I want to, in customer class, call one of the car class methods, how do I do? For example, a client object contains a location vector, and each of these locations has a car. I want, for example, the return ta car plate, in customer class.

  • Do you already have any code? Or even the class diagram? Heritage wouldn’t help you? Or even a basic interface

  • I’ve been trying to do it for a while, but since the code depends on this part for me to go on, I couldn’t write anymore, unfortunately.

  • It’s not my strong suit, but I believe you’ll have to use some design pattern( Desin patters - Gof) to resolve, because to retouch the car plate in the customer class seems very similar to the Iterator behavioral pattern, take a look at it and see if things get clearer there

3 answers

1

Classes can be used to declare variables within other classes.

EX:

public class A {
  private String nome;

  public A(String nome){
    this.nome = nome;
  }

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

In another class

public class B {
  private A a;

  public B() {
    a = new A("Test");
  }

  public void print() {
    System.out.println(a.getNome());
  }
}

As you did not put the code of your classes I created here some very simple classes to facilitate the explanation.

The explanations are commented on in the code itself, if you have any doubts then just ask.

Java car.

public class Carro {
    private String marca, ano;

    // Cria um carro com sua marca e ano.
    public Carro(String marca, String ano) {
        this.marca = marca;
        this.ano = ano;
    }

    // Métodos acessores

    public String getMarca() { return marca; }

    public void setMarca(String marca) { this.marca = marca; }

    public String getAno() { return ano; }

    public void setAno(String ano) { this.ano = ano; }
}

Java leasing.

import java.util.List;
import java.util.ArrayList;

public class Locacao {
    // Lista de carros.
    private List<Carro> carros;

    // Cria uma locacao com uma lista de carros vazia.
    public Locacao() {
        this.carros = new ArrayList<>();
    }

    // Métodos acessores

    public List<Carro> getCarros() { return carros; }

    public void setCarros(List<Carro> carros) { this.carros = carros; }
}

Java client.


public class Cliente {
    private String nome;
    private Locacao locacao;

    // Cria um cliente com seu nome e locacao.
    public Cliente(String nome) {
        this.nome = nome;
        this.locacao = new Locacao();
    }

    // Métodos acessores

    public String getNome() { return nome; }

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

    public Locacao getLocacao() { return locacao; }

    public void setLocacao(Locacao locacao) { this.locacao = locacao; }
}

Main java.

import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Cria um cliente.
        Cliente cliente = new Cliente("José");

        // Cria três carros.
        Carro carro1 = new Carro("Fiat", "2015");
        Carro carro2 = new Carro("Hyundai", "2017");
        Carro carro3 = new Carro("Jeep", "2019");

        // Pega a lista de carros que pertence a classe Locacao que pertence a classe Cliente.
        List<Carro> carros = cliente.getLocacao().getCarros();

        // Adiciona os três carros a lista.
        carros.add(carro1);
        carros.add(carro2);
        carros.add(carro3);

        System.out.printf("A locação do cliente %s possui %d carros.\n", cliente.getNome(), carros.size());

        // Faz um loop na lista de carros, mostrando cada carro com sua marca e ano.
        for(Carro carro: carros){
            System.out.printf("Carro(marca=%s, ano=%s)\n", carro.getMarca(), carro.getAno());
        }
    }
}

0

I don’t know how it works in bluej, but in netbeans and others just instantiate the class

Carros carro = new Carros();

Object placa = carro.metodoPegaPlaca(idCarro);

That would put you in the client class

I hope I helped you.

0

Since you did not put the code, I made one from scratch, take a look to see if it clears the doubt, I did not use methods accessors trying to make as clear as possible.


Declaring Variables

public class Carros{
       String placa;

}

public class Locacao{
       String marcaDocarro;

}

public class Cliente{
       String nome;

}

Declaring values

Carros novoCarro = new Carros;

novoCarro.placa = "AAA-1111";

Locacao novaLocacao = new Locacao;

novaLocacao.marcaDocarro = "FORD";

Cliente novoCliente = new Cliente;

novoCliente.nome = "joão";

Assigning calls in classes

public class Locacao{

       String marcaDocarro;

//chamando o Objeto Carros

Carros novoCarroLocado;

}

public class Cliente{

       String nome;

//chamando o Objeto Locacao

Locacao novoCarroLocado;

}

Declaring the call in the main class

Carros novoCarro = new Carros;

novoCarro.placa = "AAA-1111";

novaLocacao.novoCarroLocado(novoCarro );

Locacao novaLocacao = new Locacao;

novaLocacao.marcaDocarro = "FORD";

novoCliente.novoCarroLocado(novaLocacao );

Calling out the data

System.out.println(novaLocacao.novoCarroLocado(novoCarro.placa);

System.out.println(novoCliente.novoCarroLocado(novaLocacao.marcaDocarro );

Browser other questions tagged

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