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

Omstrukturering og føerste versjon stocks

parent 2b77da36
package uke12.delegering.boss;
public class Boss implements Worker{
Worker secretary = new Secretary();
@Override
public String work() {
return secretary.work();
}
public static void main(String[] args) {
Boss boss = new Boss();
System.out.println(boss.work());
}
}
package uke12.delegering.boss;
public class Secretary implements Worker{
@Override
public String work() {
return "Dette er arbeidet sekretæren gjør.";
}
}
package uke12.delegering.boss;
public interface Worker {
public String work();
}
package uke12.delegering.boss.uteninterface;
public class Boss extends Worker {
Worker secretary = new Secretary();
@Override
public String work() {
return secretary.work();
}
public static void main(String[] args) {
Boss boss = new Boss();
System.out.println(boss.work());
}
}
package uke12.delegering.boss.uteninterface;
public class Secretary extends Worker {
@Override
public String work() {
return "Dette er arbeidet til sekretæren.";
}
}
package uke12.delegering.boss.uteninterface;
public class Worker {
public String work() {
return "standard jobb";
}
}
package uke12.delegering.kokk;
/*
* Grensesnittet som enhver kokk må oppfylle.
* Hovedkokken ChefdelaChef og underkokkene fyller alle denne rollen.
*/
public interface Chef {
public String makeMain();
public String makePotatoes();
public String makeSauce();
}
package uke12.delegering.kokk;
import java.util.Random;
/*
* Hovedkokken har ansatt to kokker til å gjøre arbeidet sitt. Det
* er jo det delegering handler om! I tillegg er det tilfeldig hvilken
* av de to kokkene som faktisk lager maten. Legg merke til at this.makeSauce()
* dermed knyttes direkte videre til underkokk.makeSauce(). Husk også at en kan
* gjøre litt mer, en trenger ikke ha en helt dum metode for å kalle det
* delegering. ChefdelaChef gjør noe - hen velger hvilken kokk som skal lage
* maten.
*/
public class ChefdelaChef implements Chef{
Chef kokk1 = new Kokk1();
Chef kokk2 = new Kokk2();
final Random random = new Random();
public String todaysMenu() {
String menu = "Dagens rett er ";
menu += this.makeMain();
menu += " med ";
menu += this.makePotatoes();
menu += " og ";
menu += this.makeSauce();
menu += ".";
return menu;
}
/*
* ChefDelaChef har to kokker. Og velger tilfeldig hvilken av de to som
* faktisk lager maten. Det kan gi opphav til spesielle kombinasjoner...
*/
@Override
public String makeMain() {
return random.nextBoolean()? kokk1.makeMain():kokk2.makeMain();
}
@Override
public String makeSauce() {
return random.nextBoolean()? kokk1.makeSauce():kokk2.makeSauce();
}
@Override
public String makePotatoes() {
return random.nextBoolean()? kokk1.makePotatoes():kokk2.makePotatoes();
}
public static void main(String[] args) {
ChefdelaChef dude = new ChefdelaChef();
System.out.println(dude.todaysMenu());
}
}
package uke12.delegering.kokk;
public class Kokk1 implements Chef {
@Override
public String makeSauce() {
return "honning";
}
@Override
public String makeMain() {
return "pannekaker";
}
@Override
public String makePotatoes() {
return "pommes frites";
}
}
package uke12.delegering.kokk;
public class Kokk2 implements Chef {
// Kanskje kokk2 kan sette bort arbeidet til enda en kokk, skjult for sjefen?
// Dobbeldelegering!
Chef kokk3 = new Kokk3();
// Denne kokken setter bare bort mekking av saus til kokk3.
@Override
public String makeSauce() {
return kokk3.makeSauce();
}
@Override
public String makeMain() {
return "entrecôte";
}
@Override
public String makePotatoes() {
return "hasselbackpoteter";
}
}
package uke12.delegering.kokk;
public class Kokk3 implements Chef {
// Kanskje kokk2 kan sette bort arbeidet til enda en kokk, skjult for sjefen?
@Override
public String makeSauce() {
return "ketchup";
}
@Override
public String makeMain() {
return "ostesmørbrød";
}
@Override
public String makePotatoes() {
return "kokte poteter";
}
}
package uke12.delegering.merging;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class MergeBruk {
public static void main(String[] args) {
List<String> liste1 = Arrays.asList("a","b","c","d","e","f","g");
List<String> liste2 = Arrays.asList("A","B","C","D","E");
Iterator<String> it = new MergingIterator(liste1.iterator(), liste2.iterator());
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
package uke12.delegering.merging;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MergingIterator implements Iterator<String>{
private Iterator<String> it1;
private Iterator<String> it2;
private boolean flippe;
/**
* @param it1
* @param it2
*/
public MergingIterator(Iterator<String> it1, Iterator<String> it2) {
super();
this.it1 = it1;
this.it2 = it2;
this.flippe = true;
}
@Override
public boolean hasNext() {
return it1.hasNext() || it2.hasNext();
}
@Override
public String next() {
if(! hasNext()){ throw new NoSuchElementException(); }
String result;
if(! it1.hasNext()){
result = it2.next();
}
else if(! it2.hasNext()){
result = it1.next();
}
else {
if(flippe){
result = it1.next();
} else{
result = it2.next();
}
flippe = !flippe;
}
return result;
}
}
package uke12.delegering.person;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Person {
List<Person> children = new ArrayList<>();
@Override
public String toString() {
return String.format("%s (%d)",name, age);
}
String name;
int age;
int getChildCount() { return children.size();}
Person getChild(int i) {return children.get(i);}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void addChild(Person child) {
children.add(child);
}
public static void main(String[] args) {
Person p = new Person("Børge", 46);
Person j = new Person("Jørn", 13);
Person h = new Person("Håvard", 11);
p.addChild(j);
p.addChild(h);
System.out.println(p.getChildCount());
System.out.println(p.getChild(1));
}
}
package uke12.delegering.settings;
public class DefaultingSettings implements ISettings {
private ISettings userSettings = new MapSettings();
private ISettings defaultSettings = new MapSettings();
public DefaultingSettings(ISettings defaultSettings) {
this.defaultSettings = defaultSettings;
}
@Override
public boolean hasSetting(final String s) {
return userSettings.hasSetting(s) || defaultSettings.hasSetting(s);
}
@Override
public Object getSetting(final String s) {
if (userSettings.hasSetting(s)) {
return userSettings.getSetting(s);
}
if (defaultSettings != null) {
return defaultSettings.getSetting(s);
}
return null;
}
@Override
public void updateSetting(final String s, final Object o) {
userSettings.updateSetting(s, o);
}
//
public static void main(final String[] args) {
// HUSK KLASSEDIAGRAM UNDER
ISettings installationSettings = new MapSettings();
ISettings d2 = new DefaultingSettings(installationSettings);
ISettings appSettings = new DefaultingSettings(d2);
installationSettings.updateSetting("theme", "light");
installationSettings.updateSetting("font", "times new roman");
installationSettings.updateSetting("fullscreen", "yes");
System.out.println("\nupdating installationSettings");
System.out.println("Font: "+appSettings.getSetting("font"));
System.out.println("Theme: "+appSettings.getSetting("theme"));
System.out.println("Fullscreen: "+appSettings.getSetting("fullscreen"));
System.out.println("\nupdating workspaceSettings");
d2.updateSetting("font", "default, comic sans");
System.out.println("Font: "+appSettings.getSetting("font"));
System.out.println("Theme: "+appSettings.getSetting("theme"));
System.out.println("Fullscreen: "+appSettings.getSetting("fullscreen"));
System.out.println("\nupdating projectSettings");
appSettings.updateSetting("theme", "dark");
System.out.println("Font: "+appSettings.getSetting("font"));
System.out.println("Theme: "+appSettings.getSetting("theme"));
System.out.println("Fullscreen: "+appSettings.getSetting("fullscreen"));
}
/*
* @startuml
* object "appSettings: DefaultingSettings" as appSettings
* object "projectSettings: MapSettings" as project
* object "map1: Map" as map1
* object "ds2: DefaultingSettings" as ds2
* object "workspaceSettings: MapSettings" as workspace
* object "map2: Map" as map2
* object "installationSettings: MapSettings" as installation
* object "map3: Map" as map3
* appSettings -down-> project: settings
* project -down-> map1: settings
* appSettings -down-> ds2: defaultSettings
* ds2 -down-> workspace: settings
* workspace -down-> map2: settings
* ds2 -down-> installation: defaultSettings
* installation -down-> map3: settings
* @enduml
*/
}
package uke12.delegering.settings;
public interface ISettings {
boolean hasSetting(String s);
Object getSetting(String s);
void updateSetting(String s, Object o);
}
package uke12.delegering.settings;
import java.util.HashMap;
import java.util.Map;
public class MapSettings implements ISettings {
private final Map<String, Object> settings = new HashMap<>();
@Override
public boolean hasSetting(final String s) {
return settings.containsKey(s);
}
@Override
public Object getSetting(final String s) {
return settings.get(s);
}
@Override
public void updateSetting(final String s, final Object o) {
settings.put(s, o);
}
public static void main(final String[] args) {
ISettings settings = new MapSettings();
settings.updateSetting("font", "Courier");
settings.updateSetting("errorColor", "Red");
System.out.println(settings.getSetting("font"));
System.out.println(settings.getSetting("errorColor"));
}
}
package uke12.listener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.DecimalFormat;
/**
* Klassen abonnerer på endringer i en StockObservable. Jeg kunne ha latt den ta inn en
* IObservable i konstruktøren, men da måtte jeg enten castet den til en SO, ELLER utvidet
* interfacet IObservable med get-metoden, for å kunne hente ut verdiene. Det er jo en mulighet,
* men her ville jeg holde interfacet smått.
*
* Det som skjer når den får inn beskjed om endringer er å sjekke om endringene gjelder "Equinoor".
* Hvis så, åpner den filen 'stocks.txt' som ligger i rota til prosjektet (scroll helt ned i explorer
* i VS Code). Deretter skriver den ut én ny linje med aksjens nye verdi. DecimalFormat bruker jeg
* for å vise en måte å formattere desimaltall.
*
*/
public class FileObserver implements IObserver {
StockObservable observable;
DecimalFormat df = new DecimalFormat("##.#");
public FileObserver(StockObservable observable) {
this.observable = observable;
observable.addListener(this);
}
@Override
public void update(IObservable subject, String what) {
if (what.equals("Equinoor")) {
// Denne ender opp i rota på prosjektet.
try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("stocks.txt", true)))) {
System.out.println();
pw.print(what + ": "+df.format(subject.getValue(what))+"\n");
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
}
}
}
package uke12.listener;
public interface IObservable {
public void addListener(IObserver listener);
public void removeListener(IObserver listener);
public void notifyListeners(String what);
public double getValue(String what);
}
package uke12.listener;
public interface IObserver {
public void update(IObservable subject, String what);
}
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