-
Notifications
You must be signed in to change notification settings - Fork 0
/
codsoft_problem_question3.java
71 lines (48 loc) · 2.24 KB
/
codsoft_problem_question3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;
public class codsoft_problem_question3 {
private Map<String,Double> exchange_Rates;
public codsoft_problem_question3(){
exchange_Rates= new HashMap<>();
exchange_Rates.put("USD",1.0); //base currency (US Dollar )
exchange_Rates.put("EUR", 0.86); //Euro
exchange_Rates.put("GBP", 0.74); //BRITISH found
exchange_Rates.put("JPY", 109.78); //Japanse Yen
exchange_Rates.put("CAD", 1.26); //Canadian Dollar
exchange_Rates.put("AUD", 1.35); //Australian Dollar
exchange_Rates.put("INR", 73.92); //Indaian Rupee
exchange_Rates.put("CNY", 6.44); //Chinese Yuen
}
public double convert_Currency(double amount,String from_Currency,String to_Currency)
{
if(exchange_Rates.containsKey(from_Currency) && exchange_Rates.containsKey(to_Currency))
{
double fromRate=exchange_Rates.get(from_Currency);
double toRate=exchange_Rates.get(to_Currency);
return amount*(toRate/fromRate);
}
else
{
System.out.println("Invalid currency codes.");
return -1.0;
}
}
public static void main(String[] args)
{
codsoft_problem_question3 convertor=new codsoft_problem_question3();
Scanner sc=new Scanner(System.in);
System.out.println("Currency Convertor");
System.out.println("Enter the amount");
double amount =sc.nextDouble();
System.out.println("Enter the source currency code (e.g. \nUSD \nEUR \nGBP \nJPY \nCAD \nAUD \nINR \nCNY");
String from_Currency= sc.next().toUpperCase();
System.out.println("Enter the target currency code (e.g.. \nUSD \nEUR \nGBP \nJPY \nCAD \nAUD \nINR \nCNY");
String to_Currency=sc.next().toUpperCase();
double converted_currencyAmount= convertor.convert_Currency(amount,from_Currency,to_Currency);
if(converted_currencyAmount!= -1.0)
{
System.out.println(amount +" " + from_Currency + " is equal to " + converted_currencyAmount + " " + to_Currency);
}
}
}