diff --git a/src/fatturascontrini/FatturaScontrini.java b/src/fatturascontrini/FatturaScontrini.java index 3898547..5002192 100644 --- a/src/fatturascontrini/FatturaScontrini.java +++ b/src/fatturascontrini/FatturaScontrini.java @@ -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 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 cancellaInOrdinazione(ArrayList 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 cancellaOrdine(ArrayList 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 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 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 trovaScontriniPerData(ArrayList scontrini, String data) { ArrayList ritorno = new ArrayList<>();