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
93 changes: 59 additions & 34 deletions TravelCostCalculator.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//EG/2020/4200
//Senapathy J.D
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -6,68 +8,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 Map<String, Double> hashMap1 = new HashMap<>();
static Map<String, Double> hashMap2 = new HashMap<>();
static Map<String, Double> hashMap3 = new HashMap<>();

static void l1(String file) throws IOException {


//Function to find hotel rates

static void hotelRates(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]));

while ((theReadingFile = reader.readLine()) != null) {
String[] p = theReadingFile.split(",");
hashMap1.put(p[0].toUpperCase(), Double.parseDouble(p[1]));
}
}

static void l2(String file) throws IOException {
//Function to find exchange rates

static void exchangeRates(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 theReadingFile;
while ((theReadingFile = reader.readLine()) != null) {
String[] p = theReadingFile.split(",");
hashMap2.put(p[0].toUpperCase(), Double.parseDouble(p[1]));
}
}

static void l3(String file) throws IOException {
//Function to find flight costs

static void flightCosts(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 theReadingFile;
while ((theReadingFile = reader.readLine()) != null) {
String[] p = theReadingFile.split(",");
hashMap3.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");

//Data sets of hotel rates, exchange rates and flight costs
hotelRates("data/hotel_rates.csv");
exchangeRates("data/exchange_rates.csv");
flightCosts("data/flight_costs.csv");

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

//Enter destination
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);
//Getting cost of flight
double costOfFlight = hashMap3.getOrDefault(destination, 0.0);
//Getting cost of hotel
double costOfHotel = hashMap1.getOrDefault(destination, 0.0);

//Enter duration of stay
System.out.print("Enter your stay duration in days: ");
int stay_duration = Integer.parseInt(reader.readLine());
hotel_cost *= stay_duration;
int durationOfStay = Integer.parseInt(reader.readLine());

costOfHotel *= durationOfStay;

//Calculating the total cost in USD
double totalCostInUSD = costOfFlight + costOfHotel;

System.out.printf("Flight cost: USD %.2f\n", costOfFlight);

double total_cost_usd = flight_cost + hotel_cost;
System.out.printf("Hotel cost (%d days): USD %.2f\n", durationOfStay, costOfHotel);

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("Total: USD %.2f\n", totalCostInUSD);

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 = hashMap2.keySet().toArray(new String[0]);

double final_price_local_currency = total_cost_usd * b.get(selected_currency);
//Enter preferred currency
System.out.print("Select your currency for final price estimation(" + String.join(", ", availableCurrencies) + "): ");
String selectedCurrency = reader.readLine();

//Calculating the final price in the preferred currency
double finalPriceInlocalCurrency = totalCostInUSD * hashMap2.get(selectedCurrency);

System.out.printf("Total in %s: %.2f\n", selected_currency, final_price_local_currency);
//Outputing the final cost
System.out.printf("Total in %s: %.2f\n", selectedCurrency, finalPriceInLocalCurrency);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down