D
DonPromillo
Gast
Hallo,
ich hab ein Java Programm geschrieben, doch ich komme nicht wieter, es geht um eine Namensverwaltung, bei der man Namen hinzufügen, löschen und ändern kann. Als letztes soll man sie nach dem Alphabetsortieren können, doch ich weis nicht wie es geht. Kann mir jemadn helfen?
Programm:
ich hab ein Java Programm geschrieben, doch ich komme nicht wieter, es geht um eine Namensverwaltung, bei der man Namen hinzufügen, löschen und ändern kann. Als letztes soll man sie nach dem Alphabetsortieren können, doch ich weis nicht wie es geht. Kann mir jemadn helfen?
Programm:
Code:
package Namensverwaltung;
import support.*;
public class projekt1 {
public static void main(String[] args) throws Exception {
// Deklaration
String[] name = null;
char wahl = 0;
name = init();
do {
wahl = menuewahl();
switch (wahl) {
case 'a':
case 'A':
ausgabe(name);
break;
case 'h':
case 'H':
name = hinzufügen(name);
break;
case 'p':
case 'P':
programmende();
break;
case 's':
case 'S':
suchen(name);
break;
case 'ä':
case 'Ä':
ändern(name);
break;
case 'l':
case 'L':
name = löschen(name);
break;
default:
Console.println("ERROR");
}
} while (wahl != 'p' && wahl != 'P');
}
public static void ausgabe(String[] b) {
if (b != null) {
for (int i = 0; i < b.length; i++) {
Console.println(b[i]);
}
} else {
Console.println("Keine Felder mehr");
}
}
public static String[] init() {
String[] b = null;
b = new String[6];
b[0] = "Dante";
b[1] = "Cloud";
b[2] = "Tina";
b[3] = "Tifa";
b[4] = "Tidus";
b[5] = "Yuna";
return b;
}
public static char menuewahl() throws Exception {
char wahl;
Console.println("Menüwahl");
Console.println("Ausgabe=a");
Console.println("Hinzufügen=h");
Console.println("Sortieren=o");
Console.println("Suchen=s");
Console.println("Ändern=ä");
Console.println("Löschen=l");
Console.println("Programmende=q");
Console.print("Ihre Wahl = ");
wahl = Console.readChar();
return wahl;
}
public static void programmende() {
// Programmende
Console.println("\n\t\t\t* P r o g r a m m e n d e *");
}
public static String[] hinzufügen(String[] b) throws Exception {
String[] feld = null;
String newname;
if (b == null) {
Console.println("Neuer Name");
newname = Console.readString();
feld = new String[1];
feld[0] = newname;
} else {
feld = new String[b.length + 1];
for (int i = 0; i < b.length; i++) {
feld[i] = b[i];
}
Console.println("Neuer Name");
newname = Console.readString();
feld[b.length] = newname;
}
return feld;
}
public static void suchen(String[] b) throws Exception {
String sname;
Console.println("Welche Name möchten sie suchen");
sname = Console.readString();
if (such(b, sname) >= 0) {
Console.println("Name ist auf Position = " + (such(b, sname)+1));
} else {
Console.println("Keine Name gefunden");
}
}
public static int such(String[] b, String sname) {
int find = -1;
for (int i = 0; i < b.length; i++) {
if (b[i].toUpperCase().equals(sname.toUpperCase())) {
find = i;
}
}
return find;
}
public static void ändern(String[] b) throws Exception {
String sname;
int pos;
Console.println("Welche Name möchten sie änder");
sname = Console.readString();
pos = such(b, sname);
if (pos >= 0) {
Console.println("Neuer Name");
b[pos] = Console.readString();
} else {
Console.println("Keine Name gefunden");
}
}
public static String[] löschen(String[] b) throws Exception {
String sname;
int pos;
int i = 0;
String[] newfeld = null;
if (b != null) {
Console.println("Welche Name möchten sie löschen");
sname = Console.readString();
pos = such(b, sname);
if (pos == 0 && b.length == 1) {
newfeld = null;
} else if (pos >= 0) {
b[pos] = null;
newfeld = new String[b.length - 1];
for (int j = 0; i < b.length; i++) {
if (i != pos) {
newfeld[j] = b[i];
j++;
}
}
} else {
Console.println("Keine Name gefunden");
newfeld = b;
}
} else {
Console.println("Keine Felder mehr");
}
return newfeld;
}
}