Inserimento ordinazione completo

This commit is contained in:
La Programmatrice Verde
2025-11-25 11:54:02 +01:00
parent 641262ffe0
commit 9eb3d2825d

View File

@@ -60,19 +60,14 @@ public class FatturaScontrini {
pausa();
break;
case 2:
if (ordinazione != null) {
pagare(ordinazione);
} else {
System.out.println(
"Errore: è necessario effettuare un'ordinazione prima di procedere al pagamento.");
}
ordinazione = placeOrdinazione(ordinazione);
pausa();
break;
case 3:
if (!trovaScontrini().isEmpty()) {
fattura();
} else {
System.out.println(
System.out.println(
"Errore: è necessario pagare prima di procedere alla creazione della fattura.");
}
pausa();
@@ -144,15 +139,20 @@ public class FatturaScontrini {
ritorno.add(ordine);
}
System.out.println(ordinazioneToString(ritorno));
do {
error = false;
System.out.print("Inserire un altra ordinazione? [S/N] ");
System.out.print("Modificare l'ordinazione? [S/N] "); // TODO: feedback UX
scelta = sc.nextLine().trim().toLowerCase();
switch (scelta) {
case "s":
ritorno = cancellaInOrdinazione(ritorno);
exit = false;
break;
case "n":
System.out.println("Stampa dello scontrino. . .");
pagare(ritorno);
break;
default:
System.out.println(ERRORE_DEFAULT);
@@ -286,8 +286,120 @@ public class FatturaScontrini {
return ritorno;
}
static String ordinazioneToString(ArrayList<int[]> ordinazione) {
StringBuilder sb = new StringBuilder();
String[][] menu = tabellaNomePrezzo();
double totale = 0;
// Larghezze colonne
int larghezzaNome = 50;
int larghezzaPrezzoQuantita = 10;
String formattatoreLarghezzaNome = "%-" + larghezzaNome + "s";
String formattatoreLarghezzaPrezzoQuantita = "%-" + larghezzaPrezzoQuantita + "s";
// Intestazione tabella
sb.append(String.format(formattatoreLarghezzaNome, "Nome"));
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Prezzo"));
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Quantità"));
sb.append("\n");
for (int i = 0; i < ordinazione.size(); i++) {
sb.append(String.format(formattatoreLarghezzaNome, menu[0][ordinazione.get(i)[0]].trim()));// nome
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, menu[1][ordinazione.get(i)[0]].trim()));// prezzo
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, Integer.toString(ordinazione.get(i)[1])));// quantità
sb.append("\n");
totale += Double.parseDouble(menu[1][ordinazione.get(i)[0]].replace("", "").trim())
* ordinazione.get(i)[1];
}
sb.append("\nTotale: " + totale);
return sb.toString();
}
static String[][] tabellaNomePrezzo() {
String[][] ritorno = new String[2][getMenuSize()];
try (BufferedReader br = new BufferedReader(new FileReader(PATH_MENU))) {
String riga = br.readLine();
for (int i = 0; i < getMenuSize(); i++) {
ritorno[0][i] = riga.split(":")[0];
ritorno[1][i] = riga.split(":")[1].trim();
riga = br.readLine();
}
} catch (IOException e) {
System.out.println(ERRORE_FILE + e.getMessage());
}
return ritorno;
}
static ArrayList<int[]> cancellaInOrdinazione(ArrayList<int[]> ordinazione) {
String scelta;
boolean error;
do {
error = false;
System.out.print("Eliminare un ordinazione? [S/N] ");
scelta = sc.nextLine().trim().toLowerCase();
switch (scelta) {
case "s":
ordinazione = cancellaOrdine(ordinazione);
System.out.println("Ordine cancellato con successo.");
pausa();
break;
case "n":
System.out.println("Nessuna ordinazione è stata cancellata.");
pausa();
break;
default:
System.out.println(ERRORE_DEFAULT);
pausa();
error = true;
break;
}
} while (error);
return ordinazione;
}
static ArrayList<int[]> cancellaOrdine(ArrayList<int[]> ordinazione) {
int scelta = -1;
String[][] menu = tabellaNomePrezzo();
boolean error;
do {
error = false;
System.out.println("Quale opzione eliminare tra le seguenti?");
for (int i = 0; i < ordinazione.size(); i++) {
System.out.println(i + ". " + menu[0][ordinazione.get(i)[0]].trim() + ": "
+ menu[1][ordinazione.get(i)[0]].trim());
}
System.out.print("Scelta: ");
try {
scelta = sc.nextInt();
sc.nextLine();
if (scelta < 0 || scelta >= getMenuSize()) {
System.out.println(ERRORE_DEFAULT);
pausa();
error = true;
}
} catch (InputMismatchException _) {
System.out.println(ERRORE_DEFAULT);
pausa();
error = true;
}
} while (error);
ordinazione.remove(scelta);
return ordinazione;
}
static void pagare(ArrayList<int[]> ordinazione) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Date now = new Date();
String contenutoScontrino;
@@ -314,52 +426,6 @@ public class FatturaScontrini {
}
}
static String ordinazioneToString(ArrayList<int[]> ordinazione) {
StringBuilder sb = new StringBuilder();
String[][] menu = tabellaNomePrezzo();
double totale = 0;
// Larghezze colonne
int larghezzaNome = 50;
int larghezzaPrezzoQuantita = 10;
String formattatoreLarghezzaNome = "%-" + larghezzaNome + "s";
String formattatoreLarghezzaPrezzoQuantita = "%-" + larghezzaPrezzoQuantita + "s";
// Intestazione tabella
sb.append(String.format(formattatoreLarghezzaNome, "Nome"));
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Prezzo"));
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Quantità"));
sb.append("\n");
for (int i = 0; i < ordinazione.size(); i++) {
sb.append(String.format(formattatoreLarghezzaNome, menu[0][ordinazione.get(i)[0]].trim()));// nome
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, menu[1][ordinazione.get(i)[0]].trim()));// prezzo
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, Integer.toString(ordinazione.get(i)[1])));// quantità
sb.append("\n");
totale += Double.parseDouble(menu[1][ordinazione.get(i)[0]].replaceFirst(",", ".")) * ordinazione.get(i)[1];
}
sb.append("\nTotale: " + totale);
return sb.toString();
}
static String[][] tabellaNomePrezzo() {
String[][] ritorno = new String[2][getMenuSize()];
try (BufferedReader br = new BufferedReader(new FileReader(PATH_MENU))) {
String riga = br.readLine();
for (int i = 0; i < getMenuSize(); i++) {
ritorno[0][i] = riga.split(":")[0];
ritorno[1][i] = riga.split(":")[1].trim();
riga = br.readLine();
}
} catch (IOException e) {
System.out.println(ERRORE_FILE + e.getMessage());
}
return ritorno;
}
static void fattura() {
boolean error;
String formatoDataInserimento = "dd/MM/yyyy";
@@ -388,9 +454,9 @@ public class FatturaScontrini {
if (listaScontrini.isEmpty()) {
System.out.println("Errore: non sono stati trovati scontrini per la data inserita.");
}
else{
stampaFattura(new Fattura(listaScontrini.size(), calcolaTotaleScontrini(listaScontrini)), sdf.format(dataRicerca));
} else {
stampaFattura(new Fattura(listaScontrini.size(), calcolaTotaleScontrini(listaScontrini)),
sdf.format(dataRicerca));
}
}
@@ -409,7 +475,7 @@ public class FatturaScontrini {
return ritorno;
}
static ArrayList<File> trovaScontriniPerData(ArrayList<File> scontrini, String data) {
ArrayList<File> ritorno = new ArrayList<>();