diff --git a/src/fatturascontrini/FatturaScontrini.java b/src/fatturascontrini/FatturaScontrini.java index b2c6573..556db93 100644 --- a/src/fatturascontrini/FatturaScontrini.java +++ b/src/fatturascontrini/FatturaScontrini.java @@ -283,34 +283,50 @@ public class FatturaScontrini { static String ordinazioneToString(ArrayList ordinazione) { StringBuilder sb = new StringBuilder(); - String[][] menu = new String[2][getMenuSize()]; + String[][] menu = tabellaNomePrezzo(); double totale = 0; + + // 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"); + + 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("\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++) { - menu[0][i] = riga.split(":")[0]; - menu[1][i] = riga.split(":")[1].trim(); + ritorno[0][i] = riga.split(":")[0]; + ritorno[1][i] = riga.split(":")[1].trim(); riga = br.readLine(); - } - - 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]) - .append("\n"); - totale += Double.parseDouble(menu[1][ordinazione.get(i)[0]].replaceFirst(",", ".")) * ordinazione.get(i)[1]; - } - - sb.append("\nTotale: " + totale); - + } // crea tabella nome e prezzo } catch (IOException e) { System.out.println("Errore nella lettura del file: " + e.getMessage()); } - - return sb.toString(); + return ritorno; } }