package patterns.observable;

import java.util.Scanner;

public class HighscoreListProgram implements HighscoreListListener {

  private HighscoreList list;

  /** oppretter en ny HighscoreList og registrerer seg selv (altså HighscoreListProgram-instansen) som lytter */
  public void init() {
    this.list = new HighscoreList(4);

    this.list.addHighscoreListListener(this);
  }

  /** leser inn tall (resultater) fra konsollet og legger dem til i listen */
  public void run() {
    try(Scanner scanner = new Scanner(System.in)){
      var i = scanner.nextInt();
      // this.list.addResult(i);
      System.out.println(i);
    }
  }

 /** observerer endringer i HighscoreList-instansen og skriver ut posisjonsargumentet, samt selve listen, til konsollen. */
  public void listChanged(HighscoreList list, int i) {
    System.out.println(list.toString() + " " + i);
  }

  public static void main(String[] args) {
    HighscoreListProgram prog = new HighscoreListProgram();
    prog.init();
    while (true) {
      prog.run();
    }
  }
}
