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