package aufgabe.nr026.adressbuch;
import java.util.Iterator;
import java.util.List;
public class AdressbuchTest {
public AdressbuchTest() {
test1();
test2();
test3();
}
public void test1(){
Adressbuch adressbuch = new Adressbuch("f:\\",".adresse");
try {
Adresse adresse = adressbuch.getNeueAdresse();
adresse.setName("Test");
List adressen = adressbuch.getAdressen();
boolean adresseGefunden = false;
Iterator iter = adressen.iterator();
while (!adresseGefunden && iter.hasNext()) {
Adresse adr = (Adresse) iter.next();
String name = adr.getName();
if (name != null){
//test auf namens-gleichheit
adresseGefunden = name.equals(adresse.getName());
}
}
assertTrue("Test1", adresseGefunden);
} catch (AdresseException e) {
fail("Text 1: Exception: " + e.getMessage());
}
}
public void test2(){
Adressbuch adressbuch = new Adressbuch("f:\\",".adresse");
try {
Adresse adresse = adressbuch.getNeueAdresse();
adresse.setName("Test");
String id = adresse.getId();
//adressNuch neu instanzieren
adressbuch = new Adressbuch("f:\\",".adresse");
List adressen = adressbuch.getAdressen();
boolean adresseGefunden = false;
Iterator iter = adressen.iterator();
while (!adresseGefunden && iter.hasNext()) {
Adresse adr = (Adresse) iter.next();
String name = adr.getName();
if (name != null){
//test auf namens- und id-gleichheit
adresseGefunden = name.equals(adresse.getName()) && adresse.getId().equals(id);
}
}
assertTrue("Test2", adresseGefunden);
} catch (AdresseException e) {
fail("Text 2: Exception: " + e.getMessage());
}
}
public void test3(){
CdSammlung cdSammlung = new CdSammlung("f:\\",".cdsammlung");
try {
Cd cd2 = cdSammlung.getNeueCdsammlung();
cd2.setTitel("CDTitel");
List cds = cdSammlung.getCdSammlung();
boolean adresseGefunden = false;
Iterator iter = cds.iterator();
while (!adresseGefunden && iter.hasNext()) {
Cd cd1 = (Cd) iter.next();
String titel = cd1.getTitel();
if (titel != null){
//test auf namens-gleichheit
adresseGefunden = titel.equals(cd2.getTitel());
}
}
assertTrue("TestCD", adresseGefunden);
} catch (AdresseException e) {
fail("Text 1: Exception: " + e.getMessage());
}
}
public void assertTrue(String message, boolean assertArg){
if (!assertArg){
System.out.print("FEHLER!: ");
}else{
System.out.print("Test ok: ");
}
System.out.println(message);
}
public void fail(String message){
System.out.println("FEHLER!: " + message);
}
public static void main(String[] args) {
new AdressbuchTest();
}
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
import java.io.File;
import java.util.List;
public class Adressbuch extends RecordContainer {
public Adressbuch(String pAdressVerzeichnis, String endung){
super (pAdressVerzeichnis, endung);
}
public Adresse getNeueAdresse() throws AdresseException{
return (Adresse) getNewRecord();
}
public Records getInstance(File file)throws AdresseException {
return (Records)new Adresse(file);
}
public List getAdressen(){
return records;
}
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
import java.io.File;
import java.io.FilenameFilter;
public class AdressbuchFilenameFilter implements FilenameFilter {
//konstruktor erstellen damit fileendung mitgegeben werden kann.
public String dEndung;
public AdressbuchFilenameFilter(String dateiEndung){
dEndung = dateiEndung;
}
public boolean accept(File dir, String name) {
return name.endsWith(dEndung); //name.endsWith(Adressbuch.dateiEndung);
}
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
import java.io.File;
public class Adresse extends Records {
private static final transient String NAME = "name";
private static final transient String VORNAME = "vorname";
private static final transient String STRASSE = "strasse";
private static final transient String PLZ = "plz";
private static final transient String ORT = "ort";
private static final transient String TELEFON = "telefon";
protected Adresse(File pFile) throws AdresseException {
super (pFile);
}
public String getName(){
return properties.getProperty(NAME);
}
public void setName(String wert) throws AdresseException{
properties.setProperty(NAME, wert);
speichern();
}
public String getVorname(){
return properties.getProperty(VORNAME);
}
public void setVorname(String wert) throws AdresseException{
properties.setProperty(VORNAME, wert);
speichern();
}
public String getStrasse(){
return properties.getProperty(STRASSE);
}
public void setStrasse(String wert) throws AdresseException{
properties.setProperty(STRASSE, wert);
speichern();
}
public String getPLZ(){
return properties.getProperty(PLZ);
}
public void setPLZ(String wert) throws AdresseException{
properties.setProperty(PLZ, wert);
speichern();
}
public String getOrt(){
return properties.getProperty(ORT);
}
public void setOrt(String wert) throws AdresseException{
properties.setProperty(ORT, wert);
speichern();
}
public String getTelefon(){
return properties.getProperty(TELEFON);
}
public void setTelefon(String wert) throws AdresseException{
properties.setProperty(TELEFON, wert);
speichern();
}
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
public class AdresseException extends Exception {
private static final long serialVersionUID = 1L;
public AdresseException() {
super();
}
public AdresseException(String arg0) {
super(arg0);
}
public AdresseException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
public AdresseException(Throwable arg0) {
super(arg0);
}
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
import java.io.File;
public class Cd extends Records{
private static final transient String TITEL = "titel";
private static final transient String INTERPRET = "interpret";
private static final transient String GENERE = "genere";
private static final transient String JAHR = "jahr";
protected Cd(File pFile) throws AdresseException {
//nach oben kop und mit super() diesen aufrufen
super (pFile);
}
public String getTitel(){
return properties.getProperty(TITEL);
}
public void setTitel(String wert) throws AdresseException{
properties.setProperty(TITEL, wert);
speichern();
}
public String getInterpret(){
return properties.getProperty(INTERPRET);
}
public void setInterpret(String wert) throws AdresseException{
properties.setProperty(INTERPRET, wert);
speichern();
}
public String getGenere(){
return properties.getProperty(GENERE);
}
public void setGenere(String wert) throws AdresseException{
properties.setProperty(GENERE, wert);
speichern();
}
public String getJahr(){
return properties.getProperty(JAHR);
}
public void setJahr(String wert) throws AdresseException{
properties.setProperty(JAHR, wert);
speichern();
}
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
import java.io.File;
//mport java.io.IOException
import java.util.List;
public class CdSammlung extends RecordContainer {
public CdSammlung(String pCDVerzeichnis,String endung){
super (pCDVerzeichnis,endung);
}
public Cd getNeueCdsammlung() throws AdresseException{
//super(adresse);
return (Cd) getNewRecord();
}
public Records getInstance(File file)throws AdresseException
{
return new Cd(file);
}
public List getCdSammlung(){
return records;
}
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
abstract class RecordContainer {
public String recordVerzeichnis = null;
public ArrayList records = null;
public int aktuelleId = 0;
public String dateiEndung;
public RecordContainer(String pAdressVerzeichnis, String endung) {
dateiEndung = endung;
if (pAdressVerzeichnis!=null){
recordVerzeichnis = pAdressVerzeichnis;
File verzeichnis = new File(pAdressVerzeichnis);
if (verzeichnis.isDirectory()){
File[] dateien = verzeichnis.listFiles(new AdressbuchFilenameFilter(dateiEndung));
records = new ArrayList();
for (int i = 0; i < dateien.length; i++){
try {
Adresse adresse = new Adresse(dateien[i]);
records.add(adresse);
//((List) records).add(records);
aktuelleId = Math.max(aktuelleId, Integer.parseInt(adresse.getId()) );
} catch (AdresseException ex) {
System.out.println("Fehler: " + ex.getMessage());
}
}
}else{
throw new IllegalArgumentException(pAdressVerzeichnis + " ist kein Verzeichnis. Bitte eine existierendes Verzeichnis angeben.");
}
}else{
throw new IllegalArgumentException("Null-Wert nicht erlaubt. Argument muss ein gültiger Pfad zu einer vorhandenen oder zu erstellenden Adress-Datei sein.");
}
}
public Records getNewRecord() throws AdresseException{
String id = Integer.toString(++aktuelleId);
File file = new File(recordVerzeichnis + System.getProperty("file.separator") + id + dateiEndung);
try {
file.createNewFile();
} catch (IOException ex) {
throw new AdresseException("Erstellung der Datei " + file.getAbsolutePath() + " fehlgeschlagen: " + ex.getMessage());
}
Records record = getInstance(file);
record.setId(id);
records.add(record);
return record;
}
abstract Records getInstance(File file) throws AdresseException;
}
// ****************************************************************************
package aufgabe.nr026.adressbuch;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Records {
public static final transient String ID = "id";
public Properties properties = new Properties();
public File file = null;
protected Records(File pFile) throws AdresseException{
if (pFile==null){
throw new AdresseException("Null-Wert in Konstruktor nicht erlaubt.");
}
file = pFile;
FileInputStream fis = null;
try{
fis = new FileInputStream(pFile);
properties.load(fis);
}catch(FileNotFoundException ex){
throw new AdresseException(pFile.getAbsolutePath() + " nicht gefunden.");
}catch (IOException e) {
throw new AdresseException("Fehler während dem lesen der Adresse in " + pFile.getAbsolutePath());
}finally{
try {
fis.close();
} catch (IOException e1) {
throw new AdresseException("Fehler während dem lesen der Adresse in " + pFile.getAbsolutePath());
}
}
}
protected void speichern() throws AdresseException{
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
properties.store(fos, "Adresse " + getId());
}catch(IOException ex){
throw new AdresseException("Fehler während dem Speichern der Adresse " + getId(), ex);
}finally{
if (fos!=null){
try {
fos.close();
} catch (IOException ex) {
throw new AdresseException("Fehler während dem Speichern der Adresse " + getId(), ex);
}
}
}
}
protected String getId(){
return properties.getProperty(ID);
}
protected void setId(String wert) throws AdresseException{
properties.setProperty(ID, wert);
speichern();
}
}