Corrected/Optimised/Cleaned up Code, wrote most of the comments
parent
24380733bf
commit
4162f6df07
|
@ -9,22 +9,12 @@ public class C3PO extends Robot {
|
||||||
super(id, name, "C3PO");
|
super(id, name, "C3PO");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//returns the sorted list as String with the delimiter ';'
|
||||||
/* public String ausgabe(int[] input) throws RobotException{
|
|
||||||
if(input.length != 0 && !checkArray(input)){
|
|
||||||
return Arrays.stream(input)
|
|
||||||
.mapToObj(Integer::toString)
|
|
||||||
.collect(Collectors.joining("; "));
|
|
||||||
}else{
|
|
||||||
throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
||||||
// throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
@Override
|
@Override
|
||||||
public String speak(int[] input) throws RobotException {
|
public String speak(int[] input) throws RobotException {
|
||||||
if(isPowerOn()){
|
if(isPowerOn()){
|
||||||
try{
|
try{
|
||||||
return ausgabe(input, ";");
|
return output(input, ";");
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
return re.toString();
|
return re.toString();
|
||||||
}
|
}
|
||||||
|
@ -33,16 +23,6 @@ public class C3PO extends Robot {
|
||||||
this.exceptions = new ExceptionStorage(robotException);
|
this.exceptions = new ExceptionStorage(robotException);
|
||||||
throw robotException;
|
throw robotException;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] sorting(int[] input) throws RobotException{
|
|
||||||
if(checkArray(input)){
|
|
||||||
return insertionSort(input);
|
|
||||||
}else{
|
|
||||||
throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
||||||
//throw new RobotMagicValueException(getName() + " has an unknown error. Code 42");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,12 +47,11 @@ public class C3PO extends Robot {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//Sorting then returning the input arr with insertion Sort
|
||||||
@Override
|
@Override
|
||||||
public int[] think(int[] input) throws RobotException {
|
public int[] think(int[] input) throws RobotException {
|
||||||
//Insertionsort
|
|
||||||
if(isPowerOn()){
|
if(isPowerOn()){
|
||||||
return sorting(input);
|
return insertionSort(input);
|
||||||
}else{
|
}else{
|
||||||
RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
||||||
this.exceptions = new ExceptionStorage(robotException);
|
this.exceptions = new ExceptionStorage(robotException);
|
||||||
|
|
|
@ -19,6 +19,7 @@ public class Factory implements Serializable {
|
||||||
return robots.values();
|
return robots.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//return a String arr with robot attributes
|
||||||
public String[] getRobotList() {
|
public String[] getRobotList() {
|
||||||
Collection<Robot> collect = robotListToCollection();
|
Collection<Robot> collect = robotListToCollection();
|
||||||
String[] list = new String[collect.size()];
|
String[] list = new String[collect.size()];
|
||||||
|
@ -28,20 +29,21 @@ public class Factory implements Serializable {
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
//creates a new robot
|
||||||
public boolean buildNewRobot(String name, int type){
|
public boolean buildNewRobot(String name, int type){
|
||||||
Robot r ;
|
Robot r ;
|
||||||
if(type == 0){
|
if(type == 0) {
|
||||||
r = new R2D2(r2d2ID++, name);
|
r = new R2D2(r2d2ID++, name);
|
||||||
}else if(type == 1){
|
} else if(type == 1) {
|
||||||
r = new C3PO(c3poID++, name);
|
r = new C3PO(c3poID++, name);
|
||||||
}else{
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
robots.put(r.getId(), r);
|
robots.put(r.getId(), r);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
//returns a specific robot via id
|
||||||
public Robot getRobotOfList(int id){
|
public Robot getRobotOfList(int id){
|
||||||
return robots.get(id);
|
return robots.get(id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,14 @@ public final class Nexus6 extends Robot {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//This method always throws an Illegalstate exception
|
||||||
@Override
|
@Override
|
||||||
public String speak(int[] numbers) throws RobotException {
|
public String speak(int[] numbers) throws RobotException {
|
||||||
RobotException e = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
RobotException e = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
||||||
this.exceptions = new ExceptionStorage(e);
|
this.exceptions = new ExceptionStorage(e);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
//This method always throws an Illegalstate exception
|
||||||
@Override
|
@Override
|
||||||
public int[] think(int[] numbers) throws RobotException {
|
public int[] think(int[] numbers) throws RobotException {
|
||||||
RobotException e = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
RobotException e = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
||||||
|
|
|
@ -7,7 +7,6 @@ import utility.robot_exceptions.robotExceptions;
|
||||||
|
|
||||||
public class R2D2 extends Robot {
|
public class R2D2 extends Robot {
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param id> int
|
* @param id> int
|
||||||
* @param name> String
|
* @param name> String
|
||||||
*/
|
*/
|
||||||
|
@ -15,10 +14,9 @@ public class R2D2 extends Robot {
|
||||||
super(id, name, "R2D2");
|
super(id, name, "R2D2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotInstructions
|
* @see utility.interfaces.RobotInstructions
|
||||||
|
* Sorting then returning the input arr with selection sort
|
||||||
*/
|
*/
|
||||||
public int[] think(int[] input) throws RobotException {
|
public int[] think(int[] input) throws RobotException {
|
||||||
if(isPowerOn()){
|
if(isPowerOn()){
|
||||||
|
@ -44,7 +42,6 @@ public class R2D2 extends Robot {
|
||||||
for(int j = i + 1; j < input.length; j++){
|
for(int j = i + 1; j < input.length; j++){
|
||||||
if(input[j] < input[small]){
|
if(input[j] < input[small]){
|
||||||
small = j;
|
small = j;
|
||||||
// System.out.println(small);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int temp = input[i];
|
int temp = input[i];
|
||||||
|
@ -57,17 +54,16 @@ public class R2D2 extends Robot {
|
||||||
this.exceptions = new ExceptionStorage(robotexception);
|
this.exceptions = new ExceptionStorage(robotexception);
|
||||||
throw robotexception;
|
throw robotexception;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotInstructions
|
* @see utility.interfaces.RobotInstructions
|
||||||
|
* returns the sorted list as String with the delimiter ','
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String speak(int[] input) throws RobotException {
|
public String speak(int[] input) throws RobotException {
|
||||||
if (isPowerOn()) {
|
if (isPowerOn()) {
|
||||||
return ausgabe(input, ";");
|
return output(input, ",");
|
||||||
} else {
|
} else {
|
||||||
RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
||||||
this.exceptions = new ExceptionStorage(robotexception);
|
this.exceptions = new ExceptionStorage(robotexception);
|
||||||
|
|
|
@ -10,9 +10,13 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
|
// ENUMS für Robot Type
|
||||||
|
static enum RobotType {
|
||||||
|
C3PO, R2D2, Nexus6;
|
||||||
|
}
|
||||||
protected ExceptionStorage exceptions;
|
protected ExceptionStorage exceptions;
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private final String name;
|
||||||
private boolean power;
|
private boolean power;
|
||||||
|
|
||||||
private String type;
|
private String type;
|
||||||
|
@ -46,11 +50,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void triggerPowerSwitch() {
|
public void triggerPowerSwitch() {
|
||||||
if(power){
|
power = !power;
|
||||||
power = false;
|
|
||||||
}else{
|
|
||||||
power = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,6 +79,8 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @throws RobotException EMPTYARRAY Exception
|
* @throws RobotException EMPTYARRAY Exception
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Check lists for the forbidden Number 42
|
||||||
public boolean checkArray(int[] input) throws RobotException{
|
public boolean checkArray(int[] input) throws RobotException{
|
||||||
if(input.length != 0){
|
if(input.length != 0){
|
||||||
for(int x: input){
|
for(int x: input){
|
||||||
|
@ -89,11 +91,8 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
RobotException robotexception = new RobotException(robotExceptions.EMPTYARRAY, getName());
|
RobotException robotexception = new RobotException(robotExceptions.EMPTYARRAY, getName());
|
||||||
this.exceptions = new ExceptionStorage(robotexception);
|
this.exceptions = new ExceptionStorage(robotexception);
|
||||||
throw robotexception;
|
throw robotexception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method uses Streams to join any given array to a String.
|
* This method uses Streams to join any given array to a String.
|
||||||
* @param input int [ ]
|
* @param input int [ ]
|
||||||
|
@ -101,7 +100,9 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
* @return String (array as String)
|
* @return String (array as String)
|
||||||
* @throws RobotException
|
* @throws RobotException
|
||||||
*/
|
*/
|
||||||
public String ausgabe(int[] input, String delemiter)throws RobotException{
|
|
||||||
|
// Write an array with a delimiter to the command line
|
||||||
|
public String output(int[] input, String delemiter)throws RobotException{
|
||||||
if(checkArray(input)) {
|
if(checkArray(input)) {
|
||||||
return Arrays.stream(input)
|
return Arrays.stream(input)
|
||||||
.mapToObj(Integer::toString)
|
.mapToObj(Integer::toString)
|
||||||
|
@ -112,14 +113,11 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
throw robotexception;
|
throw robotexception;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String getType(){
|
public String getType(){
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Override the to String method to get a clean looking outcome
|
||||||
@Override
|
@Override
|
||||||
public String toString(){
|
public String toString(){
|
||||||
return "Name: " + name + "; ID: " + id + "; Type: " + type;
|
return "Name: " + name + "; ID: " + id + "; Type: " + type;
|
||||||
|
|
|
@ -25,11 +25,12 @@ public class FactorySystem {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//provide robot attributes
|
||||||
public String[] getAllRobots(){
|
public String[] getAllRobots(){
|
||||||
return factory.getRobotList();
|
return factory.getRobotList();
|
||||||
}
|
}
|
||||||
|
//Creating a new robot
|
||||||
public boolean buildNewRobot(String name, int type){
|
public boolean buildNewRobot(String name, int type) {
|
||||||
boolean check = factory.buildNewRobot(name, type);
|
boolean check = factory.buildNewRobot(name, type);
|
||||||
if(check) {
|
if(check) {
|
||||||
try {
|
try {
|
||||||
|
@ -40,7 +41,7 @@ public class FactorySystem {
|
||||||
}
|
}
|
||||||
return check;
|
return check;
|
||||||
}
|
}
|
||||||
|
//provides a specific robot
|
||||||
public Robot searchForRobot(int id){
|
public Robot searchForRobot(int id){
|
||||||
return factory.getRobotOfList(id);
|
return factory.getRobotOfList(id);
|
||||||
}
|
}
|
||||||
|
|
30
ui/UI.java
30
ui/UI.java
|
@ -1,12 +1,8 @@
|
||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import domain.C3PO;
|
|
||||||
import domain.R2D2;
|
|
||||||
import domain.Robot;
|
import domain.Robot;
|
||||||
import facade.FactorySystem;
|
import facade.FactorySystem;
|
||||||
import infrastructure.Persistenz;
|
import infrastructure.Persistenz;
|
||||||
|
|
||||||
import java.sql.SQLOutput;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class UI {
|
public class UI {
|
||||||
|
@ -25,14 +21,13 @@ public class UI {
|
||||||
if(Persistenz.existsSavedData(name)){
|
if(Persistenz.existsSavedData(name)){
|
||||||
try{
|
try{
|
||||||
this.fs = (FactorySystem) Persistenz.loadFactoryData(name);
|
this.fs = (FactorySystem) Persistenz.loadFactoryData(name);
|
||||||
}catch(Exception e){
|
}catch(Exception ignored){
|
||||||
|
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
this.fs = new FactorySystem(name);
|
this.fs = new FactorySystem(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//starting screen
|
||||||
private void hauptmenü() {
|
private void hauptmenü() {
|
||||||
mainloop:
|
mainloop:
|
||||||
while(true){
|
while(true){
|
||||||
|
@ -44,6 +39,7 @@ public class UI {
|
||||||
System.out.println("-3- ------- use robot ------");
|
System.out.println("-3- ------- use robot ------");
|
||||||
System.out.println("-4- --------- Exit ---------");
|
System.out.println("-4- --------- Exit ---------");
|
||||||
System.out.print(" > ");
|
System.out.print(" > ");
|
||||||
|
//User options
|
||||||
try{
|
try{
|
||||||
int input = Integer.parseInt(sc.nextLine());
|
int input = Integer.parseInt(sc.nextLine());
|
||||||
switch(input){
|
switch(input){
|
||||||
|
@ -55,15 +51,13 @@ public class UI {
|
||||||
default:
|
default:
|
||||||
System.out.println("this is an invalid option"); break;
|
System.out.println("this is an invalid option"); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}catch(NumberFormatException e){
|
}catch(NumberFormatException e){
|
||||||
System.out.println("invalid input");
|
System.out.println("invalid input");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Good Bye");
|
System.out.println("Good Bye");
|
||||||
}
|
}
|
||||||
|
//display all existing robots
|
||||||
private void listAllRobots(){
|
private void listAllRobots(){
|
||||||
String[] listOfAll = fs.getAllRobots();
|
String[] listOfAll = fs.getAllRobots();
|
||||||
if(listOfAll.length > 0){
|
if(listOfAll.length > 0){
|
||||||
|
@ -74,9 +68,8 @@ public class UI {
|
||||||
}else{
|
}else{
|
||||||
System.out.println("There are no robots yet.");
|
System.out.println("There are no robots yet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//create a new robot
|
||||||
private void buildNewRobot(){
|
private void buildNewRobot(){
|
||||||
System.out.println("What shall the name of the robot be?");
|
System.out.println("What shall the name of the robot be?");
|
||||||
System.out.print(" > ");
|
System.out.print(" > ");
|
||||||
|
@ -84,25 +77,24 @@ public class UI {
|
||||||
System.out.println("Which type of robot do u want?");
|
System.out.println("Which type of robot do u want?");
|
||||||
System.out.println(" [0] for R2D2 and [1] for C3PO");
|
System.out.println(" [0] for R2D2 and [1] for C3PO");
|
||||||
System.out.print(" > ");
|
System.out.print(" > ");
|
||||||
|
//user enters which type of robot he wants
|
||||||
int type = Integer.parseInt(sc.nextLine());
|
int type = Integer.parseInt(sc.nextLine());
|
||||||
if(fs.buildNewRobot(name, type)){
|
if(fs.buildNewRobot(name, type)){
|
||||||
switch(type){
|
switch (type) {
|
||||||
case 0:
|
case 0 -> System.out.println("Created new Robot of type R2D2 with the name " + name);
|
||||||
//Created new Robot of type R2D2 with the name
|
case 1 -> System.out.println("Created new Robot of type C3PO with the name " + name);
|
||||||
System.out.println("Created new Robot of type R2D2 with the name " + name );break;
|
|
||||||
case 1:
|
|
||||||
System.out.println("Created new Robot of type C3PO with the name " + name );break;
|
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
System.out.println("Anlegen des Roboters fehlgeschlagen");
|
System.out.println("Anlegen des Roboters fehlgeschlagen");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//let the robots sort
|
||||||
private void useRobot(){
|
private void useRobot(){
|
||||||
System.out.println("Which robot do you want to use?");
|
System.out.println("Which robot do you want to use?");
|
||||||
listAllRobots();
|
listAllRobots();
|
||||||
System.out.print(" ID > ");
|
System.out.print(" ID > ");
|
||||||
int input = Integer.parseInt(sc.nextLine());
|
int input = Integer.parseInt(sc.nextLine());
|
||||||
|
// Change the searchForRobot Methode (safety)
|
||||||
Robot r = fs.searchForRobot(input);
|
Robot r = fs.searchForRobot(input);
|
||||||
System.out.println("You choose " + r.getName() + " of type " + r.getType());
|
System.out.println("You choose " + r.getName() + " of type " + r.getType());
|
||||||
System.out.println("Yout have following options");
|
System.out.println("Yout have following options");
|
||||||
|
|
Loading…
Reference in New Issue