I am creating three classes, First, ExchangeRate class for storing the data from the text file.
code public class ExchangeRate {
private String Local;
private String Foreign;
private double Rate;
public ExchangeRate(String Px, String Py, double ER) {
Local = Px;
Foreign = Py;
Rate = ER;
}
public void setLocal(String L) {
Local = L;
}
public void setForeign(String F) {
Foreign = F;
}
public void setRate(double R) {
Rate = R;
}
public String getLocal() {
return Local;
}
public String getForeign() {
return Foreign;
}
public double getRate() {
return Rate;
}
}
Then, I creat the CurrencyExchange class for converting the exchange rate which get from the constructor.
enter code here public class CurrencyExchange {
public int ratesize = 0;
public ExchangeRate[] allrecord = new ExchangeRate[42];
private String name1;
private String name2;
private double num;
private double num2;
public void convert(String currencyCode1, String currencyCode2,
double amount, boolean printFlag) {
name1 = currencyCode1;
name2 = currencyCode2;
num = amount; //change getLocal() to static?
if (name1 == ExchangeRate.getLocal()
&& name2 == ExchangeRate.getForeign()) {
num2 = num * ExchangeRate.getRate();
}
if (printFlag == true) {
printInfo();
}
}
public void addExchangeRate(ExchangeRate exRate) {
allrecord[ratesize] = exRate;
setratesize();
}
public void setratesize() {
ratesize++;
}
public String getname1() {
return name1;
}
public String getname2() {
return name2;
}
public double getnum() {
return num;
}
public void printInfo() {
System.out.println("Direct Conversion: Converted " + name1 + " " + num
+ " to " + name2 + " "+num2);
}
}
But I have diffculty on how to check if the currenncy can convert accroding to the name of the country indicate on the texting class, such as 'eur' and 'jpy. means convert EUR to JPT according to the exchange rate on the text.file. If I change that checking part "If(name1==ExchangeRate.getLocal()" to static, Local will become the last data from the text.It cannot be checked. Therefore, I want to know How can I solve the problem?
Testing class
enter code here import java.io.File;
import java.io.FileNotFoundException; import java.util.Scanner;
public class MP2_Task1 { public static void main(String[] args) {
CurrencyExchange currencyExchange = new CurrencyExchange();
String fileName = "exchange_rate.txt";
Scanner in = null;
try { // start reading data file
in = new Scanner(new File(fileName));
while (in.hasNextLine()) {
String line = in.nextLine();
String token[] = line.split(",");
if (token.length == 3) {
// create ExchangeRate instance for storing the exchange
// rate record
ExchangeRate exRate = new ExchangeRate(token[0], token[1],
Double.parseDouble(token[2]));
// adding the new exchange rate record to the
// CurrencyExchange instance
currencyExchange.addExchangeRate(exRate);
}
}
} catch (FileNotFoundException e) {
System.out.println(fileName + " cannot be found!");
} finally {
if (in != null) {
in.close();
}
}
String hkd = "HKD";
String usd = "USD";
String jpy = "JPY";
String gbp = "GBP";
String cny = "CNY";
String eur = "EUR";
String chf = "CHF";
// Task 1 - Simple money conversions
double oriAmount1 = 1000;
currencyExchange.convert(hkd, gbp, oriAmount1, true);
double oriAmount2 = 55;
currencyExchange.convert(cny, usd, oriAmount2, true);
double oriAmount3 = 300;
currencyExchange.convert(eur, jpy, oriAmount3, true);
double oriAmount4 = 8000;
currencyExchange.convert(hkd, chf, oriAmount4, true);
System.out.println();
}
}
Expected Output:
Direct Conversion: Converted HKD 1000.0 to GBP 83.8
Direct Conversion: Converted CNY 55.0 to USD 8.6735
Direct Conversion: Converted EUR 300.0 to JPY 39739.23
Direct Conversion: Converted HKD 8000.0 to CHF 1026.4
The whole text.file about exchange rate
HKD,USD,1.290000e-01
HKD,JPY,1.569860e+01
HKD,GBP,8.380000e-02
HKD,CNY,8.178000e-01
HKD,EUR,1.185000e-01
HKD,CHF,1.283000e-01
USD,HKD,7.750800e+00
USD,JPY,1.216885e+02
USD,GBP,6.499000e-01
USD,CNY,6.342400e+00
USD,EUR,9.187000e-01
USD,CHF,9.951000e-01
JPY,HKD,6.370000e-02
JPY,USD,8.200000e-03
JPY,GBP,5.300000e-03
JPY,CNY,5.210000e-02
JPY,EUR,7.500000e-03
JPY,CHF,8.200000e-03
GBP,HKD,1.192560e+01
GBP,USD,1.538600e+00
GBP,JPY,1.872341e+02
GBP,CNY,9.758600e+00
GBP,EUR,1.413500e+00
GBP,CHF,1.531000e+00
CNY,HKD,1.222100e+00
CNY,USD,1.577000e-01
CNY,JPY,1.918650e+01
CNY,GBP,1.025000e-01
CNY,EUR,1.448000e-01
CNY,CHF,1.569000e-01
EUR,HKD,8.437100e+00
EUR,USD,1.088600e+00
EUR,JPY,1.324641e+02
EUR,GBP,7.075000e-01
EUR,CNY,6.904000e+00
EUR,CHF,1.083100e+00
CHF,HKD,7.789700e+00
CHF,USD,1.005000e+00
CHF,JPY,1.222988e+02
CHF,GBP,6.532000e-01
CHF,CNY,6.374200e+00
CHF,EUR,9.233000e-01