diff --git a/domain/C3PO.java b/domain/C3PO.java index b25c0ea..48221b4 100644 --- a/domain/C3PO.java +++ b/domain/C3PO.java @@ -6,25 +6,15 @@ import utility.robot_exceptions.robotExceptions; public class C3PO extends Robot { public C3PO(int id, String name){ - super(id, name, "C3PO"); + super(id, name, RobotType.C3PO); } - -/* 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."); - } - }*/ + //returns the sorted list as String with the delimiter ';' @Override public String speak(int[] input) throws RobotException { if(isPowerOn()){ try{ - return ausgabe(input, ";"); + return output(input, ";"); }catch(RobotException re){ return re.toString(); } @@ -33,18 +23,8 @@ public class C3PO extends Robot { this.exceptions = new ExceptionStorage(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"); - } - } - /** * Sorts any given array of integers with the insertion Sort algorithm. * @param input int [ ] @@ -67,12 +47,11 @@ public class C3PO extends Robot { } } - + //Sorting then returning the input arr with insertion Sort @Override public int[] think(int[] input) throws RobotException { - //Insertionsort if(isPowerOn()){ - return sorting(input); + return insertionSort(input); }else{ RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName()); this.exceptions = new ExceptionStorage(robotException); diff --git a/domain/Factory.java b/domain/Factory.java index 71a2c0d..18d9b9f 100644 --- a/domain/Factory.java +++ b/domain/Factory.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.HashMap; public class Factory implements Serializable { + private HashMap robots = new HashMap<>(); private int c3poID = 0; private int r2d2ID = 10000; @@ -14,25 +15,36 @@ public class Factory implements Serializable { } //Has to return Collection - public Collection getRobotList(){ + public Collection robotListToCollection(){ return robots.values(); } - public boolean buildNewRobot(String name, int type){ + //return a String arr with robot attributes + public String[] getRobotList() { + Collection collect = robotListToCollection(); + String[] list = new String[collect.size()]; + int i = 0; + for(Robot r: collect){ + list[i++] = r.toString(); + } + return list; + } + //creates a new robot + public boolean buildNewRobot(String name, int type){ Robot r ; - if(type == 0){ + if(type == 0) { r = new R2D2(r2d2ID++, name); - }else if(type == 1){ + } else if(type == 1) { r = new C3PO(c3poID++, name); - }else{ + } else { return false; } robots.put(r.getId(), r); return true; - } - - public Robot getRobotOfList(int id){ + } + //returns a specific robot via id + public Robot getRobotOfList(int id){ return robots.get(id); } } diff --git a/domain/Nexus6.java b/domain/Nexus6.java index 576d047..bb4f6bc 100644 --- a/domain/Nexus6.java +++ b/domain/Nexus6.java @@ -16,13 +16,14 @@ public final class Nexus6 extends Robot { return INSTANCE; } + //This method always throws an Illegalstate exception @Override public String speak(int[] numbers) throws RobotException { RobotException e = new RobotException(robotExceptions.ILLEGALSTATE, getName()); this.exceptions = new ExceptionStorage(e); throw e; } - + //This method always throws an Illegalstate exception @Override public int[] think(int[] numbers) throws RobotException { RobotException e = new RobotException(robotExceptions.ILLEGALSTATE, getName()); diff --git a/domain/R2D2.java b/domain/R2D2.java index a70cba1..747cde3 100644 --- a/domain/R2D2.java +++ b/domain/R2D2.java @@ -7,18 +7,16 @@ import utility.robot_exceptions.robotExceptions; public class R2D2 extends Robot { /** - * * @param id> int * @param name> String */ public R2D2(int id, String name){ - super(id, name, "R2D2"); + super(id, name, RobotType.R2D2); } - - /** * @see utility.interfaces.RobotInstructions + * Sorting then returning the input arr with selection sort */ public int[] think(int[] input) throws RobotException { if(isPowerOn()){ @@ -44,7 +42,6 @@ public class R2D2 extends Robot { for(int j = i + 1; j < input.length; j++){ if(input[j] < input[small]){ small = j; -// System.out.println(small); } } int temp = input[i]; @@ -57,17 +54,16 @@ public class R2D2 extends Robot { this.exceptions = new ExceptionStorage(robotexception); throw robotexception; } - } - /** * @see utility.interfaces.RobotInstructions + * returns the sorted list as String with the delimiter ',' */ @Override public String speak(int[] input) throws RobotException { if (isPowerOn()) { - return ausgabe(input, ";"); + return output(input, ","); } else { RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName()); this.exceptions = new ExceptionStorage(robotexception); diff --git a/domain/Robot.java b/domain/Robot.java index e429c5d..1bef763 100644 --- a/domain/Robot.java +++ b/domain/Robot.java @@ -10,14 +10,18 @@ import java.util.stream.Collectors; public abstract class Robot implements utility.interfaces.Robot, Serializable { + // ENUMS für Robot Type + static enum RobotType { + C3PO, R2D2, Nexus6; + } protected ExceptionStorage exceptions; private int id; private final String name; private boolean power; - private String type; + private RobotType type; - public Robot(int id, String name, String type){ + public Robot(int id, String name, RobotType type){ this.id = id; this.name = name; this.power = false; @@ -46,11 +50,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable { */ @Override public void triggerPowerSwitch() { - if(power){ - power = false; - }else{ - power = true; - } + power = !power; } /** @@ -79,6 +79,8 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable { * @return boolean * @throws RobotException EMPTYARRAY Exception */ + + // Check lists for the forbidden Number 42 public boolean checkArray(int[] input) throws RobotException{ if(input.length != 0){ for(int x: input){ @@ -89,11 +91,8 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable { RobotException robotexception = new RobotException(robotExceptions.EMPTYARRAY, getName()); this.exceptions = new ExceptionStorage(robotexception); throw robotexception; - } - } - /** * This method uses Streams to join any given array to a String. * @param input int [ ] @@ -101,7 +100,9 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable { * @return String (array as String) * @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)) { return Arrays.stream(input) .mapToObj(Integer::toString) @@ -112,14 +113,11 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable { throw robotexception; } } - - - - public String getType(){ - return this.type; + return this.type.toString(); } + // Override the to String method to get a clean looking outcome @Override public String toString(){ return "Name: " + name + "; ID: " + id + "; Type: " + type; diff --git a/domain/RobotType.java b/domain/RobotType.java new file mode 100644 index 0000000..dbab225 --- /dev/null +++ b/domain/RobotType.java @@ -0,0 +1,16 @@ +package domain; + +public enum RobotType { + R2D2("R2D2"), + C3PO("C3PO"), + NEXUS6("Nexus-6"); + + private final String type; + private RobotType(String type){ + this.type = type; +} + @Override + public String toString(){ + return "Type: " + type; + } +} diff --git a/facade/FactorySystem.java b/facade/FactorySystem.java index 9240c37..1d3dc97 100644 --- a/facade/FactorySystem.java +++ b/facade/FactorySystem.java @@ -1,7 +1,8 @@ package facade; -import java.util.Collection; -import domain.*; +import domain.Factory; +import domain.Robot; + import infrastructure.Persistenz; public class FactorySystem { @@ -25,17 +26,12 @@ public class FactorySystem { } + //provide robot attributes public String[] getAllRobots(){ - Collection robots = factory.getRobotList(); - String[] list = new String[robots.size()]; - int i = 0; - for(Robot r: robots){ - list[i++] = r.toString(); - } - return list; + return factory.getRobotList(); } - - public boolean buildNewRobot(String name, int type){ + //Creating a new robot + public boolean buildNewRobot(String name, int type) { boolean check = factory.buildNewRobot(name, type); if(check) { try { @@ -46,7 +42,7 @@ public class FactorySystem { } return check; } - + //provides a specific robot public Robot searchForRobot(int id){ return factory.getRobotOfList(id); } diff --git a/out/production/Robot_Factory_PR/domain/C3PO.class b/out/production/Robot_Factory_PR/domain/C3PO.class index d7c298d..41e78bc 100644 Binary files a/out/production/Robot_Factory_PR/domain/C3PO.class and b/out/production/Robot_Factory_PR/domain/C3PO.class differ diff --git a/out/production/Robot_Factory_PR/domain/Factory.class b/out/production/Robot_Factory_PR/domain/Factory.class index b2de0b8..b5330ad 100644 Binary files a/out/production/Robot_Factory_PR/domain/Factory.class and b/out/production/Robot_Factory_PR/domain/Factory.class differ diff --git a/out/production/Robot_Factory_PR/domain/Nexus6.class b/out/production/Robot_Factory_PR/domain/Nexus6.class new file mode 100644 index 0000000..61c70f9 Binary files /dev/null and b/out/production/Robot_Factory_PR/domain/Nexus6.class differ diff --git a/out/production/Robot_Factory_PR/domain/R2D2.class b/out/production/Robot_Factory_PR/domain/R2D2.class index 54b8f8b..0528945 100644 Binary files a/out/production/Robot_Factory_PR/domain/R2D2.class and b/out/production/Robot_Factory_PR/domain/R2D2.class differ diff --git a/out/production/Robot_Factory_PR/domain/Robot$RobotType.class b/out/production/Robot_Factory_PR/domain/Robot$RobotType.class new file mode 100644 index 0000000..46213d0 Binary files /dev/null and b/out/production/Robot_Factory_PR/domain/Robot$RobotType.class differ diff --git a/out/production/Robot_Factory_PR/domain/Robot.class b/out/production/Robot_Factory_PR/domain/Robot.class index ea0b3c0..bb932ee 100644 Binary files a/out/production/Robot_Factory_PR/domain/Robot.class and b/out/production/Robot_Factory_PR/domain/Robot.class differ diff --git a/out/production/Robot_Factory_PR/facade/FactorySystem.class b/out/production/Robot_Factory_PR/facade/FactorySystem.class index df80877..409ab59 100644 Binary files a/out/production/Robot_Factory_PR/facade/FactorySystem.class and b/out/production/Robot_Factory_PR/facade/FactorySystem.class differ diff --git a/out/production/Robot_Factory_PR/ui/UI.class b/out/production/Robot_Factory_PR/ui/UI.class index 50def97..a53d4f5 100644 Binary files a/out/production/Robot_Factory_PR/ui/UI.class and b/out/production/Robot_Factory_PR/ui/UI.class differ diff --git a/out/production/Robot_Factory_PR/utility/robot_exceptions/RobotException.class b/out/production/Robot_Factory_PR/utility/robot_exceptions/RobotException.class index fdcc11e..b26ffd4 100644 Binary files a/out/production/Robot_Factory_PR/utility/robot_exceptions/RobotException.class and b/out/production/Robot_Factory_PR/utility/robot_exceptions/RobotException.class differ diff --git a/ui/UI.java b/ui/UI.java index e30fd55..70080cb 100644 --- a/ui/UI.java +++ b/ui/UI.java @@ -1,13 +1,10 @@ package ui; -import domain.C3PO; -import domain.Nexus6; -import domain.R2D2; + + import domain.Robot; import facade.FactorySystem; import infrastructure.Persistenz; - -import java.sql.SQLOutput; import java.util.Scanner; public class UI { @@ -26,14 +23,13 @@ public class UI { if(Persistenz.existsSavedData(name)){ try{ this.fs = (FactorySystem) Persistenz.loadFactoryData(name); - }catch(Exception e){ - + }catch(Exception ignored){ } }else{ this.fs = new FactorySystem(name); } } - + //starting screen private void hauptmenü() { mainloop: while(true){ @@ -45,6 +41,7 @@ public class UI { System.out.println("-3- ------- use robot ------"); System.out.println("-4- --------- Exit ---------"); System.out.print(" > "); + //User options try{ int input = Integer.parseInt(sc.nextLine()); switch(input){ @@ -56,15 +53,13 @@ public class UI { default: System.out.println("this is an invalid option"); break; } - }catch(NumberFormatException e){ System.out.println("invalid input"); } } - System.out.println("Good Bye"); } - + //display all existing robots private void listAllRobots(){ String[] listOfAll = fs.getAllRobots(); if(listOfAll.length > 0){ @@ -75,9 +70,8 @@ public class UI { }else{ System.out.println("There are no robots yet."); } - } - + //create a new robot private void buildNewRobot(){ System.out.println("What shall the name of the robot be?"); System.out.print(" > "); @@ -85,25 +79,24 @@ public class UI { System.out.println("Which type of robot do u want?"); System.out.println(" [0] for R2D2 and [1] for C3PO"); System.out.print(" > "); + //user enters which type of robot he wants int type = Integer.parseInt(sc.nextLine()); if(fs.buildNewRobot(name, type)){ - switch(type){ - case 0: - //Created new Robot of type R2D2 with the 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; + switch (type) { + case 0 -> System.out.println("Created new Robot of type R2D2 with the name " + name); + case 1 -> System.out.println("Created new Robot of type C3PO with the name " + name); } }else{ System.out.println("Anlegen des Roboters fehlgeschlagen"); } } - + //let the robots sort private void useRobot(){ System.out.println("Which robot do you want to use?"); listAllRobots(); System.out.print(" ID > "); int input = Integer.parseInt(sc.nextLine()); + // Change the searchForRobot Methode (safety) Robot r = fs.searchForRobot(input); System.out.println("You choose " + r.getName() + " of type " + r.getType()); System.out.println("Yout have following options"); diff --git a/utility/robot_exceptions/ExceptionStorage.java b/utility/robot_exceptions/ExceptionStorage.java index 289976c..103c1b9 100644 --- a/utility/robot_exceptions/ExceptionStorage.java +++ b/utility/robot_exceptions/ExceptionStorage.java @@ -24,13 +24,4 @@ public class ExceptionStorage implements Serializable { return this.message; } - /*public boolean emptyErrorStorage(){ - if(this.message != ""){ - this.message = ""; - this.date = LocalDateTime.now(); - return true; - }else{return false;} - - }*/ - } diff --git a/utility/robot_exceptions/RobotException.java b/utility/robot_exceptions/RobotException.java index e8d1485..241289b 100644 --- a/utility/robot_exceptions/RobotException.java +++ b/utility/robot_exceptions/RobotException.java @@ -15,6 +15,7 @@ public class RobotException extends Exception{ } @Override + // länge auffüllen für ebene Liste public String toString(){ return getMessage(this.currentType, this.name); }