-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrencyProgram.java
More file actions
171 lines (140 loc) · 6.16 KB
/
CurrencyProgram.java
File metadata and controls
171 lines (140 loc) · 6.16 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.ssc.p;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class CurrencyProgram {
private static Scanner scan = new Scanner(System.in);
private static ArrayList<String> currencyName = new ArrayList<>();
static{
currencyName.add("USD");
currencyName.add("INR");
currencyName.add("EUR");
currencyName.add("CAD");
currencyName.add("HKD");
}
public static void main(String[] args) throws IOException, JSONException {
Scanner scan = new Scanner(System.in);
System.out.println("\\WELL COME TO THE CURRENCY CONVERTER\\");
System.out.println("-------------------------------------");
display();
}
public static void display() throws JSONException, IOException {
System.out.println("CHOOSE ONE OF THESE OPTION");
System.out.println("1 for convert");
System.out.println("2 add your favorite one");
System.out.println("3 for view favorite one");
System.out.println("4 for updating the favorite one");
System.out.println("5 exit\n");
System.out.print("Enter the option in number:");
int num = scan.nextInt();
method(num);
}
public static void method(int num) throws IOException, JSONException {
switch (num){
case 1:convertCurrency(); break;
case 2:addFavorite(); break;
case 3: viewCurrency(); break;
case 4:updateCurrency();break;
case 5 :exit(); break;
default:
System.out.println("please enter the correct option");
display();break;
}
}
static void exit() {
System.out.println("Thank you!");
System.out.println("The task has completed");
}
public static void convertCurrency() throws IOException, JSONException {
String convertFrom , convertTo;
double amount;
System.out.println(CurrencyProgram.currencyName);
System.out.print("currency converting From:");
convertFrom = scan.next().toUpperCase();
// convertFrom = currencyName.get(scan.nextInt());
System.out.println(CurrencyProgram.currencyName);
System.out.print("currency converting To:");
convertTo = scan.next().toUpperCase();
// convertTo = currencyName.get(scan.nextInt());
System.out.println("Amount you want to convert:");
amount = scan.nextDouble();
sendHTTPGetRequest(convertFrom, convertTo, amount);
}
public static void sendHTTPGetRequest(String convertFrom, String convertTo, double amount) throws IOException, JSONException {
// final String ACCESS_KEY = "9fdf399ce61a5f4ef04f5f6b5347ec7d";
final String GET_URL = "https://v6.exchangerate-api.com/v6/b257260aec0f09d5c3aabb9c/latest/USD";
URL url = new URL(GET_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuilder responce = new StringBuilder();
while ((inputLine = in.readLine())!=null){
responce.append(inputLine);
}in.close();
// System.out.println(response);
String jsonresponce = responce.toString();
JSONObject jsonObject = new JSONObject(jsonresponce);
// Extract exchange rate for the target currency from the JSON response
double exchangeRate = jsonObject.getJSONObject("conversion_rates").getDouble(convertFrom);
System.out.println(jsonObject.getJSONObject("conversion_rates"));
System.out.println(exchangeRate);//debbuging
System.out.println();
double convertedRate = amount/exchangeRate;
System.out.printf(amount +" "+ convertFrom + " = " + convertedRate +" "+ convertTo);
System.out.println();
CurrencyProgram.display();
}else {
System.out.println("don't get");
}
}
public static void addFavorite() throws JSONException, IOException {
System.out.print("Enter the currency name:");
String newCurrencyName = scan.next().toUpperCase();
if(!currencyName.contains(newCurrencyName)){
currencyName.add(newCurrencyName);
System.out.println("currency " + newCurrencyName +" is added");
}else {
System.out.println("currency is already exits");
}
CurrencyProgram.display();
}
public static void viewCurrency() throws JSONException, IOException {
if(currencyName.isEmpty()){
System.out.println("no currency is available");
}else{
System.out.print("available currency are:");
for (String currency:currencyName){
System.out.println(currency.toUpperCase());
}
}
System.out.println("hello i am in viewCurrency");
CurrencyProgram.display();
}
public static void updateCurrency() throws JSONException, IOException {
if (currencyName.isEmpty()){
System.out.println("no currency is added yet");
}else{
System.out.println("Enter the currency to update");
String oldCurrency = scan.next().toUpperCase();
if(currencyName.contains(oldCurrency)){
System.out.print("Enter the new currency:");
String newCurrency = scan.next().toUpperCase();
int index = currencyName.indexOf(oldCurrency);
currencyName.set(index, newCurrency);
System.out.println(oldCurrency+" updated to "+newCurrency );
}else{
System.out.println("currency does not exist");
}
}
CurrencyProgram.display();
}
}