I need to create a class carro
. The program should contain a collection of ArrayList
cars. Your program should allow you to add, delete, and query cars at ArrayList
.
I've implemented two programs one for main
and one for carro.java
. So my doubt is that I can enter and query data from the cars, but if I enter data for three cars shows only the last one, it points to the last car.
Class main.java
:
import java.util.ArrayList;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Carro car = new Carro();
int op;
do {
System.out.println("[1] Inserir");
System.out.println("[2] Consultar");
System.out.println("[3] Remover");
System.out.println("[4] Sair");
System.out.print("Opção desejada: ");
op = input.nextInt();
Object matricula;
switch (op) {
case 1:
input.nextLine(); /*Pretendo Inserir dados*/
System.out.print("Matricula: ");
car.setMatricula(input.nextLine());
System.out.print("Marca: ");
car.setMarca(input.nextLine());
System.out.print("Modelo: ");
car.setModelo(input.nextLine());
System.out.print("Cor: ");
car.setCor(input.nextLine());
break;
case 2:
System.out.println("Matricula: " + car.getMatricula());
System.out.println("Marca: " + car.getMarca());
System.out.println("Modelo: " + car.getModelo());
System.out.println("Cor: " + car.getCor());
break;
case 3:
System.out.print("Matricula: ");
car.setMatricula("");
car.setMarca("");
car.setModelo("");
car.setCor("");
break;
}
} while (op != 4);
}
public class Stand {
private ArrayList<Carro> listaStand = new ArrayList<Carro>();
public void insereCarro(Stand Carro) //aqui pretendo inserir dados carro*/
{
listaStand.add(Carro);
}
public void consultaCarro() //Aqui pretendo fazer consulta dos carros do stand */
{
for (Carro c: listaStand)
{
System.out.println(listaStand.get(0));
}
}
public String removeCarro(String Stand) { //*aqui pretendo remover carro pela matricula
for (Carro c : this.listaStand) {
if (c.getMatricula().equals(Stand))
c.remove();
}
}}}
Class carro.java
public class Carro
{
String marca;
String modelo;
String matricula;
String cor;
private String Stand;
public void Carro(String matricula, String marca, String modelo, String cor)
{
this.matricula = matricula;
this.marca = marca;
this.modelo = modelo;
this.cor = cor;
}
public String getMatricula() {
return matricula;
}
public void setMatricula(String matricula) {
this.matricula = matricula;
}
public String getMarca() {
return marca;
}
public void setMarca(String marca) {
this.marca = marca;
}
public String getModelo() {
return modelo;
}
public void setModelo(String modelo) {
this.modelo = modelo;
}
public String getCor() {
return matricula;
}
public void setCor(String cor) {
this.cor = cor;
}
@Override
public String toString() {
return " matricula=" + matricula +",marca=" + marca + ", modelo=" + modelo + ",cor=" + cor;
}
}