Formattazione scontrino

This commit is contained in:
La Programmatrice Verde
2025-11-18 21:35:07 +01:00
parent 09fae5caf6
commit d374731cbe

View File

@@ -283,34 +283,50 @@ public class FatturaScontrini {
static String ordinazioneToString(ArrayList<int[]> ordinazione) {
StringBuilder sb = new StringBuilder();
String[][] menu = new String[2][getMenuSize()];
String[][] menu = tabellaNomePrezzo();
double totale = 0;
try (BufferedReader br = new BufferedReader(new FileReader(PATH_MENU))) {
String riga = br.readLine();
for (int i = 0; i < getMenuSize(); i++) {
menu[0][i] = riga.split(":")[0];
menu[1][i] = riga.split(":")[1].trim();
riga = br.readLine();
}
// Larghezze colonne
int larghezzaNome = 50;
int larghezzaPrezzoQuantita = 10;
String formattatoreLarghezzaNome = "%-" + larghezzaNome + "s"; // %-50s
String formattatoreLarghezzaPrezzoQuantita = "%-" + larghezzaPrezzoQuantita + "s"; // %-10s
// Intestazione tabella
sb.append(String.format(formattatoreLarghezzaNome, "Nome"));
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Prezzo"));
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Quantità"));
sb.append("\n");
sb.append("Nome\tPrezzo\tQuantità\n");
for (int i = 0; i < ordinazione.size(); i++) {
sb.append(menu[0][ordinazione.get(i)[0]])
.append("\t")
.append(menu[1][ordinazione.get(i)[0]])
.append("\t")
.append(ordinazione.get(i)[1])
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];
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();
} // crea tabella nome e prezzo
} catch (IOException e) {
System.out.println("Errore nella lettura del file: " + e.getMessage());
}
return sb.toString();
return ritorno;
}
}