Ordinazione
This commit is contained in:
@@ -4,8 +4,13 @@
|
|||||||
*/
|
*/
|
||||||
package fatturascontrini;
|
package fatturascontrini;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.InputMismatchException;
|
import java.util.InputMismatchException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -18,15 +23,21 @@ public class FatturaScontrini {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static Scanner sc = new Scanner(System.in);
|
static Scanner sc = new Scanner(System.in);
|
||||||
|
static final String PATH_MENU = "./src/fatturascontrini/menu.txt";
|
||||||
|
static final String ERRORE_DEFAULT = "Errore: opzione non valida.";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int scelta = -1;
|
int scelta = -1;
|
||||||
|
ArrayList<int[]> ordinazione;
|
||||||
|
boolean ordinazioneEffettuata = false;
|
||||||
|
String sceltaSiNo;
|
||||||
|
boolean error;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
System.out.println("Scegliere un'opzione:");
|
System.out.println("Scegliere un'opzione:");
|
||||||
System.out.println("1. ");
|
System.out.println("1. Effettuare ordinazione");
|
||||||
System.out.println("2. ");
|
System.out.println("2. Pagare");
|
||||||
System.out.println("3. ");
|
System.out.println("3. Crea fattura");
|
||||||
System.out.println("0. Esci");
|
System.out.println("0. Esci");
|
||||||
System.out.print("Opzione: ");
|
System.out.print("Opzione: ");
|
||||||
|
|
||||||
@@ -38,7 +49,30 @@ public class FatturaScontrini {
|
|||||||
case 0:
|
case 0:
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
|
if (ordinazioneEffettuata) {
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
System.out.print(
|
||||||
|
"Attenzione: verrà sovrascritta la precedente ordinazione. Continuare? [S/N] ");
|
||||||
|
sceltaSiNo = sc.nextLine().trim().toLowerCase();
|
||||||
|
switch (sceltaSiNo) {
|
||||||
|
case "s":
|
||||||
|
ordinazione = ordinazione();
|
||||||
|
break;
|
||||||
|
case "n":
|
||||||
|
System.out.println("L'ordinazione non è stata modificata.");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println(ERRORE_DEFAULT);
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (error);
|
||||||
|
} else {
|
||||||
|
ordinazione = ordinazione();
|
||||||
|
ordinazioneEffettuata = true;
|
||||||
|
}
|
||||||
pausa();
|
pausa();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
@@ -54,8 +88,7 @@ public class FatturaScontrini {
|
|||||||
pausa();
|
pausa();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
} catch (InputMismatchException _) {
|
||||||
catch (InputMismatchException _) {
|
|
||||||
System.out.println("Errore: scelta non valida.");
|
System.out.println("Errore: scelta non valida.");
|
||||||
pausa();
|
pausa();
|
||||||
}
|
}
|
||||||
@@ -66,4 +99,155 @@ public class FatturaScontrini {
|
|||||||
System.out.println("Premere un tasto per continuare. . .");
|
System.out.println("Premere un tasto per continuare. . .");
|
||||||
sc.nextLine();
|
sc.nextLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ArrayList<int[]> ordinazione() {
|
||||||
|
ArrayList<int[]> ritorno = new ArrayList<>();
|
||||||
|
boolean exit = true;
|
||||||
|
boolean error;
|
||||||
|
boolean isDuplicate = false;
|
||||||
|
String scelta;
|
||||||
|
do {
|
||||||
|
if (exit) {
|
||||||
|
exit = false;
|
||||||
|
int[] ordine = { selezioneMenu(), quantita() };
|
||||||
|
for (int[] ordini : ritorno) {
|
||||||
|
if (ordini[0] == ordine[0]) {
|
||||||
|
isDuplicate = true;
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
System.out
|
||||||
|
.print(
|
||||||
|
"L'opzione scelta è già presente nell'ordinazione. Aggiornare la quantità? [S/N] ");
|
||||||
|
scelta = sc.nextLine().trim().toLowerCase();
|
||||||
|
switch (scelta) {
|
||||||
|
case "s":
|
||||||
|
ritorno.get(ritorno.indexOf(ordini))[1] = ordine[1];
|
||||||
|
break;
|
||||||
|
case "n":
|
||||||
|
System.out.println("L'ordinazione non è stata modificata.");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println(ERRORE_DEFAULT);
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (error);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (!isDuplicate) {
|
||||||
|
ritorno.add(ordine);
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
System.out.print("Inserire un altra ordinazione? [S/N] ");
|
||||||
|
scelta = sc.nextLine().trim().toLowerCase();
|
||||||
|
switch (scelta) {
|
||||||
|
case "s":
|
||||||
|
exit = true;
|
||||||
|
break;
|
||||||
|
case "n":
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println(ERRORE_DEFAULT);
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (error);
|
||||||
|
// controllo se c'è già e aggiorno la quantità
|
||||||
|
}
|
||||||
|
} while (exit);
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stampaMenu() {
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(PATH_MENU))) {
|
||||||
|
StringTokenizer st;
|
||||||
|
String riga = br.readLine();
|
||||||
|
|
||||||
|
for (int i = 0; i < getMenuSize(); i++) {
|
||||||
|
st = new StringTokenizer(riga, ":");
|
||||||
|
System.out.println(i + ". " + st.nextToken() + ": €" + st.nextToken().trim());
|
||||||
|
System.out.println("\t" + st.nextToken().trim() + "\n");
|
||||||
|
riga = br.readLine();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Errore nella lettura del file: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getMenuSize() {
|
||||||
|
int i = 0;
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(PATH_MENU))) {
|
||||||
|
String riga = br.readLine();
|
||||||
|
|
||||||
|
while (riga != null) {
|
||||||
|
i++;
|
||||||
|
riga = br.readLine();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Errore nella lettura del file: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int selezioneMenu() {
|
||||||
|
boolean error;
|
||||||
|
int ritorno = -1;
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
|
||||||
|
System.out.println("Selezionare un'opzione tra le presenti:");
|
||||||
|
stampaMenu();
|
||||||
|
System.out.print("Scelta: ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ritorno = sc.nextInt();
|
||||||
|
sc.nextLine();
|
||||||
|
|
||||||
|
if (ritorno < 0 || ritorno > getMenuSize()) {
|
||||||
|
System.out.println(ERRORE_DEFAULT);
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
} catch (InputMismatchException _) {
|
||||||
|
System.out.println(ERRORE_DEFAULT);
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
} while (error);
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int quantita() {
|
||||||
|
boolean error;
|
||||||
|
int ritorno = -1;
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
|
||||||
|
System.out.print("Inserire la quantità da acquistare: ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ritorno = sc.nextInt();
|
||||||
|
sc.nextLine();
|
||||||
|
|
||||||
|
if (ritorno < 0) {
|
||||||
|
System.out.println("Errore: non è possibile selezionare una quantità minore di zero.");
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
} catch (InputMismatchException _) {
|
||||||
|
System.out.println(ERRORE_DEFAULT);
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
} while (error);
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
src/fatturascontrini/menu.txt
Normal file
8
src/fatturascontrini/menu.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
cappuccino:1,30:lorem ipsum dolor sit
|
||||||
|
caffè:1,00:lorem ipsum dolor sit
|
||||||
|
brioche:0,80:lorem ipsum dolor sit
|
||||||
|
brioche_marmellata:1,00:lorem ipsum dolor sit
|
||||||
|
cioccolata:1,50:lorem ipsum dolor sit
|
||||||
|
spremuta:1,40:lorem ipsum dolor sit
|
||||||
|
toast: 1,70:lorem ipsum dolor sit
|
||||||
|
panino a scelta con affettato: 3,50:lorem ipsum dolor sit
|
||||||
Reference in New Issue
Block a user