Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 62 additions & 38 deletions TravelCostCalculator.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//Rusham
//EG_20_4170

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -6,70 +9,91 @@
import java.util.Map;

public class TravelCostCalculator {
static Map<String, Double> a = new HashMap<>();
static Map<String, Double> b = new HashMap<>();
static Map<String, Double> c = 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]));
private static final Map<String, Double> hotelCharge = new HashMap<>();
private static final Map<String, Double> currencyConversion = new HashMap<>();
private static final Map<String, Double> ticketPrice = new HashMap<>();

//Function for loading Hotel charges
private static void retrievehotelCharge(String file) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
hotelCharge.put(parts[0].toUpperCase(), Double.parseDouble(parts[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]));
//Function for loading currency conversion
private static void retrievecurrencyConversion(String file) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
currencyConversion.put(parts[0].toUpperCase(), Double.parseDouble(parts[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]));
//Function for loading Ticket price
private static void retrieveticketPrice(String file) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
ticketPrice.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");
retrieveHotelRates("data/hotel_rates.csv");
retrievecurrencyConversion("data/exchange_rates.csv");
retrieveticketPrice("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 = ticketPrice.getOrDefault(destination, 0.0);
double hotelCost = hotelRates.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 = currencyConversion.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 * currencyConversion.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();
}
}
}

The changes of the code;

1. Added some comments to understand the code well
2. Formatted the code by adding indents and spacing.
3. The variables `a`, `b`, and `c` were changed to`hotelCharge`, `currencyConversion`, and `ticketPrice`
to make the variable and the function more meaningful and improve the readability of the code.
Moreover the variable and the functions were named according to the java naming conventions to improve
readability.
4. Used the try operation to make sure there are no errors when closing.
5. The funtion for retrieveing the datas are wriiten on seperate functions to clean the code and avoid
repetitions(In this case there are no repetitions but it can be used in case of an update)