diff --git a/TravelCostCalculator.java b/TravelCostCalculator.java index 6f89acb..969e5ca 100644 --- a/TravelCostCalculator.java +++ b/TravelCostCalculator.java @@ -1,3 +1,6 @@ +// EG/2020/4119 +// Praveenan J. + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -6,50 +9,55 @@ import java.util.Map; public class TravelCostCalculator { - static Map a = new HashMap<>(); - static Map b = new HashMap<>(); - static Map c = new HashMap<>(); + static Map hotelRates = new HashMap<>(); + static Map exchangeRates = new HashMap<>(); + static Map flightCosts = new HashMap<>(); - static void l1(String file) throws IOException { + // This is the function for Hotel Rates + static void loadHotelRates(String file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); - String i; - while ((i = reader.readLine()) != null) { - String[] p = i.split(","); - a.put(p[0].toUpperCase(), Double.parseDouble(p[1])); + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(","); + hotelRates.put(parts[0].toUpperCase(), Double.parseDouble(parts[1])); + } } - static void l2(String file) throws IOException { + // This is the function for Exchange Rates + static void loadExchangeRates(String file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); - String i; - while ((i = reader.readLine()) != null) { - String[] p = i.split(","); - b.put(p[0].toUpperCase(), Double.parseDouble(p[1])); + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(","); + exchangeRates.put(parts[0].toUpperCase(), Double.parseDouble(parts[1])); } } - static void l3(String file) throws IOException { + + // This is the function for Flight Costs + static void loadFlightCosts(String file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); - String i; - while ((i = reader.readLine()) != null) { - String[] p = i.split(","); - c.put(p[0].toUpperCase(), Double.parseDouble(p[1])); + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(","); + flightCosts.put(parts[0].toUpperCase(), Double.parseDouble(parts[1])); } } public static void main(String[] args) { try { - l1("data/hotel_rates.csv"); - l2("data/exchange_rates.csv"); - l3("data/flight_costs.csv"); + loadHotelRates("data/hotel_rates.csv"); + LoadExchangeRates("data/exchange_rates.csv"); + loadFlightCosts("data/flight_costs.csv"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your destination: "); String destination = reader.readLine().toUpperCase(); - double flight_cost = c.getOrDefault(destination, 0.0); - double hotel_cost = a.getOrDefault(destination, 0.0); + double flight_cost = flightCosts.getOrDefault(destination, 0.0); + double hotel_cost = hotelRates.getOrDefault(destination, 0.0); System.out.print("Enter your stay duration in days: "); int stay_duration = Integer.parseInt(reader.readLine()); @@ -61,15 +69,15 @@ public static void main(String[] args) { System.out.printf("Hotel cost (%d days): USD %.2f\n", stay_duration, hotel_cost); System.out.printf("Total: USD %.2f\n", total_cost_usd); - String[] available_currencies = b.keySet().toArray(new String[0]); + String[] available_currencies = exchangeRates.keySet().toArray(new String[0]); System.out.print("Select your currency for final price estimation(" + String.join(", ", available_currencies) + "): "); String selected_currency = reader.readLine(); - double final_price_local_currency = total_cost_usd * b.get(selected_currency); + double final_price_local_currency = total_cost_usd * exchangeRates.get(selected_currency); System.out.printf("Total in %s: %.2f\n", selected_currency, final_price_local_currency); - } catch (IOException e) { - e.printStackTrace(); + } catch (IOException exchange) { + exchange.printStackTrace(); } } }