diff --git a/TravelCostCalculator.java b/TravelCostCalculator.java index 6f89acb..d20d4d3 100644 --- a/TravelCostCalculator.java +++ b/TravelCostCalculator.java @@ -1,3 +1,6 @@ +// Name: Vihan D. I. +// Index No: EG/2020/4323 + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -6,70 +9,73 @@ import java.util.Map; public class TravelCostCalculator { - static Map a = new HashMap<>(); - static Map b = new HashMap<>(); - static Map c = new HashMap<>(); + static Map hotelRatesData = new HashMap<>(); + static Map exchangeRatesData = new HashMap<>(); + static Map flightCostsData = new HashMap<>(); - static void l1(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])); + // For getting data from hotel_rates.csv file + static void fetchHotelRatesData(String hotelRatesDataFile) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(hotelRatesDataFile)); + String input; + while ((input = reader.readLine()) != null) { + String[] p = input.split(","); + hotelRatesData.put(p[0].toUpperCase(), Double.parseDouble(p[1])); } } - static void l2(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])); + // For getting data from exchange_rates.csv file + static void fetchExchangeRatesData(String exchangeRatesDataFile) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(exchangeRatesDataFile)); + String input; + while ((input = reader.readLine()) != null) { + String[] p = input.split(","); + exchangeRatesData.put(p[0].toUpperCase(), Double.parseDouble(p[1])); } } - static void l3(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])); + // For getting data from flight_costs.csv file + static void fetchFlightCostsData(String flightCostsDataFile) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(flightCostsDataFile)); + String input; + while ((input = reader.readLine()) != null) { + String[] p = input.split(","); + flightCostsData.put(p[0].toUpperCase(), Double.parseDouble(p[1])); } } public static void main(String[] args) { try { - l1("data/hotel_rates.csv"); - l2("data/exchange_rates.csv"); - l3("data/flight_costs.csv"); + fetchHotelRatesData("data/hotel_rates.csv"); + fetchExchangeRatesData("data/exchange_rates.csv"); + fetchFlightCostsData("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 flightCost = flightCostsData.getOrDefault(destination, 0.0); + double hotelCost = hotelRatesData.getOrDefault(destination, 0.0); System.out.print("Enter your stay duration in days: "); - int stay_duration = Integer.parseInt(reader.readLine()); - hotel_cost *= stay_duration; + int stayDuration = Integer.parseInt(reader.readLine()); + hotelCost *= stayDuration; - double total_cost_usd = flight_cost + hotel_cost; + double totalCostUsd = flightCost + hotelCost; - System.out.printf("Flight cost: USD %.2f\n", flight_cost); - System.out.printf("Hotel cost (%d days): USD %.2f\n", stay_duration, hotel_cost); - System.out.printf("Total: USD %.2f\n", total_cost_usd); + System.out.printf("Flight cost: USD %.2f\n", flightCost); + System.out.printf("Hotel cost (%d days): USD %.2f\n", stayDuration, hotelCost); + System.out.printf("Total: USD %.2f\n", totalCostUsd); - String[] available_currencies = b.keySet().toArray(new String[0]); - System.out.print("Select your currency for final price estimation(" + String.join(", ", available_currencies) + "): "); - String selected_currency = reader.readLine(); + String[] availableCurrencies = exchangeRatesData.keySet().toArray(new String[0]); + System.out.print("Select your currency for final price estimation(" + String.join(", ", availableCurrencies) + "): "); + String selectedCurrency = reader.readLine(); - double final_price_local_currency = total_cost_usd * b.get(selected_currency); + double finalPriceLocalCurrency = totalCostUsd * exchangeRatesData.get(selectedCurrency); - System.out.printf("Total in %s: %.2f\n", selected_currency, final_price_local_currency); - } catch (IOException e) { - e.printStackTrace(); + System.out.printf("Total in %s: %.2f\n", selectedCurrency, finalPriceLocalCurrency); + } catch (IOException error) { + error.printStackTrace(); } } }