my changes

main
Philipp3107 2023-01-10 00:32:43 +01:00
parent 8c130a8ddf
commit 6626d0c4ce
67 changed files with 318 additions and 227 deletions

View File

@ -1,5 +1,5 @@
import facade.FactorySystem;
import ui.UI;
import src.facade.FactorySystem;
import src.ui.UI;
public class Main {
public static void main(String[] args) {

View File

@ -1,15 +1,19 @@
package domain;
package src.domain;
import utility.robot_exceptions.ExceptionStorage;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.robotExceptions;
import src.utility.robot_exceptions.ExceptionStorage;
import src.utility.robot_exceptions.RobotException;
import src.utility.robot_exceptions.robotExceptions;
public class C3PO extends Robot {
public C3PO(int id, String name){
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
public String speak(int[] input) throws RobotException {
if(isPowerOn()){
@ -45,9 +49,13 @@ public class C3PO extends Robot {
this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
}
}
//Sorting then returning the input arr with insertion Sort
/**
* @param input
* @return the input arr after insertion Sort
* @throws RobotException
*/
@Override
public int[] think(int[] input) throws RobotException {
if(isPowerOn()){
@ -59,6 +67,3 @@ public class C3PO extends Robot {
}
}
}

View File

@ -1,4 +1,4 @@
package domain;
package src.domain;
import java.io.Serializable;
import java.util.Collection;
@ -13,13 +13,15 @@ public class Factory implements Serializable {
public Factory(){
}
//Has to return Collection<Robot>
/**
* @return Collection<Robot></Robot>
*/
public Collection<Robot> robotListToCollection(){
return robots.values();
}
//return a String arr with robot attributes
/**
* @return a String arr with robot attributes
*/
public String[] getRobotList() {
Collection<Robot> collect = robotListToCollection();
String[] list = new String[collect.size()];
@ -29,12 +31,16 @@ public class Factory implements Serializable {
}
return list;
}
//creates a new robot
public boolean buildNewRobot(String name, int type){
Robot r ;
if(type == 0) {
/**
* @param name
* @param type
* @return created or not
*/
public boolean buildNewRobot(String name, RobotType type){
Robot r;
if(type == RobotType.R2D2) {
r = new R2D2(r2d2ID++, name);
} else if(type == 1) {
} else if(type == RobotType.C3PO) {
r = new C3PO(c3poID++, name);
} else {
return false;
@ -43,7 +49,10 @@ public class Factory implements Serializable {
robots.put(r.getId(), r);
return true;
}
//returns a specific robot via id
/**
* @param id
* @return a specific robot via id
*/
public Robot getRobotOfList(int id){
return robots.get(id);
}

View File

@ -1,29 +1,35 @@
package domain;
import utility.robot_exceptions.ExceptionStorage;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.robotExceptions;
package src.domain;
import src.utility.robot_exceptions.ExceptionStorage;
import src.utility.robot_exceptions.RobotException;
import src.utility.robot_exceptions.robotExceptions;
public final class Nexus6 extends Robot {
private static final Nexus6 INSTANCE = new Nexus6();
private Nexus6() {
super(1, "Nexus-6", "Nexus-6");
super(1, "Nexus-6", RobotType.Nexus6);
}
public static Nexus6 getInstance() {
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
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
/**
* This method always throws an Illegalstate exception
* @param numbers Zahlen, die sortiert werden sollen.
* @throws RobotException
*/
@Override
public int[] think(int[] numbers) throws RobotException {
RobotException e = new RobotException(robotExceptions.ILLEGALSTATE, getName());

View File

@ -1,9 +1,9 @@
package domain;
package src.domain;
import utility.robot_exceptions.ExceptionStorage;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.robotExceptions;
import src.utility.interfaces.RobotInstructions;
import src.utility.robot_exceptions.ExceptionStorage;
import src.utility.robot_exceptions.RobotException;
import src.utility.robot_exceptions.robotExceptions;
public class R2D2 extends Robot {
/**
@ -15,7 +15,7 @@ public class R2D2 extends Robot {
}
/**
* @see utility.interfaces.RobotInstructions
* @see RobotInstructions
* Sorting then returning the input arr with selection sort
*/
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.
* @param input
* @return
* @return an sorted list
* @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 ','
*/
@Override

View File

@ -1,19 +1,16 @@
package domain;
package src.domain;
import utility.robot_exceptions.ExceptionStorage;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.robotExceptions;
import src.utility.robot_exceptions.ExceptionStorage;
import src.utility.robot_exceptions.RobotException;
import src.utility.robot_exceptions.robotExceptions;
import java.io.Serializable;
import java.util.Arrays;
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;
}
public abstract class Robot implements src.utility.interfaces.Robot, Serializable {
protected ExceptionStorage exceptions;
private int id;
private final String name;
@ -30,7 +27,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
}
/**
* @see utility.interfaces.RobotControl;
* @see RobotControl ;
*/
@Override
public int getId() {
@ -38,7 +35,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
}
/**
* @see utility.interfaces.RobotControl;
* @see RobotControl ;
*/
@Override
public String getName() {
@ -46,7 +43,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
}
/**
* @see utility.interfaces.RobotControl;
* @see RobotControl ;
*/
@Override
public void triggerPowerSwitch() {
@ -54,7 +51,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
}
/**
* @see utility.interfaces.RobotControl;
* @see RobotControl ;
*/
@Override
public boolean isPowerOn() {
@ -62,7 +59,7 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
}
/**
* @see utility.interfaces.RobotControl;
* @see RobotControl ;
*/
@Override
public RobotException getLastException() {
@ -79,12 +76,12 @@ 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){
if(x == 42){ return false; }
if (Arrays.stream(input).anyMatch(i -> i == 42)) {
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
}
return true;
}else{
@ -100,8 +97,6 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
* @return String (array as String)
* @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)
@ -113,14 +108,17 @@ public abstract class Robot implements utility.interfaces.Robot, Serializable {
throw robotexception;
}
}
public String getType(){
return this.type.toString();
public RobotType getType(){
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
public String toString(){
return "Name: " + name + "; ID: " + id + "; Type: " + type;
return "Name: " + name + "; ID: " + id + "; Type: " + type.toString();
}
}

View File

@ -0,0 +1,14 @@
package src.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;
}
}

View File

@ -1,8 +1,8 @@
package facade;
import domain.Factory;
import domain.Robot;
package src.facade;
import src.domain.Factory;
import src.domain.Robot;
import src.domain.RobotType;
import infrastructure.Persistenz;
public class FactorySystem {
@ -26,12 +26,14 @@ public class FactorySystem {
}
//provide robot attributes
/**
* @return all Robots
*/
public String[] getAllRobots(){
return factory.getRobotList();
}
//Creating a new robot
public boolean buildNewRobot(String name, int type) {
public boolean buildNewRobot(String name, RobotType type) {
boolean check = factory.buildNewRobot(name, type);
if(check) {
try {
@ -42,7 +44,11 @@ public class FactorySystem {
}
return check;
}
//provides a specific robot
/**
*
* @param id
* @return a specific robot
*/
public Robot searchForRobot(int id){
return factory.getRobotOfList(id);
}

144
src/ui/UI.java 100644
View File

@ -0,0 +1,144 @@
package src.ui;
import src.domain.Robot;
import src.domain.RobotType;
import src.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");
}
}
}
}

View File

@ -1,5 +1,5 @@
/* (c) 2012 Thomas Smits */
package utility.interfaces;
package src.utility.interfaces;
/**
@ -10,6 +10,6 @@ package utility.interfaces;
public interface Robot extends RobotControl, RobotInstructions{
// keine eigenen Methoden
// Don't write methods here
}

View File

@ -1,5 +1,5 @@
package utility.interfaces;
import utility.robot_exceptions.RobotException;
package src.utility.interfaces;
import src.utility.robot_exceptions.RobotException;
/**
* Das Interface repräsentiert einen einfachen Roboter mit seinen Funktionen.

View File

@ -1,8 +1,8 @@
package utility.interfaces;
package src.utility.interfaces;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.RobotIllegalStateException;
import utility.robot_exceptions.RobotMagicValueException;
import src.utility.robot_exceptions.RobotException;
import src.utility.robot_exceptions.RobotIllegalStateException;
import src.utility.robot_exceptions.RobotMagicValueException;
/**

View File

@ -1,4 +1,4 @@
package utility.robot_exceptions;
package src.utility.robot_exceptions;
public class ArrayEmptyException extends RobotException{
public ArrayEmptyException(robotExceptions type,String errorMessage){

View File

@ -1,4 +1,4 @@
package utility.robot_exceptions;
package src.utility.robot_exceptions;
import java.io.Serializable;
import java.time.LocalDateTime;
@ -14,7 +14,9 @@ public class ExceptionStorage implements Serializable {
LocalDateTime now = LocalDateTime.now();
this.date = now;
}
/**
* @param message of the exception
*/
public void saveLatestErrorMessage(RobotException message){
this.message = message;
this.date = LocalDateTime.now();
@ -23,5 +25,4 @@ public class ExceptionStorage implements Serializable {
public RobotException getLastErrorMessage(){
return this.message;
}
}

View File

@ -1,4 +1,4 @@
package utility.robot_exceptions;
package src.utility.robot_exceptions;
public class RobotException extends Exception{
robotExceptions currentType;
@ -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){
return name + " " + types.getMessage();
}

View File

@ -1,7 +1,11 @@
package utility.robot_exceptions;
package src.utility.robot_exceptions;
public class RobotIllegalStateException extends RobotException{
/**
* @param type
* @param errormessage
*/
public RobotIllegalStateException(robotExceptions type, String errormessage){
super(type, errormessage);
}

View File

@ -1,6 +1,10 @@
package utility.robot_exceptions;
package src.utility.robot_exceptions;
public class RobotMagicValueException extends RobotException {
/**
* @param type
* @param errormessage
*/
public RobotMagicValueException(robotExceptions type, String errormessage) {
super(type, errormessage);
}

View File

@ -1,4 +1,4 @@
package utility.robot_exceptions;
package src.utility.robot_exceptions;
public enum robotExceptions {
ILLEGALSTATE("ist in einem illegalen Zustand"),

Binary file not shown.

View File

@ -1,57 +1,57 @@
package domain;
package tests.tests;
import src.domain.C3PO;
import org.junit.jupiter.api.BeforeEach;
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.*;
class C3POTest {
C3PO Herbert;
C3PO herbert;
int id = 0;
String name = "Herbert";
@BeforeEach
void setup(){
Herbert = new C3PO(id, name);
herbert = new C3PO(id, name);
}
//Tests for basic functions
@Test
void getId() {
assertEquals(id, Herbert.getId());
assertEquals(id, herbert.getId());
}
@Test
void getName() {
assertEquals(name, Herbert.getName());
assertEquals(name, herbert.getName());
assertEquals(name,
Herbert.getName());
herbert.getName());
}
@Test
void triggerPowerSwitch() {
Herbert.triggerPowerSwitch();
assertTrue(Herbert.isPowerOn());
herbert.triggerPowerSwitch();
assertTrue(herbert.isPowerOn());
}
@Test
void isPowerOn() {
assertFalse(Herbert.isPowerOn());
Herbert.triggerPowerSwitch();
assertTrue(Herbert.isPowerOn());
assertFalse(herbert.isPowerOn());
herbert.triggerPowerSwitch();
assertTrue(herbert.isPowerOn());
}
@Test
void speak(){
Herbert.triggerPowerSwitch();
herbert.triggerPowerSwitch();
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};
String array = "";
try{
array = Herbert.speak(input);
array = herbert.speak(input);
}catch(RobotException re){
System.out.println(re);
}
@ -65,13 +65,13 @@ class C3POTest {
int[] input = { 4, 5, 12, 2, 4, 7, 56, 433, 23};
int[] value = new int[9];
try{
value = Herbert.think(input);
value = herbert.think(input);
}catch(RobotException re){
System.out.println(re);
}
Herbert.triggerPowerSwitch();
herbert.triggerPowerSwitch();
try{
value = Herbert.think(input);
value = herbert.think(input);
}catch(RobotException re){
System.out.println(re);
}
@ -81,19 +81,22 @@ class C3POTest {
}
}
@Test
void thinkTestMagicNumberException() {
assertThrows(RobotMagicValueException.class, () -> herbert.think(new int[]{42}));
}
@Test
void magicValueException(){
int[] input = {3,2,42};
Herbert.triggerPowerSwitch();
herbert.triggerPowerSwitch();
String expectedMessage = "Herbert has an unknown error. Code 42.";
try{
int[] solution = Herbert.think(input);
int[] solution = herbert.think(input);
}catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
}
try{
String test = Herbert.speak(input);
String test = herbert.speak(input);
}catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
}
@ -104,12 +107,12 @@ class C3POTest {
int[] input = {3,2,42};
String expectedMessage = "Herbert is turned off.";
try{
int[] solution = Herbert.think(input);
int[] solution = herbert.think(input);
}catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
}
try{
String test = Herbert.speak(input);
String test = herbert.speak(input);
}catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
}
@ -119,16 +122,16 @@ class C3POTest {
@Test
void arrayEmptyException(){
String expectedMessage = "Herbert got an empty array.";
Herbert.triggerPowerSwitch();
herbert.triggerPowerSwitch();
try{
int[] solution = Herbert.think(new int[0]);
int[] solution = herbert.think(new int[0]);
}catch(RobotException re){
System.out.println(re);
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
}
try{
String test = Herbert.speak(new int[0]);
String test = herbert.speak(new int[0]);
}catch(RobotException re){
System.out.println(re);
assertEquals(0, expectedMessage.compareTo(re.getMessage()));

View File

@ -1,5 +1,6 @@
package domain;
package tests;
import src.domain.R2D2;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.*;
@ -18,7 +19,6 @@ class R2D2Test {
//Tests for basic functions
@Test
void getId() {
assertEquals(id, Herbert.getId());
}

View File

@ -1,108 +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){
}
}
}