diff --git a/de.hhn.ai.prog2.blatt6.collections/.classpath b/de.hhn.ai.prog2.blatt6.collections/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/de.hhn.ai.prog2.blatt6.collections/.project b/de.hhn.ai.prog2.blatt6.collections/.project new file mode 100644 index 0000000..b4cbd0d --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/.project @@ -0,0 +1,17 @@ + + + de.hhn.ai.prog2.blatt6.collections + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt6.collections/.settings/org.eclipse.jdt.core.prefs b/de.hhn.ai.prog2.blatt6.collections/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0c68a61 --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/de.hhn.ai.prog2.blatt6.collections/.vscode/launch.json b/de.hhn.ai.prog2.blatt6.collections/.vscode/launch.json new file mode 100644 index 0000000..be6973c --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Debug (Launch) - Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "Debug (Launch)-App", + "request": "launch", + "mainClass": "app.App", + "projectName": "de.hhn.ai.prog2.blatt6.collections", + "console": "integratedTerminal", + } + ] +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt6.collections/src/app/App.java b/de.hhn.ai.prog2.blatt6.collections/src/app/App.java new file mode 100644 index 0000000..f439872 --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/src/app/App.java @@ -0,0 +1,79 @@ +package app; + +import java.util.Scanner; + +public class App { + + public static void main(String[] args) throws Exception { + Studentenverwaltung verwaltung = new Studentenverwaltung(); + Scanner scanner = new Scanner(System.in); + String[] inputData = new String[2]; + InputState inputState = InputState.DEFAULT; + + System.out.print("Herzlich Willkommen bei der Studentenverwaltung.\n" + + "Bitte verwenden Sie:\n list -> um alle Studenten anzuzeigen\n" + + " add -> um einen Studenten hinzuzufügen\n remove -> um einen Studenten zu entfernen\n" + + " quit -> um das Progrmam zu beenden\n> "); + + verwaltung.immatrikulieren("Mustermann", "Max", 123456); + + loop: while (scanner.hasNextLine()) { + String line = scanner.nextLine().toString(); + switch (inputState) { + case DEFAULT: + switch (line.toLowerCase()) { + case "q": + case "quit": + break loop; + case "list": + verwaltung.listeAusgeben(); + System.out.print("> "); + break; + case "add": + inputState = InputState.ADD1; + System.out.print("Nachname: "); + break; + case "remove": + inputState = InputState.REMOVE; + System.out.print("Matrikelnummer: "); + break; + default: + System.out.print("Fehler: Kommando exisitert nicht. Bitte versuchen Sie es erneut...\n> "); + break; + } + break; + case ADD1: + inputData[0] = line; + inputState = InputState.ADD2; + System.out.print("Vorname: "); + break; + case ADD2: + inputData[1] = line; + inputState = InputState.ADD3; + System.out.print("Matrikelnummer: "); + break; + case ADD3: + inputState = InputState.DEFAULT; + try { + verwaltung.immatrikulieren(inputData[0], inputData[1], Integer.parseInt(line)); + System.out.print("Student wurde erfolgreich immatrikuliert!\n> "); + } catch (IllegalArgumentException e) { + System.out + .print("Student konnte nicht immatrikuliert werden. Bitte versuchen Sie es erneut...\n> "); + } + break; + case REMOVE: + inputState = InputState.DEFAULT; + try { + verwaltung.exmatrikulieren(Integer.parseInt(line)); + System.out.print("Student wurde erfolgreich exmatrikuliert!\n> "); + } catch (IllegalArgumentException e) { + System.out + .print("Student konnte nicht exmatrikuliert werden. Bitte versuchen Sie es erneut...\n> "); + } + break; + } + } + scanner.close(); + } +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt6.collections/src/app/InputState.java b/de.hhn.ai.prog2.blatt6.collections/src/app/InputState.java new file mode 100644 index 0000000..cecc37b --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/src/app/InputState.java @@ -0,0 +1,5 @@ +package app; + +public enum InputState { + DEFAULT, ADD1, ADD2, ADD3, REMOVE; +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt6.collections/src/app/Studentenverwaltung.java b/de.hhn.ai.prog2.blatt6.collections/src/app/Studentenverwaltung.java new file mode 100644 index 0000000..b6e14b1 --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/src/app/Studentenverwaltung.java @@ -0,0 +1,23 @@ +package app; + +import java.util.HashMap; + +public class Studentenverwaltung { + private HashMap studis = new HashMap(); + + public void immatrikulieren(String name, String vorname, int matrikelnummer) { + if (studis.get(matrikelnummer) != null) + throw new IllegalArgumentException(); // geprüfte Exception ?? + else + studis.put(matrikelnummer, new Studi(name, vorname, matrikelnummer)); + } + + public void exmatrikulieren(int matrikelnummer) { + if (studis.remove(matrikelnummer) == null) + throw new IllegalArgumentException(); // Fehlermeldung ausgegeben ?? + } + + public void listeAusgeben() { + studis.forEach((k, v) -> System.out.println(k + ": " + v.getName() + " " + v.getLastName())); + } +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt6.collections/src/app/Studi.java b/de.hhn.ai.prog2.blatt6.collections/src/app/Studi.java new file mode 100644 index 0000000..139e343 --- /dev/null +++ b/de.hhn.ai.prog2.blatt6.collections/src/app/Studi.java @@ -0,0 +1,49 @@ +package app; + +public class Studi { + private String lastName; + private String name; + private int number; + + public Studi(String lastName, String name, int number) { + this.lastName = lastName; + this.name = name; + this.number = number; + } + + private boolean checkName(String name) { + if (name.length() < 2) + throw new IllegalArgumentException(); + return true; + } + + public String getLastName() { + return this.lastName; + } + + public void setLastName(String lastName) { + if (checkName(lastName)) + this.lastName = lastName; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + if (checkName(name)) + this.name = name; + } + + public int getNumber() { + return this.number; + } + + public void setNumber(int number) { + if (number > 99999 && number < 1000000) + this.number = number; + else + throw new IllegalArgumentException(); + } + +} \ No newline at end of file