implemnted serialisation and saving. First part of UI

main
Philipp3107 2023-01-03 01:18:57 +01:00
parent 0f217e8d6c
commit 3dac4fed82
37 changed files with 220 additions and 88 deletions

112
Main.java
View File

@ -1,69 +1,71 @@
import domain.*; import domain.*;
import facade.FactorySystem;
import safety.robot_exceptions.RobotException; import safety.robot_exceptions.RobotException;
import ui.UI; import ui.UI;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
FactorySystem fs = new FactorySystem("test_factory");
UI ui = new UI("test_factory"); 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();
// 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{ // try{
// String sorted = Herb.speak(input); // int[] sorted = Herb.think(input);
// System.out.println(sorted); // 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){ // }catch(RobotException re){
// System.out.println(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);
}
*/
} }
} }

View File

@ -4,9 +4,10 @@ import safety.robot_exceptions.ExceptionStorage;
import safety.robot_exceptions.RobotException; import safety.robot_exceptions.RobotException;
import safety.robot_exceptions.robotExceptions; import safety.robot_exceptions.robotExceptions;
public class C3PO extends RobotBasics { public class C3PO extends Robot {
public C3PO(int id, String name){ public C3PO(int id, String name){
super(id, name); super(id, name, "C3PO");
} }
/* public String ausgabe(int[] input) throws RobotException{ /* public String ausgabe(int[] input) throws RobotException{

View File

@ -1,9 +1,35 @@
package domain; package domain;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
public class Factory { public class Factory implements Serializable {
private HashMap<RobotBasics, Integer> robots = new HashMap<>(); private HashMap<Integer, Robot> robots = new HashMap<>();
private int c3poID = 0; private int c3poID = 0;
private int r2d2ID = 1000; private int r2d2ID = 1000;
public Factory(){
} }
//Has to return Collection<Robot>
public Collection<Robot> 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;
}
}

View File

@ -5,14 +5,16 @@ import safety.robot_exceptions.ExceptionStorage;
import safety.robot_exceptions.RobotException; import safety.robot_exceptions.RobotException;
import safety.robot_exceptions.robotExceptions; import safety.robot_exceptions.robotExceptions;
public class R2D2 extends RobotBasics { import java.io.Serializable;
public class R2D2 extends Robot {
/** /**
* *
* @param id> int * @param id> int
* @param name> String * @param name> String
*/ */
public R2D2(int id, String name){ public R2D2(int id, String name){
super(id, name); super(id, name, "R2D2");
} }

View File

@ -3,23 +3,26 @@ package domain;
import safety.robot_exceptions.ExceptionStorage; import safety.robot_exceptions.ExceptionStorage;
import safety.robot_exceptions.RobotException; import safety.robot_exceptions.RobotException;
import safety.robot_exceptions.robotExceptions; import safety.robot_exceptions.robotExceptions;
import safety.interfaces.Robot;
import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public abstract class RobotBasics implements Robot { public abstract class Robot implements safety.interfaces.Robot, Serializable {
protected ExceptionStorage exceptions; protected ExceptionStorage exceptions;
private int id; private int id;
private String name; private String name;
private boolean power; private boolean power;
public RobotBasics(int id, String name){ private String type;
public Robot(int id, String name, String type){
this.id = id; this.id = id;
this.name = name; this.name = name;
this.power = false; this.power = false;
this.exceptions = null; 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;
}
} }

View File

@ -1,19 +1,46 @@
package facade; package facade;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import domain.*; import domain.*;
import infrastructure.Persistenz; import infrastructure.Persistenz;
public class FactorySystem { public class FactorySystem {
private Factory factory;
private String factoryName;
private String name; public FactorySystem(String factoryName){
this.factoryName = factoryName;
public FactorySystem(String name){ if(Persistenz.existsSavedData(factoryName)){
if(Persistenz.existsSavedData(name)){ 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<Robot> 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;
}
} }

View File

@ -8,17 +8,22 @@ public class Persistenz {
return new File(name + FACTORY_DATA).exists(); return new File(name + FACTORY_DATA).exists();
} }
public static void saveFactoryData(Object Factory, String name) throws Exception{ public static void saveFactoryData(Object Factory, String name){
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name + FACTORY_DATA)); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name + FACTORY_DATA));
oos.writeObject(Factory); oos.writeObject(Factory);
oos.close(); oos.close();
}catch(Exception e){
}
} }
public static Object loadFactoryData(String name) throws Exception{ public static Object loadFactoryData(String name) throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name + FACTORY_DATA)); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name + FACTORY_DATA));
Object Factory = ois.readObject(); Object fac = ois.readObject();
ois.close(); ois.close();
return Factory; return fac;
} }
} }

View File

@ -26,8 +26,8 @@
* [Factrory](#-classe-factory-) * [Factrory](#-classe-factory-)
* ### [Infrastructure](#infratructure-1) * ### [Infrastructure](#infratructure-1)
* [Persistenz](#-classe-persistenz-) * [Persistenz](#-classe-persistenz-)
* ### [robot](#robot-1) * ### [safety](#robot-1)
* ### [exceptions](#exceptions-1) * ### [safety](#exceptions-1)
* [RobotException](#-class-robotexception-) * [RobotException](#-class-robotexception-)
* [RobotIllegalStateException](#-class-robotillegalstateexception-) * [RobotIllegalStateException](#-class-robotillegalstateexception-)
* [RobotMagicValueException](#-class-robotmagicvalueexception-) * [RobotMagicValueException](#-class-robotmagicvalueexception-)
@ -199,9 +199,9 @@ ___
`loadFactoryData():Object -> throws` `loadFactoryData():Object -> throws`
## robot ## safety
### exceptions ### safety
<h2 align="center"> <h2 align="center">
Class RobotException Class RobotException

Binary file not shown.

View File

@ -1,18 +1,19 @@
package ui; package ui;
import facade.Factory; 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 {
private Factory factory; private FactorySystem fs;
private String name; private String name;
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
public UI (Factory factory){ public UI (FactorySystem fs){
this.factory = factory; this.fs = fs;
hauptmenü(); hauptmenü();
} }
@ -20,16 +21,77 @@ public class UI {
this.name = name; this.name = name;
if(Persistenz.existsSavedData(name)){ if(Persistenz.existsSavedData(name)){
try{ try{
this.factory = (Factory) Persistenz.loadFactoryData(name); this.fs = (FactorySystem) Persistenz.loadFactoryData(name);
}catch(Exception e){ }catch(Exception e){
} }
}else{ }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");
}
}
} }