Question: Correct the errors // Source code file CurrencyMonitor.java import java.io.IOException; import java.net.MalformedURLException; import java.util.Observable; import java.util.concurrent.TimeUnit; public class CurrencyMonitor implements Observable { private String[ ]
Correct the errors // Source code file CurrencyMonitor.java
import java.io.IOException; import java.net.MalformedURLException; import java.util.Observable; import java.util.concurrent.TimeUnit; public class CurrencyMonitor implements 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(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.FileNotFoundException; import java.io.PrintWriter; import java.util.Calendar; 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 Calendar( ); _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]); sw.print(String.Format("%7.5f ", exchangeRate)); } _pw.println( ); _pw.flush( ); } } public class Main { public static void main(String[] args) throws InterruptedException { Logger logger = new Logger('rates.txt'); CurrencyMonitor monitor = new CurrencyMonitor; monitor.addObserver(logger); monitor.checkCurrencies( ); } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
