From 27519b8f7946f9474a33de54326764953ef8783f Mon Sep 17 00:00:00 2001 From: Eline Date: Fri, 30 Dec 2022 03:08:10 +0100 Subject: [PATCH] Singleton --- src/Nexus6.java | 82 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/src/Nexus6.java b/src/Nexus6.java index 853eb10..ff6ef73 100644 --- a/src/Nexus6.java +++ b/src/Nexus6.java @@ -1,4 +1,84 @@ +import tpe.exceptions.roboter.Robot; +import tpe.exceptions.roboter.exceptions.RobotException; -public class Nexus6 { +import java.util.Arrays; +import java.util.stream.Collectors; +public class Nexus6 implements Robot { + + private static Nexus6 instance = new Nexus6(); + + private int id; + private String name; + private boolean isPowerOn; + + private Nexus6() { + + } + + public static Nexus6 getInstance() { + if (instance == null) + instance = new Nexus6(); + return instance; + } + + + @Override + public int getId() { + return 19281982; + } + + @Override + public String getName() { + return "pris"; + } + + @Override + public void triggerPowerSwitch() { + if(isPowerOn==false){ + isPowerOn=true; + }else{ + isPowerOn=false; + } + } + + @Override + public boolean isPowerOn() { + return isPowerOn; + } + + @Override + public RobotException getLastException() { + return null; + } + + @Override + public String speak(int[] zahlen) throws RobotException { + + var sortiert = think(zahlen); + + return arrayFormatieren(sortiert); + } + + private String arrayFormatieren(int[] zahlen){ + var ergebnis = Arrays.stream(zahlen) + .boxed() + .map(String::valueOf) + .collect(Collectors.joining(",")); + return ergebnis; + } + @Override + public int[] think(int[] zahlen) throws RobotException { + int n = zahlen.length; + for (int i = 1; i < n; ++i) { + int key = zahlen[i]; + int j = i - 1; + while (j >= 0 && zahlen[j] < key) { + zahlen[j + 1] = zahlen[j]; + j = j - 1; + } + zahlen[j + 1] = key; + } + return zahlen; + } }