109 lines
3.5 KiB
Java
109 lines
3.5 KiB
Java
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){
|
|
|
|
}
|
|
}
|
|
}
|