diff --git a/src/fatturascontrini/FatturaScontrini.java b/src/fatturascontrini/FatturaScontrini.java index 1bfd08c..f4e062a 100644 --- a/src/fatturascontrini/FatturaScontrini.java +++ b/src/fatturascontrini/FatturaScontrini.java @@ -124,6 +124,7 @@ public class FatturaScontrini { boolean error; boolean isDuplicate = false; String scelta; + do { exit = true; int[] ordine = { selezioneMenu(), quantita() }; @@ -156,6 +157,93 @@ public class FatturaScontrini { return ritorno; } + 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 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_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_FILE + e.getMessage()); + } + return i; + } + + 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; + } + static boolean isOrdinazioneDuplicata(ArrayList ordinazione, int[] ordine) { boolean ritorno = false; boolean error; @@ -190,103 +278,20 @@ public class FatturaScontrini { 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_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_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; - } - static void pagare(ArrayList ordinazione) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm"); Date now = new Date(); String contenutoScontrino; try (BufferedWriter bw = new BufferedWriter( new FileWriter("./src/fatturascontrini/scontrino_" + sdf.format(now) + ".txt"))) { + sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); - contenutoScontrino = "Codice scontrino: " + codiceScontrino++ + "\nData e ora di emissione: " - + sdf.format(now) + "\n\n" - + ordinazioneToString(ordinazione); + + contenutoScontrino = "Codice scontrino: " + codiceScontrino++ + + "\nData e ora di emissione: " + sdf.format(now) + + "\n\n" + + ordinazioneToString(ordinazione); + System.out.println(contenutoScontrino); bw.write(contenutoScontrino); } catch (IOException e) { @@ -302,8 +307,8 @@ public class FatturaScontrini { // Larghezze colonne int larghezzaNome = 50; int larghezzaPrezzoQuantita = 10; - String formattatoreLarghezzaNome = "%-" + larghezzaNome + "s"; // %-50s - String formattatoreLarghezzaPrezzoQuantita = "%-" + larghezzaPrezzoQuantita + "s"; // %-10s + String formattatoreLarghezzaNome = "%-" + larghezzaNome + "s"; + String formattatoreLarghezzaPrezzoQuantita = "%-" + larghezzaPrezzoQuantita + "s"; // Intestazione tabella sb.append(String.format(formattatoreLarghezzaNome, "Nome")); @@ -312,14 +317,11 @@ public class FatturaScontrini { sb.append("\n"); for (int i = 0; i < ordinazione.size(); i++) { - sb.append(String.format(formattatoreLarghezzaNome, menu[0][ordinazione.get(i)[0]].trim()))// nome - .append(String.format(formattatoreLarghezzaPrezzoQuantita, - menu[1][ordinazione.get(i)[0]].trim()))// prezzo - .append(String.format(formattatoreLarghezzaPrezzoQuantita, - Integer.toString(ordinazione.get(i)[1])))// quantità - .append("\n"); - totale += Double.parseDouble(menu[1][ordinazione.get(i)[0]].replaceFirst(",", ".")) - * ordinazione.get(i)[1]; + 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); @@ -336,7 +338,7 @@ public class FatturaScontrini { ritorno[0][i] = riga.split(":")[0]; ritorno[1][i] = riga.split(":")[1].trim(); riga = br.readLine(); - } // crea tabella nome e prezzo + } } catch (IOException e) { System.out.println(ERRORE_FILE + e.getMessage()); }