It is time for holiday break and you are looking for an airline ticket. There ar
ID: 3640453 • Letter: I
Question
It is time for holiday break and you are looking for an airline ticket. There are four major
airlines in the area (Spirit, Northwest, Southwest & American). Your assignment is to calculate
and print the total cost of a round trip ticket for each line in the data file.
Each line of the input file has the following layout:
Price (the cost of the ticket for that particular trip, it should be doubled if it is one way)
Airport fees (needs to be doubled for a round trip if given for one way)
Sales tax (on the total price of the ticket, including the airport fees, 0.06 is 6% etc.)
Airline name (SP, NW, SW, AA)
Origin City (can be one or two words)
One-way/roundtrip (1 means one way, 2 means round trip)
Destination City (may be one or two words)
These are examples of some trips during the winter break:
99.99 10.00 0.06 SP Detroit 1 Palm Beach
143.95 13.00 0.06 NW Detroit 1 Palm Beach
99.00 10.00 0.10 SP Las Vegas 2 Detroit
You need to write a program to read the file one record at a time, then print the itinerary for
that trip and calculate the ticket price as a round trip ticket (even if it was given as a
one-way trip in the file.) The program will read the code letters for the name of the airline
from the input and then expand them to the full name in the output. For example, NW, will
become Northwest Airline, and SP will print Spirit Airline. The program also will read the
name of the city from the input and abbreviate it. If the city is one word, then the abbreviation
will be the first and last letters of the word. Both letters should be capitalized. If the city
is two words, then the abbreviation will be the first letter of each word.
A sample output for 99.99 10.00 0.06 SP Detroit 1 Palm Beach is:
The round trip price from DT to PB using Spirit Airline is $233.18
And the sample output for 143.95 13.00 0.06 NW Detroit 1 Palm Beach is:
The round trip price from DT to PB using Northwest Airline is $332.73
The program must be designed using functions, enums and string functions in addition to the user
defined functions to get the data, process the file and print the results. You may assume that
there are no errors in the input file. (A suggested enum would be for the two-letter code for
the airlines.)
Explanation / Answer
Hi, Keep AirLineInputFile.txt file in C drive, or change to the specific directory in the AirLineProj.java main program. There are two files in a package AirLine. ConstantsIF .java AirLineProj.java first file ConstantsIF .java ********************************************************************************************************* package AirLine; public class ConstantsIF { public static final String PRICE = "price"; public static final String AIRPORT_FEES = "Airport Fees"; public static final String SALES_TAX = "Sales Tax"; public static final String AIRLINE_NAME = "AirLine Name" ; public static final String ORIGIN_CITY = "Origin City"; public static final String TRIP_CATEGORY = "Trip Category"; public static final String DESTN_CITY = "Destination City"; public static final String TOTAL_PRICE = "Total Price"; public static final double tax = 0.06; } ******************************************************************************************************* second file AirLineProj.java package AirLine; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; public class AirLineProj { AirLineEnum myAirLine; String price = null; String airportFees = null; String salesTax = null; String airLineName = null; String originCity = null; String tripCategory = null; String destCity = null; /** * This method is used to process the input file line by line. * * @param fileName * @return List * @throws FileNotFoundException * @throws IOException */ public List processLineByLine(String fileName) throws FileNotFoundException, IOException { List resList = new ArrayList(); try { HashMap hashMap = new HashMap(); if (fileName != null) { FileReader input = new FileReader(fileName); BufferedReader bufRead = new BufferedReader(input); String thisLine = null; while ((thisLine = bufRead.readLine()) != null) { hashMap = processLine(thisLine); resList.add(hashMap); } bufRead.close(); } else { System.out.println("File Name is null"); } } catch (FileNotFoundException e1) { // If file not found in current directory throw e1; } catch (IOException e) { // If IO exception is generated, print a stack trace throw e; } return resList; } /** * This Method is used to parse the single line at a time and get the * relevant fields * * @param aLine * @return HashMap */ protected HashMap processLine(String aLine) { HashMap resMap = new HashMap(); List strList = new ArrayList(); StringTokenizer st = new StringTokenizer(aLine); while (st.hasMoreTokens()) { strList.add(st.nextToken()); } price = strList.get(0); airportFees = strList.get(1); salesTax = strList.get(2); airLineName = strList.get(3); originCity = strList.get(4); if (strList.get(5).equals("1") || strList.get(5).equals("2")) { tripCategory = strList.get(5); if (strList.size() < 8) { destCity = strList.get(6); } else { destCity = strList.get(6); destCity = destCity + " "; destCity = destCity.concat(strList.get(7)); } } else { originCity = originCity + " "; originCity = originCity.concat(strList.get(5)); tripCategory = strList.get(6); if (strList.size() < 9) { destCity = strList.get(7); } else { destCity = strList.get(7); destCity = destCity + " "; destCity = destCity.concat(strList.get(8)); } } airLineName = AirLineEnum.valueOf(airLineName).getAirLineType(); originCity = processCity(originCity); destCity = processCity(destCity); resMap.put(ConstantsIF.PRICE, price); resMap.put(ConstantsIF.AIRPORT_FEES, airportFees); resMap.put(ConstantsIF.SALES_TAX, salesTax); resMap.put(ConstantsIF.AIRLINE_NAME, airLineName); resMap.put(ConstantsIF.ORIGIN_CITY, originCity); resMap.put(ConstantsIF.TRIP_CATEGORY, tripCategory); resMap.put(ConstantsIF.DESTN_CITY, destCity); String totalPrice = calcPrice(resMap); resMap.put(ConstantsIF.TOTAL_PRICE, totalPrice); return resMap; } /** * This method is used to process the city to get the 2 char city code * * @param city * @return String */ public String processCity(String city) { String tempCity = city; if (city.contains(" ")) { StringTokenizer stringTokenizer = new StringTokenizer(tempCity); String firstName = stringTokenizer.nextToken(); String lastName = stringTokenizer.nextToken(); String firstChar = (String) firstName.subSequence(0, 1); String secondChar = (String) lastName.subSequence(0, 1); tempCity = firstChar.concat(secondChar); tempCity = tempCity.toUpperCase(); } else { int size = tempCity.length(); String firstChar = (String) tempCity.subSequence(0, 1); String secondChar = (String) tempCity.subSequence(size - 1, size); tempCity = firstChar.concat(secondChar); tempCity = tempCity.toUpperCase(); } return tempCity; } /** * This method is used to calculate the total price of the trip * * @param resMap * @return String */ public String calcPrice(HashMap resMap) { String total = null; double totalPrice = 0.0; double tempPrice = 0.0; double tempAirPortFees = 0.0; double saleTax = 0.0; price = resMap.get(ConstantsIF.PRICE); airportFees = resMap.get(ConstantsIF.AIRPORT_FEES); salesTax = resMap.get(ConstantsIF.SALES_TAX); airLineName = resMap.get(ConstantsIF.AIRLINE_NAME); originCity = resMap.get(ConstantsIF.ORIGIN_CITY); tripCategory = resMap.get(ConstantsIF.TRIP_CATEGORY); destCity = resMap.get(ConstantsIF.DESTN_CITY); if (tripCategory.equals("1")) { tempPrice = Double.parseDouble(price) * 2; tempAirPortFees = Double.parseDouble(airportFees) * 2; saleTax = calcSalesTax(tempPrice, tempAirPortFees); totalPrice = tempPrice + tempAirPortFees + saleTax; } else { tempPrice = Double.parseDouble(price); tempAirPortFees = Double.parseDouble(airportFees); saleTax = calcSalesTax(tempPrice, tempAirPortFees); totalPrice = tempPrice + tempAirPortFees + saleTax; } DecimalFormat decimalFormat = new DecimalFormat("#.##"); total = decimalFormat.format(totalPrice); return total; } /** * This method is used to calculate sales tax * * @param tempPrice * @param tempAirPortFees * @return double */ public double calcSalesTax(double tempPrice, double tempAirPortFees) { double saleTax = 0.0; saleTax = (tempPrice + tempAirPortFees) * ConstantsIF.tax; return saleTax; } /** * This method is used to display the ailLine Details * * @param resList */ public void displayData(List resList) { HashMap resMap = new HashMap(); if (resList != null) { Iterator it = resList.iterator(); while (it.hasNext()) { resMap = it.next(); price = resMap.get(ConstantsIF.PRICE); airportFees = resMap.get(ConstantsIF.AIRPORT_FEES); salesTax = resMap.get(ConstantsIF.SALES_TAX); airLineName = resMap.get(ConstantsIF.AIRLINE_NAME); originCity = resMap.get(ConstantsIF.ORIGIN_CITY); tripCategory = resMap.get(ConstantsIF.TRIP_CATEGORY); destCity = resMap.get(ConstantsIF.DESTN_CITY); String totalPrice = resMap.get(ConstantsIF.TOTAL_PRICE); System.out.println("The round trip price from " + originCity + " to " + destCity + " using " + airLineName + " is $" + totalPrice); } } else { System.out.println("List is empty"); } } public static void main(String args[]) { // System.out.println(myAirLine.SP.getAirLineType()); String fileName = "C:\AirLineInputFile.txt"; AirLineProj airLineProj = new AirLineProj(); List hashMap; try { hashMap = airLineProj.processLineByLine(fileName); airLineProj.displayData(hashMap); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * AirLine Enum for the AirLine Types * */ enum AirLineEnum { SP("Spirit Airline"), NW("Northwest Airline"), SW("Southwest Airline"), AA( "American Airline"); private String airLineType; private AirLineEnum(String airLineType) { this.airLineType = airLineType; } public String getAirLineType() { return this.airLineType; } } ****************************************************************************************************** sample Input File AirLineInputFile.txt 99.99 10.00 0.06 SP Detroit 1 Palm Beach 143.95 13.00 0.06 NW Detroit 1 Palm Beach 99.00 10.00 0.10 SP Las Vegas 2 Detroit ******************************************************************************************************* Sample Output The round trip price from DT to PB using Spirit Airline is $233.18 The round trip price from DT to PB using Northwest Airline is $332.73 The round trip price from LV to DT using Spirit Airline is $115.54Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.