diff --git a/Main.java b/Main.java index a67aecf..7b9e807 100644 --- a/Main.java +++ b/Main.java @@ -1,69 +1,71 @@ import domain.*; +import facade.FactorySystem; import safety.robot_exceptions.RobotException; import ui.UI; public class Main { public static void main(String[] args) { + FactorySystem fs = new FactorySystem("test_factory"); - UI ui = new UI("test_factory"); - - 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(); - + UI ui = new UI(fs); +// 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(); +// +// +//// try{ +//// String sorted = Herb.speak(input); +//// System.out.println(sorted); +//// } catch (RobotException re) { +//// System.out.println(re); +//// } +// // try{ -// String sorted = Herb.speak(input); -// System.out.println(sorted); -// } catch (RobotException re) { +// int[] sorted = Herb.think(input); +// for(int i = 0; i < sorted.length; i++){ +// System.out.print(" " + sorted[i]); +// } +// }catch(RobotException re){ +// re.printStackTrace(); +// } +// +// System.out.println("last exception thrown"); +// 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){ +// e.getLocalizedMessage(); +// } +// System.out.println("last exception thrown"); +// re = Herb.getLastException().toString(); +// System.out.println(re); +// +// //System.out.println("Was neues ausgeben"); +// +// //just some testing +// /*C3PO Herbert = new C3PO(0, "Herbert"); +// int[] input = {6,5,4,3,2,1}; +// Herbert.triggerPowerSwitch(); +// try{ +// String asString = Herbert.speak(input); +// System.out.println(asString); +// }catch(RobotException re){ // System.out.println(re); // } - - try{ - int[] sorted = Herb.think(input); - for(int i = 0; i < sorted.length; i++){ - System.out.print(" " + sorted[i]); - } - }catch(RobotException re){ - re.printStackTrace(); - } - - System.out.println("last exception thrown"); - 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){ - e.getLocalizedMessage(); - } - System.out.println("last exception thrown"); - re = Herb.getLastException().toString(); - System.out.println(re); - - //System.out.println("Was neues ausgeben"); - - //just some testing - /*C3PO Herbert = new C3PO(0, "Herbert"); - int[] input = {6,5,4,3,2,1}; - Herbert.triggerPowerSwitch(); - try{ - String asString = Herbert.speak(input); - System.out.println(asString); - }catch(RobotException re){ - System.out.println(re); - } - - */ +// +// */ } } diff --git a/domain/C3PO.java b/domain/C3PO.java index 339e645..90f49e0 100644 --- a/domain/C3PO.java +++ b/domain/C3PO.java @@ -4,9 +4,10 @@ import safety.robot_exceptions.ExceptionStorage; import safety.robot_exceptions.RobotException; import safety.robot_exceptions.robotExceptions; -public class C3PO extends RobotBasics { +public class C3PO extends Robot { public C3PO(int id, String name){ - super(id, name); + super(id, name, "C3PO"); + } /* public String ausgabe(int[] input) throws RobotException{ diff --git a/domain/Factory.java b/domain/Factory.java index 0cf6adb..4c6130f 100644 --- a/domain/Factory.java +++ b/domain/Factory.java @@ -1,9 +1,35 @@ package domain; +import java.io.Serializable; +import java.util.Collection; import java.util.HashMap; -public class Factory { - private HashMap robots = new HashMap<>(); +public class Factory implements Serializable { + private HashMap robots = new HashMap<>(); private int c3poID = 0; private int r2d2ID = 1000; -} + + public Factory(){ + + } + + //Has to return Collection + public Collection getRobotList(){ + return robots.values(); + } + + public boolean buildNewRobot(String name, int type){ + Robot r ; + if(type == 0){ + r = new R2D2(r2d2ID++, name); + }else if(type == 1){ + r = new C3PO(c3poID++, name); + }else{ + return false; + } + + robots.put(r.getId(), r); + return true; + } + } + diff --git a/domain/R2D2.java b/domain/R2D2.java index 9279b9e..035e4f3 100644 --- a/domain/R2D2.java +++ b/domain/R2D2.java @@ -5,14 +5,16 @@ import safety.robot_exceptions.ExceptionStorage; import safety.robot_exceptions.RobotException; import safety.robot_exceptions.robotExceptions; -public class R2D2 extends RobotBasics { +import java.io.Serializable; + +public class R2D2 extends Robot { /** * * @param id> int * @param name> String */ public R2D2(int id, String name){ - super(id, name); + super(id, name, "R2D2"); } diff --git a/domain/RobotBasics.java b/domain/Robot.java similarity index 93% rename from domain/RobotBasics.java rename to domain/Robot.java index 202c1ab..a85404e 100644 --- a/domain/RobotBasics.java +++ b/domain/Robot.java @@ -3,23 +3,26 @@ package domain; import safety.robot_exceptions.ExceptionStorage; import safety.robot_exceptions.RobotException; import safety.robot_exceptions.robotExceptions; -import safety.interfaces.Robot; +import java.io.Serializable; import java.util.Arrays; import java.util.stream.Collectors; -public abstract class RobotBasics implements Robot { +public abstract class Robot implements safety.interfaces.Robot, Serializable { protected ExceptionStorage exceptions; private int id; private String name; private boolean power; - public RobotBasics(int id, String name){ + private String type; + + public Robot(int id, String name, String type){ this.id = id; this.name = name; this.power = false; this.exceptions = null; + this.type = type; } /** @@ -168,5 +171,9 @@ public abstract class RobotBasics implements Robot { } + @Override + public String toString(){ + return "Name: " + name + "; ID: " + id + "; Type: " + type; + } } diff --git a/facade/FactorySystem.java b/facade/FactorySystem.java index f887e8d..6ec5f3e 100644 --- a/facade/FactorySystem.java +++ b/facade/FactorySystem.java @@ -1,19 +1,46 @@ package facade; +import java.util.Collection; import java.util.HashMap; import domain.*; import infrastructure.Persistenz; public class FactorySystem { + private Factory factory; + private String factoryName; - private String name; - - public FactorySystem(String name){ - if(Persistenz.existsSavedData(name)){ - + public FactorySystem(String factoryName){ + this.factoryName = factoryName; + if(Persistenz.existsSavedData(factoryName)){ + try{ + this.factory = (Factory) Persistenz.loadFactoryData(factoryName); + } catch (Exception e) { + System.out.println("Loading of old factory not possible"); + System.out.println(e.getCause()); + } + }else{ + this.factory = new Factory(); } } + 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; + } + + public boolean buildNewRobot(String name, int type){ + boolean check = factory.buildNewRobot(name, type); + if(check) { + Persistenz.saveFactoryData(factory, factoryName); + } + return check; + } + } diff --git a/infrastructure/Persistenz.java b/infrastructure/Persistenz.java index 51e9b3f..c50b74c 100644 --- a/infrastructure/Persistenz.java +++ b/infrastructure/Persistenz.java @@ -8,17 +8,22 @@ public class Persistenz { return new File(name + FACTORY_DATA).exists(); } - public static void saveFactoryData(Object Factory, String name) throws Exception{ - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name + FACTORY_DATA)); - oos.writeObject(Factory); - oos.close(); + public static void saveFactoryData(Object Factory, String name){ + try{ + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name + FACTORY_DATA)); + oos.writeObject(Factory); + oos.close(); + }catch(Exception e){ + + } + } public static Object loadFactoryData(String name) throws Exception{ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name + FACTORY_DATA)); - Object Factory = ois.readObject(); + Object fac = ois.readObject(); ois.close(); - return Factory; + return fac; } } diff --git a/out/production/Robot_Factory_PR/Main.class b/out/production/Robot_Factory_PR/Main.class index 2afee34..27517b0 100644 Binary files a/out/production/Robot_Factory_PR/Main.class and b/out/production/Robot_Factory_PR/Main.class differ diff --git a/out/production/Robot_Factory_PR/README.md b/out/production/Robot_Factory_PR/README.md index 260ddad..43a3495 100644 --- a/out/production/Robot_Factory_PR/README.md +++ b/out/production/Robot_Factory_PR/README.md @@ -26,8 +26,8 @@ * [Factrory](#-classe-factory-) * ### [Infrastructure](#infratructure-1) * [Persistenz](#-classe-persistenz-) -* ### [robot](#robot-1) - * ### [exceptions](#exceptions-1) +* ### [safety](#robot-1) + * ### [safety](#exceptions-1) * [RobotException](#-class-robotexception-) * [RobotIllegalStateException](#-class-robotillegalstateexception-) * [RobotMagicValueException](#-class-robotmagicvalueexception-) @@ -199,9 +199,9 @@ ___ `loadFactoryData():Object -> throws` -## robot +## safety -### exceptions +### safety

Class RobotException diff --git a/out/production/Robot_Factory_PR/domain/C3PO.class b/out/production/Robot_Factory_PR/domain/C3PO.class index a432459..2107338 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/C3POTest.class b/out/production/Robot_Factory_PR/domain/C3POTest.class index 81076ce..ec5b81c 100644 Binary files a/out/production/Robot_Factory_PR/domain/C3POTest.class and b/out/production/Robot_Factory_PR/domain/C3POTest.class differ diff --git a/out/production/Robot_Factory_PR/domain/Factory.class b/out/production/Robot_Factory_PR/domain/Factory.class new file mode 100644 index 0000000..41206cd Binary files /dev/null and b/out/production/Robot_Factory_PR/domain/Factory.class differ diff --git a/out/production/Robot_Factory_PR/domain/R2D2.class b/out/production/Robot_Factory_PR/domain/R2D2.class index 4668be1..d93fbf9 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.class b/out/production/Robot_Factory_PR/domain/Robot.class new file mode 100644 index 0000000..37b3a75 Binary files /dev/null and b/out/production/Robot_Factory_PR/domain/Robot.class differ diff --git a/out/production/Robot_Factory_PR/domain/RobotBasics.class b/out/production/Robot_Factory_PR/domain/RobotBasics.class deleted file mode 100644 index 28b7021..0000000 Binary files a/out/production/Robot_Factory_PR/domain/RobotBasics.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/facade/Factory.class b/out/production/Robot_Factory_PR/facade/Factory.class deleted file mode 100644 index fe3da3e..0000000 Binary files a/out/production/Robot_Factory_PR/facade/Factory.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/facade/FactorySystem.class b/out/production/Robot_Factory_PR/facade/FactorySystem.class new file mode 100644 index 0000000..2e404a2 Binary files /dev/null and b/out/production/Robot_Factory_PR/facade/FactorySystem.class differ diff --git a/out/production/Robot_Factory_PR/infrastructure/Persistenz.class b/out/production/Robot_Factory_PR/infrastructure/Persistenz.class index b173611..38db354 100644 Binary files a/out/production/Robot_Factory_PR/infrastructure/Persistenz.class and b/out/production/Robot_Factory_PR/infrastructure/Persistenz.class differ diff --git a/out/production/Robot_Factory_PR/robot/exceptions/ArrayEmptyException.class b/out/production/Robot_Factory_PR/robot/exceptions/ArrayEmptyException.class deleted file mode 100644 index a22a604..0000000 Binary files a/out/production/Robot_Factory_PR/robot/exceptions/ArrayEmptyException.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/robot/exceptions/RobotIllegalStateException.class b/out/production/Robot_Factory_PR/robot/exceptions/RobotIllegalStateException.class deleted file mode 100644 index e7988e5..0000000 Binary files a/out/production/Robot_Factory_PR/robot/exceptions/RobotIllegalStateException.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/robot/exceptions/RobotMagicValueException.class b/out/production/Robot_Factory_PR/robot/exceptions/RobotMagicValueException.class deleted file mode 100644 index 3bc7be9..0000000 Binary files a/out/production/Robot_Factory_PR/robot/exceptions/RobotMagicValueException.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/robot/interfaces/Robot.class b/out/production/Robot_Factory_PR/robot/interfaces/Robot.class deleted file mode 100644 index 3b56f20..0000000 Binary files a/out/production/Robot_Factory_PR/robot/interfaces/Robot.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/robot/interfaces/RobotControl.class b/out/production/Robot_Factory_PR/robot/interfaces/RobotControl.class deleted file mode 100644 index f47ab02..0000000 Binary files a/out/production/Robot_Factory_PR/robot/interfaces/RobotControl.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/robot/interfaces/RobotInstructions.class b/out/production/Robot_Factory_PR/robot/interfaces/RobotInstructions.class deleted file mode 100644 index ab7d6de..0000000 Binary files a/out/production/Robot_Factory_PR/robot/interfaces/RobotInstructions.class and /dev/null differ diff --git a/out/production/Robot_Factory_PR/safety/interfaces/Robot.class b/out/production/Robot_Factory_PR/safety/interfaces/Robot.class new file mode 100644 index 0000000..abedae1 Binary files /dev/null and b/out/production/Robot_Factory_PR/safety/interfaces/Robot.class differ diff --git a/out/production/Robot_Factory_PR/safety/interfaces/RobotControl.class b/out/production/Robot_Factory_PR/safety/interfaces/RobotControl.class new file mode 100644 index 0000000..bf76523 Binary files /dev/null and b/out/production/Robot_Factory_PR/safety/interfaces/RobotControl.class differ diff --git a/out/production/Robot_Factory_PR/safety/interfaces/RobotInstructions.class b/out/production/Robot_Factory_PR/safety/interfaces/RobotInstructions.class new file mode 100644 index 0000000..78060e1 Binary files /dev/null and b/out/production/Robot_Factory_PR/safety/interfaces/RobotInstructions.class differ diff --git a/out/production/Robot_Factory_PR/safety/robot_exceptions/ArrayEmptyException.class b/out/production/Robot_Factory_PR/safety/robot_exceptions/ArrayEmptyException.class new file mode 100644 index 0000000..1e5de9f Binary files /dev/null and b/out/production/Robot_Factory_PR/safety/robot_exceptions/ArrayEmptyException.class differ diff --git a/out/production/Robot_Factory_PR/robot/exceptions/ExceptionStorage.class b/out/production/Robot_Factory_PR/safety/robot_exceptions/ExceptionStorage.class similarity index 52% rename from out/production/Robot_Factory_PR/robot/exceptions/ExceptionStorage.class rename to out/production/Robot_Factory_PR/safety/robot_exceptions/ExceptionStorage.class index 5f07ef7..d643427 100644 Binary files a/out/production/Robot_Factory_PR/robot/exceptions/ExceptionStorage.class and b/out/production/Robot_Factory_PR/safety/robot_exceptions/ExceptionStorage.class differ diff --git a/out/production/Robot_Factory_PR/robot/exceptions/RobotException.class b/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotException.class similarity index 55% rename from out/production/Robot_Factory_PR/robot/exceptions/RobotException.class rename to out/production/Robot_Factory_PR/safety/robot_exceptions/RobotException.class index 89c4054..c3c0717 100644 Binary files a/out/production/Robot_Factory_PR/robot/exceptions/RobotException.class and b/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotException.class differ diff --git a/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotIllegalStateException.class b/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotIllegalStateException.class new file mode 100644 index 0000000..207e044 Binary files /dev/null and b/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotIllegalStateException.class differ diff --git a/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotMagicValueException.class b/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotMagicValueException.class new file mode 100644 index 0000000..4d7d141 Binary files /dev/null and b/out/production/Robot_Factory_PR/safety/robot_exceptions/RobotMagicValueException.class differ diff --git a/out/production/Robot_Factory_PR/robot/exceptions/robotExceptions.class b/out/production/Robot_Factory_PR/safety/robot_exceptions/robotExceptions.class similarity index 57% rename from out/production/Robot_Factory_PR/robot/exceptions/robotExceptions.class rename to out/production/Robot_Factory_PR/safety/robot_exceptions/robotExceptions.class index 577d7f1..f848019 100644 Binary files a/out/production/Robot_Factory_PR/robot/exceptions/robotExceptions.class and b/out/production/Robot_Factory_PR/safety/robot_exceptions/robotExceptions.class differ diff --git a/out/production/Robot_Factory_PR/test_factoryFactory.ser b/out/production/Robot_Factory_PR/test_factoryFactory.ser new file mode 100644 index 0000000..9e07453 Binary files /dev/null and b/out/production/Robot_Factory_PR/test_factoryFactory.ser differ diff --git a/out/production/Robot_Factory_PR/ui/UI.class b/out/production/Robot_Factory_PR/ui/UI.class index 51a0402..0cda82e 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/test_factoryFactory.ser b/test_factoryFactory.ser new file mode 100644 index 0000000..3648c0e Binary files /dev/null and b/test_factoryFactory.ser differ diff --git a/ui/UI.java b/ui/UI.java index a0a5271..6317070 100644 --- a/ui/UI.java +++ b/ui/UI.java @@ -1,18 +1,19 @@ package ui; -import facade.Factory; +import facade.FactorySystem; import infrastructure.Persistenz; +import java.sql.SQLOutput; import java.util.Scanner; public class UI { - private Factory factory; + private FactorySystem fs; private String name; Scanner sc = new Scanner(System.in); - public UI (Factory factory){ - this.factory = factory; + public UI (FactorySystem fs){ + this.fs = fs; hauptmenü(); } @@ -20,16 +21,77 @@ public class UI { this.name = name; if(Persistenz.existsSavedData(name)){ try{ - this.factory = (Factory) Persistenz.loadFactoryData(name); + this.fs = (FactorySystem) Persistenz.loadFactoryData(name); }catch(Exception e){ } }else{ - this.factory = new Factory(); + this.fs = new FactorySystem(name); } } - public void hauptmenü(){ + private void hauptmenü() { + mainloop: + while(true){ + System.out.println(); + System.out.println("______________________"); + System.out.println("Sie haben folgende optionen:"); + System.out.println("-1- show all robots ----"); + System.out.println("-2- build new robot ----"); + System.out.println("-3- empty --------------"); + System.out.println("-4- Exit ---------------"); + System.out.print(" > "); + try{ + int input = Integer.parseInt(sc.nextLine()); + switch(input){ + case 1: + listAllRobots();break; + case 2: buildNewRobot();break; + case 3: System.out.println("u pressed 3");break; + case 4: break mainloop; + default: + System.out.println("this is an invalid option"); break; + } + + }catch(NumberFormatException e){ + System.out.println("invalid input"); + } + } + + System.out.println("Good Bye"); + } + + private void listAllRobots(){ + String[] listOfAll = fs.getAllRobots(); + if(listOfAll.length > 0){ + System.out.println("These robtos exist right now:"); + for (String s: listOfAll) { + System.out.println(s); + } + }else{ + System.out.println("There are no robots yet."); + } } + + private void buildNewRobot(){ + System.out.println("What shall the name of the robot be?"); + System.out.print(" > "); + String name = sc.nextLine(); + System.out.println("Which type of robot do u want?"); + System.out.println(" [0] for R2D2 and [1] for C3PO"); + System.out.print(" > "); + 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; + } + }else{ + System.out.println("Anlegen des Roboters fehlgeschlagen"); + } + } }