Singleton

master
Eline 2022-12-30 03:08:10 +01:00
parent 684854b525
commit 27519b8f79
1 changed files with 81 additions and 1 deletions

View File

@ -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;
}
} }