📄 converter.java
字号:
import java.io.*;
import javagently.*;
import java.util.*;
import myutilities.*;
class Converter {
/* The Converter Program by J M Bishop Dec 1998
* --------------------- updated June 2000
* Keeps the exchange rates from one currency into
* many others and enables currency purchases to be
* estimated.
*
* Illustrates the use of hash tables.
*/
Display display = new Display ("Currency Converter");
Hashtable table = new Hashtable();
Converter () throws IOException {
initialise();
readIn();
echo();
transactions();
}
// read in each line of data and store in
// the hash table with country as key
void initialise () {
display.println("Currency Converter\n"+
"==================");
display.prompt("Rates file", "rates.dat");
display.ready("Press ready when the file is ok");
}
void readIn() throws IOException {
Rates rate;
String filename = display.getString("Rates file");
Stream fin = Filer.open(filename);
try {
for (int i = 0; ; i++) {
rate = new Rates();
rate.setRate(fin);
table.put(rate.country, rate);
}
}
catch (EOFException e) {
display.println("Data read in and stored\n");
}
}
void echo() {
display.println("In the table");
int i = 1;
for (Enumeration e = table.keys(); e.hasMoreElements(); i++) {
String country = (String)e.nextElement();
display.println(country);
}
}
void transactions () {
display.prompt("Country", "American");
display.prompt("Amount", 1000);
String country;
double amount;
String c;
Rates r;
while (true) {
display.println("Scroll up to see countries");
display.ready("Enter country and amount and press ready");
c = display.getString("Country");
if (table.containsKey(c)) {
amount = display.getDouble("Amount");
r = (Rates) table.get(c);
display.println( Stream.format(amount,6,2) +" ZAR in "+c+" "
+r.currency+" is "+
Stream.format(amount * r.conversion,6,2)+"\n");
}
else {
display.println("Sorry, country "+c+" not in table\n");
}
} // while
} // transactions
public static void main(String[] args) throws IOException {
Converter data = new Converter ();
}
} // class Converter
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -