Initl commit of exercise 1
This commit is contained in:
parent
a61dc4e7f2
commit
f1ed0953ef
8 changed files with 208 additions and 0 deletions
6
de.hhn.ai.prog2.blatt6.collections/.classpath
Normal file
6
de.hhn.ai.prog2.blatt6.collections/.classpath
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
de.hhn.ai.prog2.blatt6.collections/.project
Normal file
17
de.hhn.ai.prog2.blatt6.collections/.project
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<projectDescription>
|
||||
<name>de.hhn.ai.prog2.blatt6.collections</name>
|
||||
<comment/>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -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
|
22
de.hhn.ai.prog2.blatt6.collections/.vscode/launch.json
vendored
Normal file
22
de.hhn.ai.prog2.blatt6.collections/.vscode/launch.json
vendored
Normal file
|
@ -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<de.hhn.ai.prog2.blatt6.collections>",
|
||||
"request": "launch",
|
||||
"mainClass": "app.App",
|
||||
"projectName": "de.hhn.ai.prog2.blatt6.collections",
|
||||
"console": "integratedTerminal",
|
||||
}
|
||||
]
|
||||
}
|
79
de.hhn.ai.prog2.blatt6.collections/src/app/App.java
Normal file
79
de.hhn.ai.prog2.blatt6.collections/src/app/App.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package app;
|
||||
|
||||
public enum InputState {
|
||||
DEFAULT, ADD1, ADD2, ADD3, REMOVE;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package app;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Studentenverwaltung {
|
||||
private HashMap<Integer, Studi> studis = new HashMap<Integer, Studi>();
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
49
de.hhn.ai.prog2.blatt6.collections/src/app/Studi.java
Normal file
49
de.hhn.ai.prog2.blatt6.collections/src/app/Studi.java
Normal file
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue