No prodotti duplicati
This commit is contained in:
@@ -90,106 +90,118 @@ public class listaSpesa {
|
||||
float scontoFidelityCard = 0;
|
||||
Date dataScadenza = null;
|
||||
String tipoMateriale = null;
|
||||
boolean error, isAlimentare = true;
|
||||
boolean error = false, isAlimentare = true;
|
||||
String scelta;
|
||||
String descrizione;
|
||||
|
||||
System.out.print("Inserire il nome: ");
|
||||
nome = sc.nextLine();
|
||||
|
||||
System.out.print("Inserire la descrizione: ");
|
||||
descrizione = sc.nextLine();
|
||||
|
||||
Prodotto prodotto;
|
||||
|
||||
do {
|
||||
try {
|
||||
System.out.print("Inserire il nome: ");
|
||||
nome = sc.nextLine();
|
||||
|
||||
error = false;
|
||||
System.out.print("Inserire il prezzo: ");
|
||||
prezzo = sc.nextFloat();
|
||||
sc.nextLine();
|
||||
if (prezzo <= 0) {
|
||||
System.out.println("Errore: non è possibile inserire un prezzo minore o uguale a zero.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: numero non valido.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
System.out.print("Inserire la descrizione: ");
|
||||
descrizione = sc.nextLine();
|
||||
|
||||
do {
|
||||
try {
|
||||
error = false;
|
||||
System.out.print("Inserire lo sconto con carta fedeltà (in %): ");
|
||||
scontoFidelityCard = sc.nextFloat() / 100;
|
||||
sc.nextLine();
|
||||
if (scontoFidelityCard < 0) {
|
||||
System.out.println("Errore: non è possibile inserire uno sconto negativo.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
do {
|
||||
try {
|
||||
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: numero non valido.");
|
||||
}
|
||||
} while (error);
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Il prodotto è un genere alimentare? [S/n]");
|
||||
scelta = sc.nextLine().toLowerCase();
|
||||
|
||||
if (scelta.equals("y") || scelta.equals("s")) {
|
||||
isAlimentare = true;
|
||||
|
||||
boolean dateError;
|
||||
do {
|
||||
dateError = false;
|
||||
try {
|
||||
System.out.print("Inserire la data di scadenza nel formato " + datePattern + ": ");
|
||||
dataScadenza = sdf.parse(sc.nextLine());
|
||||
} catch (ParseException e) {
|
||||
System.out.println("Errore nella lettura della data.");
|
||||
error = false;
|
||||
System.out.print("Inserire il prezzo: ");
|
||||
prezzo = sc.nextFloat();
|
||||
sc.nextLine();
|
||||
if (prezzo <= 0) {
|
||||
System.out.println("Errore: non è possibile inserire un prezzo minore o uguale a zero.");
|
||||
pausa();
|
||||
dateError = true;
|
||||
error = true;
|
||||
}
|
||||
} while (dateError);
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: numero non valido.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
} else if (scelta.equals("n")) {
|
||||
isAlimentare = false;
|
||||
do {
|
||||
try {
|
||||
error = false;
|
||||
System.out.print("Inserire lo sconto con carta fedeltà (in %): ");
|
||||
scontoFidelityCard = sc.nextFloat() / 100;
|
||||
sc.nextLine();
|
||||
if (scontoFidelityCard < 0) {
|
||||
System.out.println("Errore: non è possibile inserire uno sconto negativo.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
|
||||
System.out.println("Inserire il materiale del prodotto: ");
|
||||
tipoMateriale = sc.nextLine();
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: numero non valido.");
|
||||
}
|
||||
} while (error);
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Il prodotto è un genere alimentare? [S/n]");
|
||||
scelta = sc.nextLine().toLowerCase();
|
||||
|
||||
if (scelta.equals("y") || scelta.equals("s")) {
|
||||
isAlimentare = true;
|
||||
|
||||
boolean dateError;
|
||||
do {
|
||||
dateError = false;
|
||||
try {
|
||||
System.out.print("Inserire la data di scadenza nel formato " + datePattern + ": ");
|
||||
dataScadenza = sdf.parse(sc.nextLine());
|
||||
} catch (ParseException e) {
|
||||
System.out.println("Errore nella lettura della data.");
|
||||
pausa();
|
||||
dateError = true;
|
||||
}
|
||||
} while (dateError);
|
||||
|
||||
} else if (scelta.equals("n")) {
|
||||
isAlimentare = false;
|
||||
|
||||
System.out.println("Inserire il materiale del prodotto: ");
|
||||
tipoMateriale = sc.nextLine();
|
||||
} else {
|
||||
System.out.println("Errore: opzione non valida.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
do {
|
||||
try {
|
||||
error = false;
|
||||
System.out.print("Inserire la quantità: ");
|
||||
qta = isAlimentare ? sc.nextFloat() : (float) sc.nextInt();
|
||||
sc.nextLine();
|
||||
if (qta <= 0) {
|
||||
System.out.println("Errore: non è possibile inserire meno di un prodotto.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: numero non valido.");
|
||||
}
|
||||
} while (error);
|
||||
|
||||
if (isAlimentare) {
|
||||
prodotto = new Alimentare(nome, prezzo, qta, scontoFidelityCard, dataScadenza, descrizione);
|
||||
} else {
|
||||
System.out.println("Errore: opzione non valida.");
|
||||
prodotto = new NonAlimentare(nome, prezzo, qta, scontoFidelityCard, tipoMateriale, descrizione);
|
||||
}
|
||||
if (trovaProdottoInMagazzino(prodotto) != -1) {
|
||||
magazzino[indiceMagazzino] = prodotto;
|
||||
} else {
|
||||
System.out.println("Errore: il prodotto esiste già nel magazzino.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
do {
|
||||
try {
|
||||
error = false;
|
||||
System.out.print("Inserire la quantità: ");
|
||||
qta = isAlimentare ? sc.nextFloat() : (float) sc.nextInt();
|
||||
sc.nextLine();
|
||||
if (qta <= 0) {
|
||||
System.out.println("Errore: non è possibile inserire meno di un prodotto.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: numero non valido.");
|
||||
}
|
||||
} while (error);
|
||||
|
||||
magazzino[indiceMagazzino] = isAlimentare
|
||||
? new Alimentare(nome, prezzo, qta, scontoFidelityCard, dataScadenza, descrizione)
|
||||
: new NonAlimentare(nome, prezzo, qta, scontoFidelityCard, tipoMateriale, descrizione);
|
||||
|
||||
}
|
||||
|
||||
public static void inserisciDatiScontrino() {
|
||||
|
||||
Reference in New Issue
Block a user