verwendung von Robotinstanzen vermieden, Anwendungsmenü aktualisiert

main
Philipp3107 2023-01-10 11:47:10 +01:00
parent 0f4c343fce
commit 7d8237bf76
61 changed files with 96 additions and 62 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

Binary file not shown.

View File

@ -7,7 +7,6 @@ import utility.robot_exceptions.robotExceptions;
public class C3PO extends Robot {
public C3PO(int id, String name){
super(id, name, RobotType.C3PO);
}
/**
* @param input from the user

View File

@ -41,7 +41,9 @@ public class Factory implements Serializable {
r = new R2D2(r2d2ID++, name);
} else if(type == RobotType.C3PO) {
r = new C3PO(c3poID++, name);
} else {
}else if(type == RobotType.Nexus6){
r = new Nexus6("Pris");
}else {
return false;
}

View File

@ -99,9 +99,11 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
*/
public String output(int[] input, String delemiter)throws RobotException{
if(checkArray(input)) {
return Arrays.stream(input)
.mapToObj(Integer::toString)
.collect(Collectors.joining(delemiter + " "));
final String[] msg = {""};
Arrays.stream(input).forEach(s -> {
msg[0] += s + delemiter + " ";
});
return msg[0];
}else{
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
this.exceptions = new ExceptionStorage(robotexception);

View File

@ -4,6 +4,7 @@ import domain.Factory;
import domain.Robot;
import domain.RobotType;
import infrastructure.Persistenz;
import utility.robot_exceptions.RobotException;
public class FactorySystem {
private Factory factory;
@ -22,6 +23,7 @@ public class FactorySystem {
}
}else{
this.factory = new Factory();
boolean pris = buildNewRobot("Pris", RobotType.Nexus6);
}
}
@ -36,11 +38,7 @@ public class FactorySystem {
public boolean buildNewRobot(String name, RobotType type) {
boolean check = factory.buildNewRobot(name, type);
if(check) {
try {
Persistenz.saveFactoryData(factory, factoryName);
}catch(Exception e){
System.out.println(e.getCause());
}
saveFactroyData();
}
return check;
}
@ -53,4 +51,37 @@ public class FactorySystem {
return factory.getRobotOfList(id);
}
public String letRobotSpeak(int[] input, int id){
String output = "";
try{
output = factory.getRobotOfList(id).speak(input);
}catch(RobotException re){
saveFactroyData();
}
return output;
}
public int[] letRobotThink(int[] input, int id){
int[] unsorted = new int[input.length];
try{
unsorted = factory.getRobotOfList(id).think(input);
}catch(RobotException re){
saveFactroyData();
}
return unsorted;
}
public RobotException getExceptions(int id){
return factory.getRobotOfList(id).getLastException();
}
public void saveFactroyData(){
try {
Persistenz.saveFactoryData(factory, factoryName);
}catch(Exception e){
System.out.println(e.getCause());
}
}
}

View File

@ -125,43 +125,54 @@ public class UI {
mainloop:
while(true) {
System.out.println();
System.out.println("_______________________________");
System.out.println("Sie haben folgende optionen: ");
System.out.println("-1- --- Roboter " + (!fs.searchForRobot(idInput).isPowerOn() ? "Einschalten " : "Ausschalten " ) + " ---");
System.out.println("-2- -- Sortieren einer Liste --");
System.out.println("-3- ----- Namen ausgeben ------");
System.out.println("-4- ------ ID ausgeben --------");
System.out.println("-5- ------ Typ ausgeben -------");
System.out.println("-6- ---------- Exit -----------");
System.out.println("____________________________________");
System.out.println("--- Sie haben folgende optionen: ---");
System.out.println("-1- ----- Roboter " + (!fs.searchForRobot(idInput).isPowerOn() ? "Einschalten " : "Ausschalten " ) + " -----");
System.out.println("-2- ---- Sortieren einer Liste -----");
System.out.println("-3- ------- Namen ausgeben ---------");
System.out.println("-4- --------- ID ausgeben ----------");
System.out.println("-5- -------- Typ ausgeben ----------");
System.out.println("-6- - letzte Fehlermeldung anzeigen -");
System.out.println("-7- ------------- Exit --------------");
System.out.print(" > ");
//User options
try {
int input = Integer.parseInt(sc.nextLine());
switch (input) {
case 1:
case 1 -> {
fs.searchForRobot(idInput).triggerPowerSwitch();
System.out.println("Der Roboter hat seinen Zustand gewechselt");
break;
case 2:
}
case 2 -> {
int[] unsortedList = null;
while(unsortedList == null) {
while (unsortedList == null) {
try {
unsortedList = fs.searchForRobot(idInput).think(Arrays.stream(sc.nextLine().split(", ")).mapToInt(Integer::parseInt).toArray());
System.out.println(fs.searchForRobot(idInput).speak(unsortedList));
} catch (NumberFormatException | RobotException e) {
if(e.getClass() == RobotException.class){
System.out.println(((RobotException) e).getMessage());
break;
}else{
unsortedList = fs.letRobotThink(Arrays.stream(sc.nextLine().split(", ")).mapToInt(Integer::parseInt).toArray(), idInput);
System.out.println(fs.letRobotSpeak(unsortedList, idInput));
} catch (NumberFormatException nfe) {
System.out.println("Falsches Format versuch es erneut");
break;
}
}
}
break;
case 6: break mainloop;
default:
System.out.println("Keine valide Option"); break;
case 3 -> System.out.println("Mein Name lautet " + fs.searchForRobot(idInput).getName() + ".");
case 4 -> System.out.println("Meine ID ist " + fs.searchForRobot(idInput).getId() + ".");
case 5 -> System.out.println("Meine Typ ist " + fs.searchForRobot(idInput).getType().toString() + ".");
case 6 -> {
try{
String message = fs.getExceptions(idInput).getMessage();
System.out.println("Letze Fehlermeldung: ");
System.out.println(message);
}catch(NullPointerException npe){
System.out.println("Bisher keine Fehlermeldung vorhanden.");
}
}
case 7 -> {
break mainloop;
}
default -> System.out.println("Keine valide Option");
}
}catch(NumberFormatException nfe) {
System.out.println("Kein valider input");

View File

@ -1,25 +1,12 @@
package utility.robot_exceptions;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ExceptionStorage implements Serializable {
private RobotException message;
private LocalDateTime date;
public ExceptionStorage(RobotException message){
this.message = message;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
this.date = now;
}
/**
* @param message of the exception
*/
public void saveLatestErrorMessage(RobotException message){
this.message = message;
this.date = LocalDateTime.now();
}
public RobotException getLastErrorMessage(){

View File

@ -1,5 +1,8 @@
package utility.robot_exceptions;
import facade.FactorySystem;
import infrastructure.Persistenz;
public class RobotException extends Exception{
robotExceptions currentType;
String name;

Binary file not shown.

View File

@ -40,7 +40,7 @@ class C3POTest {
@Test
void speak(){
String solution = "12; 2; 4; 5; 12; 2; 4; 7; 56; 433; 23";
String solution = "12; 2; 4; 5; 12; 2; 4; 7; 56; 433; 23; ";
int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23};
String array = "";
try{
@ -53,7 +53,7 @@ class C3POTest {
@Test
void C3POSpeakTestStandard() throws RobotException {
assertEquals("1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12", c3po.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
assertEquals("1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; ", c3po.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
}
@Test
@ -67,17 +67,17 @@ class C3POTest {
@Test
void C3POSpeakTestZeros() throws RobotException {
assertEquals("0; 0; 0; 0", c3po.speak(new int[4]));
assertEquals("0; 0; 0; 0; ", c3po.speak(new int[4]));
}
@Test
void C3POSpeakTestOneElement() throws RobotException {
assertEquals("1", c3po.speak(new int[]{1}));
assertEquals("1; ", c3po.speak(new int[]{1}));
}
@Test
void C3POSpeakTestUnitElements() throws RobotException {
assertEquals("-1; 0; 1", c3po.speak(new int[]{-1, 0, 1}));
assertEquals("-1; 0; 1; ", c3po.speak(new int[]{-1, 0, 1}));
}
@Test

View File

@ -1,7 +1,6 @@
package tests;
import domain.Nexus6;
import domain.R2D2;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import utility.robot_exceptions.RobotException;

View File

@ -38,7 +38,7 @@ class R2D2Test {
@Test
void R2D2SpeakTestStandard() throws RobotException {
assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
}
@Test
@ -52,17 +52,17 @@ class R2D2Test {
@Test
void R2D2SpeakTestZeros() throws RobotException {
assertEquals("0, 0, 0, 0", r2d2.speak(new int[4]));
assertEquals("0, 0, 0, 0, ", r2d2.speak(new int[4]));
}
@Test
void R2D2SpeakTestOneElement() throws RobotException {
assertEquals("1", r2d2.speak(new int[]{1}));
assertEquals("1, ", r2d2.speak(new int[]{1}));
}
@Test
void R2D2SpeakTestUnitElements() throws RobotException {
assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1}));
assertEquals("-1, 0, 1, ", r2d2.speak(new int[]{-1, 0, 1}));
}
@Test
@ -76,7 +76,7 @@ class R2D2Test {
@Test
void speak(){
String solution = "12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23";
String solution = "12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23, ";
int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23};
String array = "";
try{