Commit ed1c3f69 authored by Børge Haugset's avatar Børge Haugset
Browse files

etter forelesning

parent a3beed61
package uke12.extra_listener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
public class FileObserver implements IObserver {
IObservable iObservable = new Observable();
public FileObserver(IObservable observable) {
this.iObservable = observable;
observable.addObserver(this);
}
@Override
public void update(IObservable observable, String name) {
try (PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter("Fil.txt", true)))) {
printWriter.print("Lagret ny aksje: "+name+" ("+observable.getStock(name)+")\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package uke12.extra_listener;
public interface IObservable {
public void updateStock(String name, double value);
public void addObserver(IObserver observer);
public void removeObserver(IObserver observer);
public double getStock(String name);
}
package uke12.extra_listener;
public interface IObserver {
public void update(IObservable observable, String name);
}
package uke12.extra_listener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class Observable implements IObservable, Runnable{
Collection<IObserver> observers = new ArrayList<>();
HashMap<String, Double> stocks = new HashMap<>();
Random random = new Random();
@Override
public double getStock(String name) {return stocks.get(name);}
public void updateStock(String name, double value) {
stocks.put(name, stocks.getOrDefault(name, 0.0) + value);
notifyListener(name);
}
private void notifyListener(String name) {
for (IObserver observer : observers) {
observer.update(this, name);
}
}
@Override
public void removeObserver(IObserver observer) {
this.observers.remove(observer);
}
@Override
public void addObserver(IObserver observer) {
this.observers.add(observer);
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(1000);
List<String> names = List.of("Equinoor", "DnBee", "Slemdal Skotthyllforening ASA");
double moneys = random.nextDouble(10)-5;
updateStock(names.get(random.nextInt(names.size())), moneys);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package uke12.extra_listener;
import java.text.DecimalFormat;
public class PrintObserver implements IObserver{
IObservable observable;
DecimalFormat df = new DecimalFormat("##.#");
public PrintObserver(IObservable observable) {
this.observable = observable;
observable.addObserver(this);
}
@Override
public void update(IObservable observable, String name) {
System.out.println(name+": "+df.format(observable.getStock(name)));
}
}
package uke12.listener_forelesning;
// Eksempelet er litt utvidet fra forelesning, og dekker interface av begge typer.
public class RunMe {
public static void main(String[] args) {
Observable observable = new Observable();
PrintObserver observer = new PrintObserver(observable);
FileObserver fileObserver = new FileObserver(observable);
observable.updateStock("Equinoor", 500);
observable.updateStock("Equinoor", 50);
observable.updateStock("Frosta SkottHyllLag", 50);
Thread thread = new Thread(observable);
thread.start();
System.out.println("Da venter vi!");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment