corrected language consistency, Javadoc comments, Enumeration, Lambda expressions, UI Code, Updated project structure
parent
4162f6df07
commit
b5475b051c
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/src/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
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.
|
@ -6,10 +6,14 @@ import utility.robot_exceptions.robotExceptions;
|
||||||
|
|
||||||
public class C3PO extends Robot {
|
public class C3PO extends Robot {
|
||||||
public C3PO(int id, String name){
|
public C3PO(int id, String name){
|
||||||
super(id, name, "C3PO");
|
super(id, name, RobotType.C3PO);
|
||||||
|
|
||||||
}
|
}
|
||||||
//returns the sorted list as String with the delimiter ';'
|
/**
|
||||||
|
* @param input from the user
|
||||||
|
* @return the sorted list as String with the delimiter ';'
|
||||||
|
* @throws RobotException
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String speak(int[] input) throws RobotException {
|
public String speak(int[] input) throws RobotException {
|
||||||
if(isPowerOn()){
|
if(isPowerOn()){
|
||||||
|
@ -45,9 +49,13 @@ public class C3PO extends Robot {
|
||||||
this.exceptions = new ExceptionStorage(robotexception);
|
this.exceptions = new ExceptionStorage(robotexception);
|
||||||
throw robotexception;
|
throw robotexception;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//Sorting then returning the input arr with insertion Sort
|
|
||||||
|
/**
|
||||||
|
* @param input
|
||||||
|
* @return the input arr after insertion Sort
|
||||||
|
* @throws RobotException
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int[] think(int[] input) throws RobotException {
|
public int[] think(int[] input) throws RobotException {
|
||||||
if(isPowerOn()){
|
if(isPowerOn()){
|
||||||
|
@ -58,7 +66,4 @@ public class C3PO extends Robot {
|
||||||
throw robotException;
|
throw robotException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,13 +13,15 @@ public class Factory implements Serializable {
|
||||||
public Factory(){
|
public Factory(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
//Has to return Collection<Robot>
|
* @return Collection<Robot></Robot>
|
||||||
|
*/
|
||||||
public Collection<Robot> robotListToCollection(){
|
public Collection<Robot> robotListToCollection(){
|
||||||
return robots.values();
|
return robots.values();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
//return a String arr with robot attributes
|
* @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()];
|
||||||
|
@ -29,12 +31,16 @@ public class Factory implements Serializable {
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
//creates a new robot
|
/**
|
||||||
public boolean buildNewRobot(String name, int type){
|
* @param name
|
||||||
Robot r ;
|
* @param type
|
||||||
if(type == 0) {
|
* @return created or not
|
||||||
|
*/
|
||||||
|
public boolean buildNewRobot(String name, RobotType type){
|
||||||
|
Robot r;
|
||||||
|
if(type == RobotType.R2D2) {
|
||||||
r = new R2D2(r2d2ID++, name);
|
r = new R2D2(r2d2ID++, name);
|
||||||
} else if(type == 1) {
|
} else if(type == RobotType.C3PO) {
|
||||||
r = new C3PO(c3poID++, name);
|
r = new C3PO(c3poID++, name);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -43,7 +49,10 @@ public class Factory implements Serializable {
|
||||||
robots.put(r.getId(), r);
|
robots.put(r.getId(), r);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//returns a specific robot via id
|
/**
|
||||||
|
* @param id
|
||||||
|
* @return a specific robot via id
|
||||||
|
*/
|
||||||
public Robot getRobotOfList(int id){
|
public Robot getRobotOfList(int id){
|
||||||
return robots.get(id);
|
return robots.get(id);
|
||||||
}
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
package domain;
|
package domain;
|
||||||
|
|
||||||
import utility.robot_exceptions.ExceptionStorage;
|
import utility.robot_exceptions.ExceptionStorage;
|
||||||
import utility.robot_exceptions.RobotException;
|
import utility.robot_exceptions.RobotException;
|
||||||
import utility.robot_exceptions.robotExceptions;
|
import utility.robot_exceptions.robotExceptions;
|
||||||
|
@ -9,21 +8,28 @@ public final class Nexus6 extends Robot {
|
||||||
private static final Nexus6 INSTANCE = new Nexus6();
|
private static final Nexus6 INSTANCE = new Nexus6();
|
||||||
|
|
||||||
private Nexus6() {
|
private Nexus6() {
|
||||||
super(1, "Nexus-6", "Nexus-6");
|
super(1, "Nexus-6", RobotType.Nexus6);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Nexus6 getInstance() {
|
public static Nexus6 getInstance() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
//This method always throws an Illegalstate exception
|
* This method always throws an Illegalstate exception
|
||||||
|
* @param numbers Zahlen, die ausgegeben werden sollen.
|
||||||
|
* @throws RobotException
|
||||||
|
*/
|
||||||
@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
|
/**
|
||||||
|
* This method always throws an Illegalstate exception
|
||||||
|
* @param numbers Zahlen, die sortiert werden sollen.
|
||||||
|
* @throws RobotException
|
||||||
|
*/
|
||||||
@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());
|
|
@ -1,6 +1,6 @@
|
||||||
package domain;
|
package domain;
|
||||||
|
|
||||||
|
import utility.interfaces.RobotInstructions;
|
||||||
import utility.robot_exceptions.ExceptionStorage;
|
import utility.robot_exceptions.ExceptionStorage;
|
||||||
import utility.robot_exceptions.RobotException;
|
import utility.robot_exceptions.RobotException;
|
||||||
import utility.robot_exceptions.robotExceptions;
|
import utility.robot_exceptions.robotExceptions;
|
||||||
|
@ -11,11 +11,11 @@ public class R2D2 extends Robot {
|
||||||
* @param name> String
|
* @param name> String
|
||||||
*/
|
*/
|
||||||
public R2D2(int id, String name){
|
public R2D2(int id, String name){
|
||||||
super(id, name, "R2D2");
|
super(id, name, RobotType.R2D2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotInstructions
|
* @see RobotInstructions
|
||||||
* Sorting then returning the input arr with selection sort
|
* Sorting then returning the input arr with selection sort
|
||||||
*/
|
*/
|
||||||
public int[] think(int[] input) throws RobotException {
|
public int[] think(int[] input) throws RobotException {
|
||||||
|
@ -31,7 +31,7 @@ public class R2D2 extends Robot {
|
||||||
/**
|
/**
|
||||||
* Sorts any given array of integers with the selection Sort algorithm.
|
* Sorts any given array of integers with the selection Sort algorithm.
|
||||||
* @param input
|
* @param input
|
||||||
* @return
|
* @return an sorted list
|
||||||
* @throws RobotException
|
* @throws RobotException
|
||||||
*/
|
*/
|
||||||
public int[] selectionSort(int[] input) throws RobotException{
|
public int[] selectionSort(int[] input) throws RobotException{
|
||||||
|
@ -57,7 +57,7 @@ public class R2D2 extends Robot {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotInstructions
|
* @see RobotInstructions
|
||||||
* returns the sorted list as String with the delimiter ','
|
* returns the sorted list as String with the delimiter ','
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
|
@ -1,5 +1,5 @@
|
||||||
package domain;
|
package domain;
|
||||||
|
import utility.interfaces.RobotControl;
|
||||||
import utility.robot_exceptions.ExceptionStorage;
|
import utility.robot_exceptions.ExceptionStorage;
|
||||||
import utility.robot_exceptions.RobotException;
|
import utility.robot_exceptions.RobotException;
|
||||||
import utility.robot_exceptions.robotExceptions;
|
import utility.robot_exceptions.robotExceptions;
|
||||||
|
@ -10,18 +10,15 @@ 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 final String name;
|
private final String name;
|
||||||
private boolean power;
|
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.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.power = false;
|
this.power = false;
|
||||||
|
@ -30,7 +27,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotControl;
|
* @see RobotControl ;
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int getId() {
|
public int getId() {
|
||||||
|
@ -38,7 +35,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotControl;
|
* @see RobotControl ;
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -46,7 +43,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotControl;
|
* @see RobotControl ;
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void triggerPowerSwitch() {
|
public void triggerPowerSwitch() {
|
||||||
|
@ -54,7 +51,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotControl;
|
* @see RobotControl ;
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean isPowerOn() {
|
public boolean isPowerOn() {
|
||||||
|
@ -62,7 +59,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see utility.interfaces.RobotControl;
|
* @see RobotControl ;
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public RobotException getLastException() {
|
public RobotException getLastException() {
|
||||||
|
@ -79,12 +76,12 @@ 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){
|
if (Arrays.stream(input).anyMatch(i -> i == 42)) {
|
||||||
if(x == 42){ return false; }
|
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
|
||||||
|
this.exceptions = new ExceptionStorage(robotexception);
|
||||||
|
throw robotexception;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}else{
|
}else{
|
||||||
|
@ -100,8 +97,6 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
* @return String (array as String)
|
* @return String (array as String)
|
||||||
* @throws RobotException
|
* @throws RobotException
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Write an array with a delimiter to the command line
|
|
||||||
public String output(int[] input, String delemiter)throws RobotException{
|
public String output(int[] input, String delemiter)throws RobotException{
|
||||||
if(checkArray(input)) {
|
if(checkArray(input)) {
|
||||||
return Arrays.stream(input)
|
return Arrays.stream(input)
|
||||||
|
@ -113,14 +108,17 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
||||||
throw robotexception;
|
throw robotexception;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public String getType(){
|
|
||||||
|
public RobotType getType(){
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override the to String method to get a clean looking outcome
|
/**
|
||||||
|
* Override the to String method to get a clean looking outcome
|
||||||
|
* @return {@link String}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString(){
|
public String toString(){
|
||||||
return "Name: " + name + "; ID: " + id + "; Type: " + type;
|
return "Name: " + name + "; ID: " + id + "; Type: " + type.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package domain;
|
||||||
|
public enum RobotType {
|
||||||
|
C3PO("C3PO"), R2D2("R2D2"), Nexus6("Nexus6");
|
||||||
|
private final String type;
|
||||||
|
private RobotType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package facade;
|
||||||
|
|
||||||
import domain.Factory;
|
import domain.Factory;
|
||||||
import domain.Robot;
|
import domain.Robot;
|
||||||
|
import domain.RobotType;
|
||||||
import infrastructure.Persistenz;
|
import infrastructure.Persistenz;
|
||||||
|
|
||||||
public class FactorySystem {
|
public class FactorySystem {
|
||||||
|
@ -25,12 +26,14 @@ public class FactorySystem {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//provide robot attributes
|
/**
|
||||||
|
* @return all Robots
|
||||||
|
*/
|
||||||
public String[] getAllRobots(){
|
public String[] getAllRobots(){
|
||||||
return factory.getRobotList();
|
return factory.getRobotList();
|
||||||
}
|
}
|
||||||
//Creating a new robot
|
//Creating a new robot
|
||||||
public boolean buildNewRobot(String name, int type) {
|
public boolean buildNewRobot(String name, RobotType type) {
|
||||||
boolean check = factory.buildNewRobot(name, type);
|
boolean check = factory.buildNewRobot(name, type);
|
||||||
if(check) {
|
if(check) {
|
||||||
try {
|
try {
|
||||||
|
@ -41,7 +44,11 @@ public class FactorySystem {
|
||||||
}
|
}
|
||||||
return check;
|
return check;
|
||||||
}
|
}
|
||||||
//provides a specific robot
|
/**
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return a specific robot
|
||||||
|
*/
|
||||||
public Robot searchForRobot(int id){
|
public Robot searchForRobot(int id){
|
||||||
return factory.getRobotOfList(id);
|
return factory.getRobotOfList(id);
|
||||||
}
|
}
|
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.
|
@ -0,0 +1,144 @@
|
||||||
|
package ui;
|
||||||
|
|
||||||
|
import domain.Robot;
|
||||||
|
import domain.RobotType;
|
||||||
|
import facade.FactorySystem;
|
||||||
|
import infrastructure.Persistenz;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class UI {
|
||||||
|
|
||||||
|
private FactorySystem fs;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
public UI (FactorySystem fs){
|
||||||
|
this.fs = fs;
|
||||||
|
hauptmenü();
|
||||||
|
|
||||||
|
}
|
||||||
|
public UI (String name){
|
||||||
|
this.name = name;
|
||||||
|
if(Persistenz.existsSavedData(name)){
|
||||||
|
try{
|
||||||
|
this.fs = (FactorySystem) Persistenz.loadFactoryData(name);
|
||||||
|
}catch(Exception ignored){
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
this.fs = new FactorySystem(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* starting screen
|
||||||
|
*/
|
||||||
|
private void hauptmenü() {
|
||||||
|
mainloop:
|
||||||
|
while(true){
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("_______________________________");
|
||||||
|
System.out.println("Sie haben folgende optionen: ");
|
||||||
|
System.out.println("-1- -- Alle Roboter anzeigen --");
|
||||||
|
System.out.println("-2- -- Erzeuge neuen Roboter --");
|
||||||
|
System.out.println("-3- ----- Roboter benutzen ----");
|
||||||
|
System.out.println("-4- ---------- Exit -----------");
|
||||||
|
System.out.print(" > ");
|
||||||
|
//User options
|
||||||
|
try{
|
||||||
|
int input = Integer.parseInt(sc.nextLine());
|
||||||
|
switch(input){
|
||||||
|
case 1:
|
||||||
|
listAllRobots();break;
|
||||||
|
case 2: buildNewRobot();break;
|
||||||
|
case 3: useRobot();break;
|
||||||
|
case 4: break mainloop;
|
||||||
|
default:
|
||||||
|
System.out.println("Keine valide Option auswahl"); break;
|
||||||
|
}
|
||||||
|
}catch(NumberFormatException e){
|
||||||
|
System.out.println("Kein valider Input");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Auf Wiedersehen");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* display all existing robots
|
||||||
|
*/
|
||||||
|
private void listAllRobots(){
|
||||||
|
String[] listOfAll = fs.getAllRobots();
|
||||||
|
if(listOfAll.length > 0){
|
||||||
|
System.out.println("Diese Roboter existieren bereits:");
|
||||||
|
for (String s: listOfAll) {
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
System.out.println("Es wurden noch keine Roboter erzeugt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* create a new robot
|
||||||
|
*/
|
||||||
|
private void buildNewRobot(){
|
||||||
|
System.out.println("Wie soll der neue Roboter heißen");
|
||||||
|
System.out.print(" > ");
|
||||||
|
String name = sc.nextLine();
|
||||||
|
System.out.println("Welche Art Roboter wollen Sie");
|
||||||
|
System.out.println(" [0] für R2D2 und [1] für C3PO");
|
||||||
|
System.out.print(" > ");
|
||||||
|
//user enters which type of robot he wants
|
||||||
|
int in = Integer.parseInt(sc.nextLine());
|
||||||
|
RobotType type = null;
|
||||||
|
if(in == 0) {
|
||||||
|
type = RobotType.R2D2;
|
||||||
|
} else if(in == 1) {
|
||||||
|
type = RobotType.C3PO;
|
||||||
|
}
|
||||||
|
if(fs.buildNewRobot(name, type)){
|
||||||
|
switch (type) {
|
||||||
|
case R2D2 -> System.out.println("Neuer Roboter vom typ R2D2 mit dem Namen " + name + " wurde erzeugt");
|
||||||
|
case C3PO -> System.out.println("Neuer Roboter vom typ C3PO mit dem Namen " + name + " wurde erzeugt");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
System.out.println("Anlegen des Roboters fehlgeschlagen");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* let the robots sort
|
||||||
|
*/
|
||||||
|
private void useRobot(){
|
||||||
|
System.out.println("Welchen Roboter wollen Sie verwenden");
|
||||||
|
listAllRobots();
|
||||||
|
System.out.print(" ID > ");
|
||||||
|
int idInput = Integer.parseInt(sc.nextLine());
|
||||||
|
// Change the searchForRobot Methode (safety)
|
||||||
|
Robot r = fs.searchForRobot(idInput);
|
||||||
|
System.out.println("Du hast " + r.getName() + " gewählt der vom Typ " + r.getType() + " ist");
|
||||||
|
mainloop:
|
||||||
|
while(true) {
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("_______________________________");
|
||||||
|
System.out.println("Sie haben folgende optionen: ");
|
||||||
|
System.out.println("-1- --- An oder Ausschalten ---");
|
||||||
|
System.out.println("-2- -- Sortieren einer Liste --");
|
||||||
|
System.out.println("-3- ---------- Exit -----------");
|
||||||
|
System.out.print(" > ");
|
||||||
|
//User options
|
||||||
|
try {
|
||||||
|
int input = Integer.parseInt(sc.nextLine());
|
||||||
|
switch (input) {
|
||||||
|
case 1:
|
||||||
|
listAllRobots();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
buildNewRobot();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Keine valide Option auswahl"); break;
|
||||||
|
}
|
||||||
|
}catch(NumberFormatException e) {
|
||||||
|
System.out.println("Kein valider Input");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,6 @@ package utility.interfaces;
|
||||||
|
|
||||||
|
|
||||||
public interface Robot extends RobotControl, RobotInstructions{
|
public interface Robot extends RobotControl, RobotInstructions{
|
||||||
// keine eigenen Methoden
|
// Don't write methods here
|
||||||
|
|
||||||
}
|
}
|
|
@ -14,7 +14,9 @@ public class ExceptionStorage implements Serializable {
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
this.date = now;
|
this.date = now;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @param message of the exception
|
||||||
|
*/
|
||||||
public void saveLatestErrorMessage(RobotException message){
|
public void saveLatestErrorMessage(RobotException message){
|
||||||
this.message = message;
|
this.message = message;
|
||||||
this.date = LocalDateTime.now();
|
this.date = LocalDateTime.now();
|
||||||
|
@ -23,14 +25,4 @@ public class ExceptionStorage implements Serializable {
|
||||||
public RobotException getLastErrorMessage(){
|
public RobotException getLastErrorMessage(){
|
||||||
return this.message;
|
return this.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public boolean emptyErrorStorage(){
|
|
||||||
if(this.message != ""){
|
|
||||||
this.message = "";
|
|
||||||
this.date = LocalDateTime.now();
|
|
||||||
return true;
|
|
||||||
}else{return false;}
|
|
||||||
|
|
||||||
}*/
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -10,6 +10,11 @@ public class RobotException extends Exception{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param types
|
||||||
|
* @param name
|
||||||
|
* @return a combined String of both
|
||||||
|
*/
|
||||||
private static String getMessage(robotExceptions types, String name){
|
private static String getMessage(robotExceptions types, String name){
|
||||||
return name + " " + types.getMessage();
|
return name + " " + types.getMessage();
|
||||||
}
|
}
|
|
@ -2,6 +2,10 @@ package utility.robot_exceptions;
|
||||||
|
|
||||||
public class RobotIllegalStateException extends RobotException{
|
public class RobotIllegalStateException extends RobotException{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param type
|
||||||
|
* @param errormessage
|
||||||
|
*/
|
||||||
public RobotIllegalStateException(robotExceptions type, String errormessage){
|
public RobotIllegalStateException(robotExceptions type, String errormessage){
|
||||||
super(type, errormessage);
|
super(type, errormessage);
|
||||||
}
|
}
|
|
@ -1,6 +1,10 @@
|
||||||
package utility.robot_exceptions;
|
package utility.robot_exceptions;
|
||||||
|
|
||||||
public class RobotMagicValueException extends RobotException {
|
public class RobotMagicValueException extends RobotException {
|
||||||
|
/**
|
||||||
|
* @param type
|
||||||
|
* @param errormessage
|
||||||
|
*/
|
||||||
public RobotMagicValueException(robotExceptions type, String errormessage) {
|
public RobotMagicValueException(robotExceptions type, String errormessage) {
|
||||||
super(type, errormessage);
|
super(type, errormessage);
|
||||||
}
|
}
|
Binary file not shown.
|
@ -1,57 +1,57 @@
|
||||||
package domain;
|
package tests;
|
||||||
|
import src.domain.C3PO;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import utility.robot_exceptions.RobotException;
|
import src.utility.robot_exceptions.RobotException;
|
||||||
|
import src.utility.robot_exceptions.RobotMagicValueException;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class C3POTest {
|
class C3POTest {
|
||||||
|
|
||||||
C3PO Herbert;
|
C3PO herbert;
|
||||||
int id = 0;
|
int id = 0;
|
||||||
String name = "Herbert";
|
String name = "Herbert";
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup(){
|
void setup(){
|
||||||
Herbert = new C3PO(id, name);
|
herbert = new C3PO(id, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Tests for basic functions
|
//Tests for basic functions
|
||||||
@Test
|
@Test
|
||||||
void getId() {
|
void getId() {
|
||||||
|
assertEquals(id, herbert.getId());
|
||||||
assertEquals(id, Herbert.getId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getName() {
|
void getName() {
|
||||||
assertEquals(name, Herbert.getName());
|
assertEquals(name, herbert.getName());
|
||||||
assertEquals(name,
|
assertEquals(name,
|
||||||
Herbert.getName());
|
herbert.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void triggerPowerSwitch() {
|
void triggerPowerSwitch() {
|
||||||
Herbert.triggerPowerSwitch();
|
herbert.triggerPowerSwitch();
|
||||||
assertTrue(Herbert.isPowerOn());
|
assertTrue(herbert.isPowerOn());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void isPowerOn() {
|
void isPowerOn() {
|
||||||
assertFalse(Herbert.isPowerOn());
|
assertFalse(herbert.isPowerOn());
|
||||||
Herbert.triggerPowerSwitch();
|
herbert.triggerPowerSwitch();
|
||||||
assertTrue(Herbert.isPowerOn());
|
assertTrue(herbert.isPowerOn());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void speak(){
|
void speak(){
|
||||||
Herbert.triggerPowerSwitch();
|
herbert.triggerPowerSwitch();
|
||||||
String solution = "12; 2; 4; 5; 12; 2; 4; 7; 56; 433; 23";
|
String solution = "12; 2; 4; 5; 12; 2; 4; 7; 56; 433; 23";
|
||||||
int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23};
|
int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23};
|
||||||
String array = "";
|
String array = "";
|
||||||
try{
|
try{
|
||||||
array = Herbert.speak(input);
|
array = herbert.speak(input);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
System.out.println(re);
|
System.out.println(re);
|
||||||
}
|
}
|
||||||
|
@ -65,13 +65,13 @@ class C3POTest {
|
||||||
int[] input = { 4, 5, 12, 2, 4, 7, 56, 433, 23};
|
int[] input = { 4, 5, 12, 2, 4, 7, 56, 433, 23};
|
||||||
int[] value = new int[9];
|
int[] value = new int[9];
|
||||||
try{
|
try{
|
||||||
value = Herbert.think(input);
|
value = herbert.think(input);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
System.out.println(re);
|
System.out.println(re);
|
||||||
}
|
}
|
||||||
Herbert.triggerPowerSwitch();
|
herbert.triggerPowerSwitch();
|
||||||
try{
|
try{
|
||||||
value = Herbert.think(input);
|
value = herbert.think(input);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
System.out.println(re);
|
System.out.println(re);
|
||||||
}
|
}
|
||||||
|
@ -81,19 +81,22 @@ class C3POTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
void thinkTestMagicNumberException() {
|
||||||
|
assertThrows(RobotMagicValueException.class, () -> herbert.think(new int[]{42}));
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
void magicValueException(){
|
void magicValueException(){
|
||||||
int[] input = {3,2,42};
|
int[] input = {3,2,42};
|
||||||
Herbert.triggerPowerSwitch();
|
herbert.triggerPowerSwitch();
|
||||||
String expectedMessage = "Herbert has an unknown error. Code 42.";
|
String expectedMessage = "Herbert has an unknown error. Code 42.";
|
||||||
try{
|
try{
|
||||||
int[] solution = Herbert.think(input);
|
int[] solution = herbert.think(input);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||||
}
|
}
|
||||||
try{
|
try{
|
||||||
String test = Herbert.speak(input);
|
String test = herbert.speak(input);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||||
}
|
}
|
||||||
|
@ -104,12 +107,12 @@ class C3POTest {
|
||||||
int[] input = {3,2,42};
|
int[] input = {3,2,42};
|
||||||
String expectedMessage = "Herbert is turned off.";
|
String expectedMessage = "Herbert is turned off.";
|
||||||
try{
|
try{
|
||||||
int[] solution = Herbert.think(input);
|
int[] solution = herbert.think(input);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||||
}
|
}
|
||||||
try{
|
try{
|
||||||
String test = Herbert.speak(input);
|
String test = herbert.speak(input);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||||
}
|
}
|
||||||
|
@ -119,16 +122,16 @@ class C3POTest {
|
||||||
@Test
|
@Test
|
||||||
void arrayEmptyException(){
|
void arrayEmptyException(){
|
||||||
String expectedMessage = "Herbert got an empty array.";
|
String expectedMessage = "Herbert got an empty array.";
|
||||||
Herbert.triggerPowerSwitch();
|
herbert.triggerPowerSwitch();
|
||||||
try{
|
try{
|
||||||
int[] solution = Herbert.think(new int[0]);
|
int[] solution = herbert.think(new int[0]);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
System.out.println(re);
|
System.out.println(re);
|
||||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
String test = Herbert.speak(new int[0]);
|
String test = herbert.speak(new int[0]);
|
||||||
}catch(RobotException re){
|
}catch(RobotException re){
|
||||||
System.out.println(re);
|
System.out.println(re);
|
||||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
|
@ -1,5 +1,6 @@
|
||||||
package domain;
|
package tests;
|
||||||
|
|
||||||
|
import src.domain.R2D2;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
@ -18,7 +19,6 @@ class R2D2Test {
|
||||||
//Tests for basic functions
|
//Tests for basic functions
|
||||||
@Test
|
@Test
|
||||||
void getId() {
|
void getId() {
|
||||||
|
|
||||||
assertEquals(id, Herbert.getId());
|
assertEquals(id, Herbert.getId());
|
||||||
}
|
}
|
||||||
|
|
106
ui/UI.java
106
ui/UI.java
|
@ -1,106 +0,0 @@
|
||||||
package ui;
|
|
||||||
|
|
||||||
import domain.Robot;
|
|
||||||
import facade.FactorySystem;
|
|
||||||
import infrastructure.Persistenz;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class UI {
|
|
||||||
|
|
||||||
private FactorySystem fs;
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
public UI (FactorySystem fs){
|
|
||||||
this.fs = fs;
|
|
||||||
hauptmenü();
|
|
||||||
|
|
||||||
}
|
|
||||||
public UI (String name){
|
|
||||||
this.name = name;
|
|
||||||
if(Persistenz.existsSavedData(name)){
|
|
||||||
try{
|
|
||||||
this.fs = (FactorySystem) Persistenz.loadFactoryData(name);
|
|
||||||
}catch(Exception ignored){
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
this.fs = new FactorySystem(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//starting screen
|
|
||||||
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- ------- use robot ------");
|
|
||||||
System.out.println("-4- --------- Exit ---------");
|
|
||||||
System.out.print(" > ");
|
|
||||||
//User options
|
|
||||||
try{
|
|
||||||
int input = Integer.parseInt(sc.nextLine());
|
|
||||||
switch(input){
|
|
||||||
case 1:
|
|
||||||
listAllRobots();break;
|
|
||||||
case 2: buildNewRobot();break;
|
|
||||||
case 3: useRobot();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");
|
|
||||||
}
|
|
||||||
//display all existing robots
|
|
||||||
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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//create a new robot
|
|
||||||
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(" > ");
|
|
||||||
//user enters which type of robot he wants
|
|
||||||
int type = Integer.parseInt(sc.nextLine());
|
|
||||||
if(fs.buildNewRobot(name, type)){
|
|
||||||
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");
|
|
||||||
mainloop:
|
|
||||||
while(true){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue