package BillGUI; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.LinkedHashMap; import java.util.Vector; /** * A sample garage application * @author Pierre-André Mudry, HES-SO Valais 2010-2016 */ public class GarageManager { private LinkedHashMap services = new LinkedHashMap(); GarageManager() { // The various services provided in this garage services.put("Oil level control", 20); services.put("Tire replacement ", 50); services.put("Windshield exchange", 60); services.put("Oil filter change", 210); services.put("Battery replacement", 320); services.put("Pollution control", 200); services.put("Brake revision", 400); } String[] getServices() { return services.keySet().toArray(new String[services.size()]); } int[] vectorToArray(Vector prestations) { Integer[] v = prestations.toArray(new Integer[prestations.size()]); int[] array = new int[v.length]; for (int i = 0; i < v.length; i++) array[i] = v[i].intValue(); return array; } String generateBill(Vector prestations) { return generateBill(vectorToArray(prestations)); } String generateHTMLBill(Vector prestations) { return generateHTMLBill(vectorToArray(prestations)); } /** * Generates an HTML formatted string with the bill * * @param prestations * An array containing all the prestations * @return The HTML formatted string */ String generateHTMLBill(int[] prestations) { int total_sum = 0; Object[] keys = services.keySet().toArray(); String result = ""; result += "

"; result += "

Super Auto 20000 bill

"; result += "
Version 1.0

"; result += "

"; // Create an HTML table result += ""; result += ""; result += ""; result += ""; result += ""; for (int i = 0; i < prestations.length; i++) { if (prestations[i] > services.size()) { System.out.println("Error, non existing prestation !"); System.exit(-1); } String cKey = (String) keys[prestations[i]]; // HTML row result += ""; result += ""; result += ""; total_sum += services.get(cKey); } result += ""; result += ""; result += ""; result += ""; // End of the HTML table result += "
PrestationPrice
" + cKey + "" + services.get(cKey) + "
Total price" + total_sum + "
"; result += "


"; // Horizontal line result += "Lab 15 generator"; result += ""; return result; } /** * Generates a text bill * * @param prestations * An array containing all the prestations * @return The generated text bill */ String generateBill(int[] prestations) { int total_sum = 0; Object[] keys = services.keySet().toArray(); String result = ""; result += "*************************\n"; result += "* Super Auto 20000 bill ****\n"; result += "*******************************\n\n"; for (int i = 0; i < prestations.length; i++) { if (prestations[i] > services.size()) { System.out.println("Error, non existing prestation !"); System.exit(-1); } String cKey = (String) keys[prestations[i]]; result += "- " + cKey + " \t" + services.get(cKey) + "\n"; total_sum += services.get(cKey); } result += "\n----------------------------------\n"; result += " Bill total \t\t" + total_sum + "\n"; result += "----------------------------------\n"; result += "\nPayment in 30 days. Thank you !"; return result; } public static void main(String[] args) { GarageManager gm = new GarageManager(); // Prestation 0 is "Oil level control" // Prestation 1 is "Tire replacement " // Prestation 2 is "Windshield exchange" // Prestation 3 is "Oil filter change" // Prestation 4 is "Battery replacement" // Prestation 5 is "Pollution control" // Prestation 6 is "Brake revision" int[] client1 = { 1, 1, 1, 1, 4, 6, 0 }; String bill1 = gm.generateBill(client1); System.out.println(bill1); PrintWriter outputStreamName; try { outputStreamName = new PrintWriter(new FileOutputStream("bill.txt")); outputStreamName.print(bill1); outputStreamName.close(); } catch (Exception e) { e.printStackTrace(); } } }