implemnted serialisation and saving. First part of UI
parent
0f217e8d6c
commit
3dac4fed82
114
Main.java
114
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);
|
||||
}
|
||||
|
||||
*/
|
||||
//
|
||||
// */
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -1,9 +1,35 @@
|
|||
package domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Factory {
|
||||
private HashMap<RobotBasics, Integer> robots = new HashMap<>();
|
||||
public class Factory implements Serializable {
|
||||
private HashMap<Integer, Robot> robots = new HashMap<>();
|
||||
private int c3poID = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,17 +8,22 @@ public class Persistenz {
|
|||
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));
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -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
|
||||
|
||||
<h2 align="center">
|
||||
Class RobotException
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
76
ui/UI.java
76
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue