0
Good morning, I am developing a very simple stock system where the items will be stored in a matrix follows code:
package main;
import java.io.Console;
import java.util.Arrays;
import java.util.Scanner;
public class IncluirItem{
public static void main(String[] args) {
String[][] itens = new String[2][5];
String[] campos = new String[7];
int opcao = 0;
int remover;
int verificar = 0;
int adcionar = 0;
int tamanho = 2;
Scanner sc = new Scanner(System.in);
campos [1] = "Nome";
campos [2] = "Codigo de barras";
campos [3] = "quantidade";
campos [4] = "validade";
campos [5] = "data de entrada";
do {
System.out.println("\n\n### Sistema de estoque - simplificado ###");
System.out.println("=========================================");
System.out.println(" | 1 - Adcionar itens |");
System.out.println(" | 2 - Excluir itens |");
System.out.println(" | 3 - Mostrar itens |");
System.out.println(" | 0 - Sair |");
System.out.println("=========================================\n");
System.out.println("Escolha uma opcao: ");
opcao = sc.nextInt();
System.out.print("\n");
switch (opcao) {
case 1:
System.out.println("Cadastro de itens: ");
//Adciona itens
for (int i = 0; i <2; i++) {
System.out.println("Adcionar item? ");
System.out.println("1 para sim \n2 para nao: ");
adcionar = sc.nextInt();
if (adcionar == 1) {
System.out.printf("%d. item \n", (i+1));
verificar = verificar + 1;
for (int j=0; j < 5; j++) {
System.out.printf("%s ", campos[j+1]);
System.out.printf("= ", i,(j+1));
itens[i][j] = sc.next();
}
System.out.printf("\n");
}
else {
break;
}
}
break;
case 2:
//Remove itens
System.out.println("Exclusao de itens: ");
if(verificar > 0) {
for (int i = 0; i <2; i++) {
System.out.printf("%d. item \n", (i+1));
System.out.printf("ID do produto= 00%d \n", (i+1));
System.out.printf("\n");
}
}
else {
System.out.println("Nenhum item listado!");
}
System.out.println("\nEscolha o numero do item a ser removido: \n");
remover = sc.nextInt();
for (int i = 0; i <2; i++) {
if(remover == 2) {
itens[i] = itens[i-1];
}
}
break;
case 3:
//Mostra itens
if(verificar > 0) {
for (int i = 0; i <2; i++) {
if (verificar == 2) {
System.out.printf("%d. item \n", (i+1));
System.out.printf("ID do produto= 00%d \n", (i+1));
for (int j=0; j < 5; j++) {
System.out.printf("%s ", campos[j+1]);
System.out.printf("= %s \n", itens [i][j]);
}
System.out.printf("\n");
}
else {
itens = Arrays.copyOf(itens, tamanho - 1);
verificar = 0;
System.out.printf("%d. item \n", (i+1));
System.out.printf("ID do produto= 00%d \n", (i+1));
for (int j=0; j < 5; j++) {
System.out.printf("%s ", campos[j+1]);
System.out.printf("= %s \n", itens [i][j]);
}
System.out.printf("\n");
break;
}
}
}
else {
System.out.println("Nenhum item listado!");
}
break;
default:
System.out.println("Opção Inválida!");
break;
}
} while(opcao != 0);
}
}
My doubt is the following, I want to delete a position of the matrix, example: I recorded two items, in column one and column two of the matrix and I would like to remove column one from the list, as I perform this function, and another thing, I would like when I insert for the second time some item the matrix starts at the next empty possible, currently if I ask to insert a new item the matrix starts from the first position and so erases the elements already inserted.
This solution worked for me, I now need to delete the vector position =(
– Gabriel Rodrigues Pavolin