From d2131e439cbf520b9a3123c76d73598694d6e808 Mon Sep 17 00:00:00 2001 From: Pramitha Jayasooriya <123730262+PramithaMJ@users.noreply.github.com> Date: Fri, 22 Sep 2023 11:02:49 +0000 Subject: [PATCH 1/2] // EG/2020/3990 --- TravelCostCalculator.java | 80 +++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/TravelCostCalculator.java b/TravelCostCalculator.java index 6f89acb..2e01495 100644 --- a/TravelCostCalculator.java +++ b/TravelCostCalculator.java @@ -6,70 +6,76 @@ import java.util.Map; public class TravelCostCalculator { - static Map a = new HashMap<>(); - static Map b = new HashMap<>(); - static Map c = new HashMap<>(); + private static final Map hotel = new HashMap<>(); + private static final Map exchange = new HashMap<>(); + private static final Map flight = 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])); + // load hotel from line + private static void loadHotel(String file) throws IOException { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + String[] part = line.split(","); + hotelRates.put(part[0].toUpperCase(), Double.parseDouble(part[1])); + } } } - static void l2(String file) throws IOException { + // load exchange from line + static void loadExchange(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[] part = line.split(","); + b.put(part[0].toUpperCase(), Double.parseDouble(part[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])); + // load flight from line + private static void loadFlight(String file) throws IOException { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + String[] part = line.split(","); + flightCosts.put(part[0].toUpperCase(), Double.parseDouble(part[1])); + } } } public static void main(String[] args) { try { - l1("data/hotel_rates.csv"); - l2("data/exchange_rates.csv"); - l3("data/flight_costs.csv"); + loadHotel("data/hotel_rates.csv"); + loadExchange("data/exchange_rates.csv"); + loadFlight("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 = flight.getOrDefault(destination, 0.0); + double hotelCost = hotel.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 = exchange.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 * exchange.get(selectedCurrency); - System.out.printf("Total in %s: %.2f\n", selected_currency, final_price_local_currency); + System.out.printf("Total in %s: %.2f\n", selectedCurrency, finalPriceLocalCurrency); + } catch (IOException e) { e.printStackTrace(); } } -} +} \ No newline at end of file From 05379272cc39c7f612e8bfa8c268b8bf86a8bc22 Mon Sep 17 00:00:00 2001 From: Pramitha Jayasooriya <123730262+PramithaMJ@users.noreply.github.com> Date: Fri, 22 Sep 2023 11:09:40 +0000 Subject: [PATCH 2/2] EG_2020_3990 --- TravelCostCalculator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/TravelCostCalculator.java b/TravelCostCalculator.java index 2e01495..0a6f154 100644 --- a/TravelCostCalculator.java +++ b/TravelCostCalculator.java @@ -1,3 +1,4 @@ +//EG_2020_3990 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;