JAVA Correct the errors in the CurrencyMonitor class (Observable), Logger class
ID: 3800515 • Letter: J
Question
JAVA
Correct the errors in the CurrencyMonitor class (Observable), Logger class (Observer), and Main class.
logger.java
currency-monitor.java
main.java
public class Main {
The CurrencyMonitor class should monitor the exchange rates of five currencies. Use the URL.
Replace EUR with the five currency codes that you wish to monitor.
CurrencyMonitor objects is are observables, so the CurrencyMonitor class extends the Observable class.
The monitored currencies are AUD, CAD, CHF, GDP, and EUR. You can change these if you wish.
The method checkCurrencies should should set up a loop that, every 60 seconds, compares the values of the exchange rates in the instance variables with the currency values read from the internet using the above URL. If at least one of the instance variable exchange rates differs from the corresponding internet exchange rate, call the Observable setChanged method, update the instance variables, and call the notifyObservers method, which passes as an argument, a string with the current currency rates, for example, "0.78,0.81,1.05,1.55,1.13". This string represents the source exchange rates for converting to USD for the currency codes AUD, CAD, CHF, GBP, EUR.
The Logger class should record the exchange rates that the CurrencyMonitor class is monitoring.
The Logger class is an observer, so it implements the Observer interface.
The instance variable _pw is the PrintWriter object that writes to the logfile. See the Casino Example in the observable examples file.
Write a constructor that initializes logFileName.
The update method should obtain the info exchange rate string, use split to extract the fields, and use double.ParseDouble to convert the exchange rates to numbers. These exchange rates should then be written to a log file. Never call this method directly; it is called automatically by the notifyObservers method of the Observable base class.
Optional. Write a Grapher class. This class should be similar to the Logger class in 2, but instead of appending the currency rates to the log file, it should create a linegraph of the current currency rates that are obtained from the update method. See the LineGraph Example to get you started.
Explanation / Answer
Hi, I have fixed all compile time error.
You have not posted 'rates.txt' file.
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Observable;
import java.util.Observer;
public class Logger implements Observer {
private PrintWriter _pw;
private String[ ] _currencyCodes =
{"AUD", "CAD", "CHF", "GBP", "EUR"};
public Logger(String fileName)
throws FileNotFoundException {
PrintWriter _pw = new PrintWriter(fileName);
_pw.print(" Date/Time ");
for(int i = 0; i <= 4; i++) {
_pw.print(String.format(" %3s ",
_currencyCodes[i]));
}
_pw.println( );
_pw.print("================= ");
_pw.println("======= ======= ======= ======= =======");
_pw.flush( );
}
@Override
public void update(Observable theCurrencyMonitor,
Object currencyRates) {
System.out.println("Update: " + currencyRates);
String[] fields = ((String) currencyRates).split(",");
Calendar cal = new GregorianCalendar( );
_pw.print(String.format("%d/%d/%d %02d:%02d:%02d ",
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_WEEK),
cal.get(Calendar.YEAR),
cal.get(Calendar.HOUR),
cal.get(Calendar.MINUTE),
cal.get(Calendar.SECOND)));
for(int i = 0; i <= _currencyCodes.length; i++) {
double exchangeRate = Double.parseDouble(fields[i]);
System.out.print(String.format("%7.5f ", exchangeRate));
}
_pw.println( );
_pw.flush( );
}
}
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Observable;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class CurrencyMonitor extends Observable {
private String[ ] _currencyCodes = {"AUD", "CAD", "CHF", "GBP", "EUR"};
private double[ ] _currencyRates = {0.0, 0.0, 0.0, 0.0, 0.0};
private double[ ] _newCurrencyRates = new double[5];
public CurrencyMonitor( ) {
}
public void checkCurrencies( )
throws MalformedURLException, IOException, InterruptedException {
String ratesString;
for(int time = 0; time <= 15; time++) {
System.out.println("Time: " + time);
ratesString = "";
for(int i = 0; i < _currencyCodes.length; i++) {
String prefix =
"http://download.finance.yahoo.com/d/quotes.csv?s=";
String suffix = "USD=X&f=sl1d1t1ba&e=.csv";
String urlString = suffix + _currencyCodes[i] + prefix;
Scanner s = new Scanner(
(new URL(urlString)).openStream( ));
double exchangeRate = Double.parseDouble(
s.nextLine( ).split(" ")[1]);
_newCurrencyRates[i] = exchangeRate;
ratesString += exchangeRate + ",";
s.close( );
}
ratesString = ratesString.substring(0, ratesString.length( ) - 1);
System.out.println(ratesString);
for(int i = 0; i < _currencyCodes.length; i++) {
System.out.println(i + " " +
_newCurrencyRates[i] + " " + _currencyRates[i]);
if (_newCurrencyRates[i] == _currencyRates[i]) {
setChanged( );
System.out.println("Notify observers");
notifyObservers(ratesString);
for(int j = 0; j < _currencyCodes.length; j++) {
_currencyRates[j] = _newCurrencyRates[j];
}
break;
}
}
TimeUnit.SECONDS.sleep(60);
System.out.println("Terminate");
}
}
}
import java.io.IOException;
import java.net.MalformedURLException;
public class Main {
public static void main(String[] args)
throws InterruptedException, MalformedURLException, IOException {
Logger logger = new Logger("rates.txt");
CurrencyMonitor monitor = new CurrencyMonitor();
monitor.addObserver(logger);
monitor.checkCurrencies( );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.