Pulizia estetica

This commit is contained in:
La Programmatrice Verde
2025-11-18 21:59:44 +01:00
parent d67f5e7581
commit 088bff78d5

View File

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