2
I would like to create a product registration program using the ArrayList
.
How I put a loop to read NOME
, MODELO
, TAMANHO
and PREÇO
and store this first loop at an array list location?
What code I did is entering each data into an array location.
public class CadastroProduto {
ArrayList cadastroProduto = new ArrayList();
private String nome;
private String modelo;
private String tamanho;
private double preco;
public String getNome() {
return nome;
}
public void setNome(String nome) {
cadastroProduto.add(nome);
this.nome = nome;
}
public String getModelo() {
return modelo;
}
public void setModelo(String modelo) {
cadastroProduto.add(modelo);
this.modelo = modelo;
}
public String getTamanho() {
return tamanho;
}
public void setTamanho(String tamanho) {
cadastroProduto.add(tamanho);
this.tamanho = tamanho;
}
public double getPreco() {
return preco;
}
public void setPreco(double preco) {
cadastroProduto.add(preco);
this.preco = preco;
}
}
public static void main(String[] args) {
Scanner dados = new Scanner(System.in);
Scanner dados1 = new Scanner(System.in);
Scanner dados2 = new Scanner(System.in);
Scanner dados3 = new Scanner(System.in);
CadastroProduto c1 = new CadastroProduto();
String nome = "";
String modelo = "";
String tamanho = "";
double preco = 0;
System.out.println("Opçoes\n 1-Cadastrar produto\n 2-Remover produto\n 3-Verificar produto\n 4-Dinheiro\n");
int numopc = 0;
System.out.print("nº: ");
numopc = dados.nextInt();
while (numopc == 1) {
switch (numopc) {
case 1:
System.out.println("Nome do produto: ");
nome = dados1.nextLine();
c1.setNome(nome);
System.out.println("Modelo do produto: ");
modelo = dados2.nextLine();
c1.setModelo(modelo);
System.out.println("Tamanho do produto: ");
tamanho = dados3.nextLine();
c1.setTamanho(tamanho);
System.out.println("Preço do produto: ");
preco = dados.nextDouble();
c1.setPreco(preco);
System.out.println("\n Cadastrar mais produtos?\n 1-Sim\n 0-Não\n");
numopc = dados.nextInt();
break;
case 2:
break;
}
}
System.out.println(c1.cadastroProduto.toString());
}
}
There is, but it’s not worth doing for an exercise, I usually do this kind of thing creating abstractions, but almost nobody does, and for those who are starting I do not recommend, at least until you have the confidence to figure out how to do it on your own because it’s tip Oda thing that requires a certain dominance, independence to accomplish. At the moment you are making mistakes on much more basic things, like creating several scanners no need, organization of what is a class (the most serious problem), follow cake recipes that nothing helps the code get better, create unnecessary variables, repeat...
– Maniero
Read this: https://answall.com/a/387029/101
– Maniero
Is this an exercise for college? would have the statement there to show (I’ve done some work like this before in college)?
– user12100
Maniero, Scanner if I create only one it jumps a data input dairy and do not know why it is only right with several scanner (gambiarra kk). I’ll check the link I send, thank you
– Pedro
@Elizeu, it’s not college exercise. I’m doing it on my own. if recommend to do otherwise that is possible for me, I am just q beginner I am in the second semester of college.
– Pedro
@Pedro follows below a project I have just created. Any questions please use the chat on this link as soon as I can tell you: https://chat.stackexchange.com/rooms/info/95753/duvida-projeto-crud-com-array-lista?tab=general
– user12100