// LANDSCAPE GARDENING QUOTE // Frans Coenen // University of Liverpool // 18 April 2013 import java.util.*; import java.io.*; // PrintWriter class import javax.swing.*; // Contains JOptionPane class /* File format 0 = month 1 = lawn length 2 = lawn width 3 = lawn unit cost 4 = lawn unit installation time 5 = patio length 6 = patio width 7 = patio unit cost 8 = patio unit installation time 9 = water feature quantity 10 = water feature unit cost 11 = water feature unit installation time */ class LandsGardQuote extends JFrame { // ------------------- CONSTANTS ------------------------ /** Constant for installation (labour) time (minutes) for lawn type 1 item (per m^2). */ private static double INST_TIME_LAWN = 20.0; /** Constant for installation (labour) time (minutes) for patio type 1 item (per m^2). */ private static double INST_TIME_PATIO = 20.0; /** Constant for installation (labour) time (minutes) for water feature type 2 item (per item). */ private static double INST_TIME_WATER_FEATURE = 60.0; /** Constant for labour cost. */ private static double LABOUR_COST = 16.49; /** Filename for list of quotes made to date. */ private static String QUOTES_TO_DATE_FILE = "quotesToDateFile.txt"; // ------------------- FIELDS ------------------------ /** Instance of Scanner class (to allow keyboard input). */ private static Scanner input = new Scanner(System.in); /** Material cost (£) for lawn type 1 item (per m^2). */ private double materialCostLawn = 15.5; /** Material cost (£) for patio type 1 item (per m^2). */ private double materialCostPatio = 20.99; /** Material cost (£) for water feature type 2 item (per item). */ private double materialCostWaterFeature = 150.0; /** The current quote. */ private Quote myQuote = null; // ------------------ CONSTRUCTORS ------------------------ /* ------ None, default constructor ------ */ // --------------- METHODS TO GENERATE A NEW QUOTE (MENU OPTION 1) --------------- /** Top level method to prepare a quote. */ public void prepareAnewQuote() { // Input inputQuoteDetail(); // Calculate working costs myQuote.calculateCosts(); // Output quote details to screen outputQuoteDetailsToScreen(); // Output quote details to file outputQuoteDetailsToFile(); } private void inputQuoteDetail() { // Input month System.out.print("Input month (For example May): "); String month = input.nextLine(); // Create quote elements (type 1 and type 2) QuoteItemType1 lawn = new QuoteItemType1("lawn",materialCostLawn,INST_TIME_LAWN); QuoteItemType1 patio = new QuoteItemType1("concrete patio",materialCostPatio,INST_TIME_PATIO); QuoteItemType2 waterFeature = new QuoteItemType2("water feature",materialCostWaterFeature, INST_TIME_WATER_FEATURE); // Create Quote myQuote = new Quote(month,lawn,patio,waterFeature,LABOUR_COST); } /** Method to output quote details to screen. */ private void outputQuoteDetailsToScreen() { System.out.println(myQuote); } /** Method to output quote details to file. */ private void outputQuoteDetailsToFile() { // Create a FileHandlingUtility instance FileHandlingUtility newFHU = new FileHandlingUtility(); if (newFHU.openOutputFile()) { // Get instance of PrintWriter class. */ PrintWriter fileOutput = newFHU.getfileOutput(); // Output myQuote.outputToFile(fileOutput); // End by closing file fileOutput.close(); // Get name of last file opened String fileName = newFHU.getfileName(); System.out.println("Quote written to " + fileName); // Create new PrintWriterInstance for "quotesToDateFile.txt" file so as to // store name of latest quote file (uses same PrintWriter instance name // as above) try { fileOutput = new PrintWriter(new FileWriter(QUOTES_TO_DATE_FILE,true)); // Append the file name for the most recent quote to the //"quotesToDateFile.txt" file fileOutput.println(fileName); // Again end by closing "quotesToDateFile.txt" filefile fileOutput.close(); } catch(IOException ioException) { JOptionPane.showMessageDialog(this,"Error opening File", "Error",JOptionPane.ERROR_MESSAGE); } } } // ------------- METHODS TO LOAD AND VIEW A SAVED QUOTE (MENU OPTION 2) ------------- /** Method to load and view a previously generated quote. */ public void viewExistingQuote() { // Create a FileHandlingUtility instance FileHandlingUtility newFHU = new FileHandlingUtility(); // select and read quote file (if successfully opened) if (newFHU.openInputFile()) { // Get data array String[] data = newFHU.getDataArray(); // Process viewExistingQuote(data); } } /** Method to view a quote that has been uploaded from file. @param data the uploaded file data (in string format). */ private void viewExistingQuote(String[] data) { // Create quote elements (type 1 and type 2) QuoteItemType1 lawn = new QuoteItemType1("lawn", Double.parseDouble(data[1]),Double.parseDouble(data[2]), Double.parseDouble(data[3]),Double.parseDouble(data[4])); QuoteItemType1 patio = new QuoteItemType1("concrete patio", Double.parseDouble(data[5]),Double.parseDouble(data[6]), Double.parseDouble(data[7]),Double.parseDouble(data[8])); QuoteItemType2 waterFeature = new QuoteItemType2("water feature", Integer.parseInt(data[9]),Double.parseDouble(data[10]), Double.parseDouble(data[11])); // Create Quote myQuote = new Quote(data[0],lawn,patio,waterFeature,LABOUR_COST); // Calculate working costs myQuote.calculateCosts(); // Output quote details to screen outputQuoteDetailsToScreen(); } // ------------- METHODS TO LOAD NEW MATERIAL COST DATA (MENU OPTION 3) ------------- /** Method to load a new set of material costs. */ public void loadNewMaterialCosts() { // Create a FileHandlingUtility instance FileHandlingUtility newFHU = new FileHandlingUtility(); // select and read quote file (if successfully opened) if (newFHU.openInputFile()) { // Get data array String[] data = newFHU.getDataArray(); // Set new material cost values materialCostLawn = Double.parseDouble(data[0]); materialCostPatio = Double.parseDouble(data[1]); materialCostWaterFeature = Double.parseDouble(data[2]); // Output outputMaterialCost(); } } // ---------------- OUTPUT METHODS --------------------- /** Method to output material costs. */ public void outputMaterialCost() { System.out.println("Material Costs:" + "\n\tLawn = " + materialCostLawn + "\n\tConcrete patio = " + materialCostPatio + "\n\tWater feature = " + materialCostWaterFeature + "\n"); } }