Dies ist eine alte Version des Dokuments!


Mögliche Lösung zur Personendatenbank

Die Aufgabenstellung finden Sie hier.

hello.java
import pers.*;
 
import java.io.Console;
import java.lang.Integer;
 
public class hello
{
    public static void main ( String[] args )
    {
        pers.menu men = new pers.menu();
        while (men.main_menu()) {;}
        System.out.println("Und tschüss!\n");
    }
}
pers/datebase.java
package pers;
 
import java.util.*;
 
public class database
{   
    ArrayList<person> payload;
    public database()
    {
        payload=new ArrayList<person>();
    }
 
    public void add ( person p )
    {
        payload.add(p);
    }
 
    public void remove ( person p )
    {
        payload.remove(p);
    }
 
    public void remove ( int i )
    {
        payload.remove(i);
    }
 
    public void set ( person old, person p )
    {
        int i = payload.indexOf ( old );
        payload.set(i, p);
    }
 
    public pers.person get ( int index )
    {
        return payload.get(index);
    }
 
    public int size ()
    {
        return payload.size();
    }
 
    public void list ()
    {
        pers.person p;
        for (int i=0; i<payload.size(); i++)
        {
            p = payload.get ( i );
            System.out.println("Index: "+i);
            p.printInfo();
            System.out.println("");
        }
    }
}
pers/menu.java
package pers;
 
public class menu 
{
    private pers.database db;
    public menu()
    {
        this.db = new database();
    }
 
    public boolean main_menu()
    {
        System.out.println ( "1 Neuen Datensatz hinzufügen\n"+
                             "2 Alle Datensätze anzeigen\n"+
                             "3 Einen Datensatz ändern\n"+
                             "4 Einen Datensatz löschen\n"+
                             "5 Programm beenden");
        java.io.Console console = System.console();
        char choice = console.readLine ("Deine Wahl: ").charAt(0);
 
        if (choice=='1')
        {
            this.main_add();
        }
        else if (choice=='2')
        {
            this.show_all();
        }
        else if (choice=='3')
        {
            this.main_change();
        }
        else if (choice=='4')
        {
            this.main_delete();
        }
        else if (choice=='5')
        {
            return false;
        }
        else
        {
 
        }
        return true;
    }
 
    public void main_add()
    {
        java.io.Console console = System.console();
        String name = console.readLine ("Name: ");
        String adress = console.readLine ("Adress: ");
        int phone = java.lang.Integer.parseInt( console.readLine ("Phone: ") );
 
        pers.person myperson = new pers.person ( name, adress, phone );
        this.db.add ( myperson );
    }
 
    public void main_delete()
    {
        if (db.size() < 1)
            return; 
 
        this.show_all();
        java.io.Console console = System.console();
        int index = java.lang.Integer.parseInt( console.readLine("Welcher Index soll gelöscht werden? "));
 
        pers.person p = db.get( index );
 
        char choice = console.readLine("Sind Sie sicher, dass Eintrag '" + p.getName() + "' gelöscht werden soll? (y|n) ").charAt(0);
        if (choice=='y')
        {
            db.remove(index);
        }
    }
 
    public void main_change()
    {
        if (db.size() < 1)
            return; 
 
        this.show_all();
        java.io.Console console = System.console();
        int index = java.lang.Integer.parseInt( console.readLine("Welcher Index soll geändert werden? "));
 
        pers.person p = db.get( index );
        pers.person old=p;
 
        p.printInfo();
        String name = console.readLine("Neuer Name: ");
        String adress = console.readLine("Neue Adresse: ");
        String phone = console.readLine("Neue Phone-Nummer: ");
 
        if (!name.isEmpty())
            p.setName(name);
        if (!adress.isEmpty())
            p.setAdress(adress);
        if (!phone.isEmpty())
            p.setPhone( java.lang.Integer.parseInt( phone ) );
 
        db.set ( old, p );
    }
 
    public void show_all ()
    {
        db.list();
    }
}
pers/person.java
package pers;
 
public class person
{
    private String name;
    private String adress;
    private int phone;
    public person ( String nm, String adr, int ph )
    {
        this.name=nm;
        this.adress=adr;
        this.phone=ph;
    }
 
    public String getName()
    {
        return this.name;
    }
 
    public String getAdress()
    {
        return this.adress;
    }
 
    public int getPhone()
    {
        return this.phone;
    }
 
    public void setName( String nm )
    {
        this.name=nm;
    }
 
    public void setAdress ( String adr )
    {
        this.adress=adr;
    }
 
    public void setPhone ( int ph )
    {
        this.phone=ph;
    }
 
    public void printInfo ()
    {
        System.out.println ( "   Name: " + name + "\n"+
                             "   Adress: " + adress + "\n"+
                             "   Phone: " + phone);
    }
}

Makefile

CC=javac
TO=-d ../class

all:
	$(CC) $(TO) pers/database.java
	$(CC) $(TO) pers/person.java
	$(CC) $(TO) pers/menu.java
	$(CC) $(TO) -classpath ../class hello.java