updated Exceptions with Enum of errormessages and Exception Storage. Also added a new Exception *ArrayEmptyException* and an Exception Storage to get the last Exception that occured.

main
Philipp3107 2022-12-21 16:44:49 +01:00
parent de13ede97d
commit d7a505607d
24 changed files with 297 additions and 94 deletions

View File

@ -5,21 +5,49 @@ public class Main {
public static void main(String[] args) {
int[] input = {42,6,5,4,3,2,1};
int[] input = {42,6,5,4,3,43,1};
int[] input2 = input;
C3PO Herbert = new C3PO(1, "Herbert");
R2D2 Herb = new R2D2(0, "Herb");
int[] input3 = {};
//Herbert.triggerPowerSwitch();
Herb.triggerPowerSwitch();
// Herb.triggerPowerSwitch();
// try{
// String sorted = Herb.speak(input);
// System.out.println(sorted);
// } catch (RobotException re) {
// System.out.println(re);
// }
try{
String array = Herb.speak(input);
System.out.println(array);
} catch (RobotException e) {
int[] sorted = Herb.think(input);
for(int i = 0; i < sorted.length; i++){
System.out.print(" " + sorted[i]);
}
}catch(RobotException re){
System.out.println(re);
}
String re = Herb.getLastException().toString();
System.out.println(re);
Herb.triggerPowerSwitch();
try{
int[] sorted = Herb.think(input);
for(int i = 0; i < sorted.length; i++){
System.out.print(" " + sorted[i]);
}
}catch(RobotException e){
System.out.println(e);
}
re = Herb.getLastException().toString();
System.out.println(re);
//System.out.println("Was neues ausgeben");
//just some testing

View File

@ -1,8 +1,7 @@
package domain;
import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException;
import robot.exceptions.RobotMagicValueException;
import robot.exceptions.robotExceptions;
import java.util.Arrays;
import java.util.stream.Collectors;
@ -18,26 +17,46 @@ public class C3PO extends RobotBasics {
.mapToObj(Integer::toString)
.collect(Collectors.joining("; "));
}else{
throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
throw new RobotException(robotExceptions.MAGICVALUE, getName());
// throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
}
}
@Override
public String speak(int[] zahlen) throws RobotException {
//Insertionsort
public String speak(int[] input) throws RobotException {
if(isPowerOn()){
try{
return ausgabe(zahlen);
return ausgabe(input);
}catch(RobotException re){
return re.toString();
}
}else{
throw new RobotIllegalStateException(getName() + " is turned off.");
throw new RobotException(robotExceptions.ILLEGALSTATE, getName());
//throw new RobotIllegalStateException(getName() + " is turned off.");
}
}
public int[] sorting(int[] input) throws RobotException{
if(input.length != 0 ){
if(checkArray(input)){
//sort
}else{
throw new RobotException(robotExceptions.MAGICVALUE, getName());
//throw new RobotMagicValueException(getName() + " has an unknown error. Code 42");
}
}
return new int[0];
}
@Override
public int[] think(int[] zahlen) throws RobotException {
return new int[0];
public int[] think(int[] input) throws RobotException {
//Insertionsort
if(isPowerOn()){
return sorting(input);
}else{
throw new RobotException(robotExceptions.ILLEGALSTATE, getName());
// throw new RobotIllegalStateException(getName() + " is turned off.");
}
}
}

View File

@ -1,16 +1,9 @@
package domain;
import robot.exceptions.ExceptionStorage;
import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException;
import robot.exceptions.RobotMagicValueException;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.IntPredicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import robot.exceptions.robotExceptions;
public class R2D2 extends RobotBasics {
/**
@ -22,53 +15,45 @@ public class R2D2 extends RobotBasics {
super(id, name);
}
/*Sorting sort = (int[] input) -> {
int small;
for(int i = 0; i < input.length -1; i++){
small = i;
for(int j = i + 1; j < input.length; j++){
if(input[j] < )
}
}
}*/
public int[] think(int[] zahlen) throws RobotException {
public int[] think(int[] input) throws RobotException {
if(isPowerOn()){
return sorting(zahlen);
return selectionSort(input);
}else{
throw new RobotIllegalStateException(getName() + " is turned off!");
this.exceptions = new ExceptionStorage( new RobotException(robotExceptions.ILLEGALSTATE, getName()));
throw new RobotException(robotExceptions.ILLEGALSTATE, getName());
}
}
public int[] sorting(int[] arr) {
//Selectionsort
int small;
for (int i = 0; i <arr.length - 1; i++) {
small = i;
for (int j = i + 1; j < arr.length; j++) {
//if there is a smaller number on this position
if (arr[j] < arr[small]) {
small = j;
//swap values
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
/*
* this method sorts
* @param input
* @return integer array
* @throws RobotException
public int[] sorting(int[] input) throws RobotException {
if(checkArray(input)){
//Selectionsort
int small;
for (int i = 0; i <input.length - 1; i++) {
small = i;
for (int j = i + 1; j < input.length; j++) {
//if there is a smaller number on this position
if (input[j] < input[small]) {
small = j;
//swap values
int temp = input[i];
input[i] = input[small];
input[small] = temp;
}
}
}
}
return arr;
}
// Diese ganze Methode muss zu C3P0 und ist hier falsch.
public String ausgabe(int[] input)throws RobotException{
if(input.length != 0 && !checkArray(input)) {
return Arrays.stream(input)
.mapToObj(Integer::toString)
.collect(Collectors.joining("; "));
return input;
}else{
throw new RobotMagicValueException(getName() +" has an unknown Error. Code 42.");
throw new RobotException(robotExceptions.MAGICVALUE, getName());
}
}
}*/
/**
* Die Methode nimmt ein Array aus int entgegen und wandelt diese in einen String um.
@ -78,21 +63,11 @@ public class R2D2 extends RobotBasics {
*/
@Override
public String speak(int[] input) throws RobotException {
final boolean[] found_42 = {false};
if (isPowerOn()) {
try{
return ausgabe(input);
}catch(RobotException re){
return re.toString();
}
return ausgabe(input, ";");
} else {
throw new RobotIllegalStateException(getName() + " is turned off!");
this.exceptions = new ExceptionStorage( new RobotException(robotExceptions.ILLEGALSTATE, getName()));
throw new RobotException(robotExceptions.ILLEGALSTATE, getName());
}
/*public void something() {
System.out.println("Hello");
}*/
}
}

View File

@ -1,13 +1,16 @@
package domain;
import robot.exceptions.ExceptionStorage;
import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException;
import robot.exceptions.robotExceptions;
import robot.interfaces.Robot;
import java.util.Arrays;
import java.util.stream.Collectors;
public abstract class RobotBasics implements Robot {
protected ExceptionStorage exceptions;
private int id;
private String name;
private boolean power;
@ -16,7 +19,9 @@ public abstract class RobotBasics implements Robot {
this.id = id;
this.name = name;
this.power = false;
this.exceptions = null;
}
/**
* @see robot.interfaces.RobotControl;
*/
@ -58,16 +63,98 @@ public abstract class RobotBasics implements Robot {
*/
@Override
public RobotException getLastException() {
return null;
return exceptions.getLastErrorMessage();
}
public boolean checkArray(int[] input){
for(int x: input){
if(x == 42){
return true;
/**
* This method checks an array of integers and gives back a boolean value.
* If the array contains the number 42 the method returns false.
* Otherwise, always true.
*
* If the length of the Array = 0 it throws an EMPTYARRAY-Exception
* @param input
* @return boolean
* @throws RobotException EMPTYARRAY Exception
*/
public boolean checkArray(int[] input) throws RobotException{
if(input.length != 0){
for(int x: input){
if(x == 42){ return false; }
}
return true;
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.EMPTYARRAY, getName()));
throw new RobotException(robotExceptions.EMPTYARRAY, getName());
}
return false;
}
/**
* This method uses Streams to join any given array to a String.
*
* @param input int [ ]
* @param delemiter String
* @return String (array as String)
* @throws RobotException
*/
public String ausgabe(int[] input, String delemiter)throws RobotException{
if(checkArray(input)) {
return Arrays.stream(input)
.mapToObj(Integer::toString)
.collect(Collectors.joining(delemiter + " "));
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
throw new RobotException(robotExceptions.MAGICVALUE, getName());
}
}
/**
* Sorts any given array of integers with the insertion Sort algorithm.
* @param input int [ ]
* @return input int [ ] (sorted)
* @throws RobotException
*/
public int[] insertionSort(int[] input) throws RobotException{
if(checkArray(input)){
return new int[0];
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
throw new RobotException(robotExceptions.MAGICVALUE, getName());
}
}
/**
* Sorts any given array of integers with the selection Sort algorithm.
* @param input
* @return
* @throws RobotException
*/
public int[] selectionSort(int[] input) throws RobotException{
if(checkArray(input)){
int small;
for(int i = 0; i < input.length; i++){
small = i;
for(int j = i + 1; j < input.length; j++){
if(input[j] < input[small]){
small = j;
// System.out.println(small);
}
}
int temp = input[i];
input[i] = input[small];
input[small] = temp;
/*for(int n = 0; n < input.length; n++){
System.out.print( " " + input[n]);
}
System.out.println();*/
}
return input;
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
throw new RobotException(robotExceptions.MAGICVALUE, getName());
}
}
}

View File

@ -12,7 +12,6 @@
- Philipp Kotte Matr. : 2211945;
- Cedric Hermann Matr. : 2210943;
- XXXXXXX XXXXX Matr. : XXXXXXX;
# Overview
@ -44,6 +43,25 @@
* [Main](#-classe-main-)
* [makefile](#-makefile-for-Git-updates-)
# TO-Dos:
* Sortier Algorythem C3PO, R2D2 (mit Ausgabe)
* Bei Erstellung eines Roboters wird einne SerienNr erstellt
* Wichtige getter for Robots (getName)
* Exception Classes (Throwable einfügen)
* RobotFactory, die mit enum(RobotType) Objekt von R2 und C3PO erstellen kann
* Nexus6(Singleton) implementieren, kann nichts (Illegal-State)
* JUnit5 Tests
* Wie fügt man weitere Roboter hinzu erklärung in einer Text Datei
## Domain
<h2 align="center"> Class R2D2 </h2>
@ -350,3 +368,9 @@ Textdatei in Ihr Projekt, in der Sie die notwendigen Erweiterungen und evtl. Än
* Testen Sie Ihre Implementierung möglichst umfangreich. Wir werden es auf jeden Fall tun. !
* Nutzen Sie für die gemeinsame Arbeit an der Implementierung ein Git-Repository und nutzen Sie regelmäßig.
* Es müssen von allen Team-Mitgliedern jeweils mindestens fünf Commits mit substanziellem Inhalt auf den main-Branch gepusht werden.
# Fragen an Hummel
Was ist mit der Umwandlung von zahlen in arrays den inhalt mit Kommas getrennt gemeint?

View File

@ -15,6 +15,13 @@ update_domain:
git add domain/
git commit -m "Updated domain. Further explanation in README"
git push -u origin main
update_exceptions:
git add robot/exceptions
git commit -m "Updated Exceptions"
git push -u origin main
update_interfaces:
git add robot/interfaces
git commit -m "updated interfaces"
git push -u origin main
fetch_git:
git pull origin main

View File

@ -0,0 +1,7 @@
package robot.exceptions;
public class ArrayEmptyException extends RobotException{
public ArrayEmptyException(robotExceptions type,String errorMessage){
super(type, errorMessage);
}
}

View File

@ -0,0 +1,35 @@
package robot.exceptions;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ExceptionStorage {
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;
}
public void saveLatestErrorMessage(RobotException message){
this.message = message;
this.date = LocalDateTime.now();
}
public RobotException getLastErrorMessage(){
return this.message;
}
/*public boolean emptyErrorStorage(){
if(this.message != ""){
this.message = "";
this.date = LocalDateTime.now();
return true;
}else{return false;}
}*/
}

View File

@ -1,8 +1,22 @@
package robot.exceptions;
public class RobotException extends Exception{
public RobotException(String errorMessage){
super(errorMessage);
robotExceptions currentType;
public RobotException(robotExceptions type, String name){
super(getMessage(type, name));
this.currentType = type;
}
private static String getMessage(robotExceptions types, String name){
String message = "";
switch (types){
case ILLEGALSTATE: message = name + " is turned off."; break;
case MAGICVALUE: message = name + " has an unknown error. Code 42."; break;
case EMPTYARRAY: message = name + " got an empty array."; break;
}
return message;
}
}

View File

@ -1,7 +1,8 @@
package robot.exceptions;
public class RobotIllegalStateException extends RobotException{
public RobotIllegalStateException(String errormessage){
super(errormessage);
public RobotIllegalStateException(robotExceptions type, String errormessage){
super(type, errormessage);
}
}

View File

@ -1,7 +1,8 @@
package robot.exceptions;
public class RobotMagicValueException extends RobotException{
public RobotMagicValueException(String errormessage){
super(errormessage);
public class RobotMagicValueException extends RobotException {
public RobotMagicValueException(robotExceptions type, String errormessage) {
super(type, errormessage);
}
}
}

View File

@ -0,0 +1,5 @@
package robot.exceptions;
public enum robotExceptions {
ILLEGALSTATE, MAGICVALUE, EMPTYARRAY
}

View File

@ -30,23 +30,23 @@ public interface RobotInstructions {
* Gibt ein Array von Zahlen als String zurück. Die Zahlen werden
* <b>nicht</b> sortiert.
*
* @param zahlen Zahlen, die ausgegeben werden sollen.
* @param numbers Zahlen, die ausgegeben werden sollen.
* @return Zahlen als String
* @throws RobotException wenn der Roboter in einem ungültigen Zustand ist,
* oder das Array nicht seinen Vorstellungen entspricht.
*/
String speak(int[] zahlen) throws RobotException;
String speak(int[] numbers) throws RobotException;
/**
* Sortiert ein Array von Zahlen. Die Reihenfolge hängt von dem Typ des
* Roboters ab.
*
* @param zahlen Zahlen, die sortiert werden sollen.
* @param numbers Zahlen, die sortiert werden sollen.
* @return Sortierte Zahlen
* @throws RobotException wenn der Roboter in einem ungültigen Zustand ist,
* oder das Array nicht seinen Vorstellungen entspricht.
*/
int[] think(int[] zahlen) throws RobotException;
int[] think(int[] numbers) throws RobotException;
}