Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop/0.2' into develop/0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
oboehm committed Apr 19, 2017
2 parents c023c37 + 756247e commit f544198
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/jfachwert/AbstractFachwert.java
Expand Up @@ -35,7 +35,7 @@ protected AbstractFachwert(T code) {
}

/**
* Liefert die interne Praesentation fuer die agbgeleiteten Klassen. Er
* Liefert die interne Praesentation fuer die abgeleiteten Klassen. Er
* ist nicht fuer den direkten Aufruf vorgesehen, weswegen die Methode auch
* 'final' ist.
*
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/de/jfachwert/post/Ort.java
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2017 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 13.04.17 by oliver (ob@oasd.de)
*/
package de.jfachwert.post;

import de.jfachwert.Fachwert;

import java.util.Objects;
import java.util.Optional;

/**
* Ein Ort (oder auch Ortschaft) ist eine Stadt oder Gemeinde. Ein Ort hat
* i.d.R. eine Postleitzahl (PLZ). Diese ist aber in dieser Klasse optional,
* sodass man einen Ort auch ohne eine PLZ einsetzen kann.
* <p>
* Anmerkung: Die PLZ ist nicht als Optional realisert, da in Java Optionals
* leider nicht serialiserbar sind :-(
* </p>
*
* @author oboehm
* @since 0.2.0 (13.04.2017)
*/
public class Ort implements Fachwert {

private final String name;
private final PLZ plz;

/**
* Hierueber kann ein Ort (ohne PLZ) angelegt werden.
*
* @param name des Ortes
*/
public Ort(String name) {
this(null, name);
}

/**
* Hierueber kann ein Ort mit PLZ angelegt werden.
*
* @param name des Ortes
*/
public Ort(PLZ plz, String name) {
this.plz = plz;
this.name = name;
}

/**
* Liefert den Ortsnamen zurueck.
*
* @return den Ortsnamen
*/
public String getName() {
return this.name;
}

/**
* Da die Postleitzahl optional ist, wird sie auch als {@link Optional}
* zurueckgegeben.
*
* @return die PLZ
*/
public Optional<PLZ> getPLZ() {
if (this.plz == null) {
return Optional.empty();
} else {
return Optional.of(this.plz);
}
}

/**
* Beim Vergleich wird nicht zwischen Gross- und Kleinschreibung
* unterschieden.
*
* @param obj der andere Ort
* @return true, falls es der gleiche Ort ist
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Ort)) {
return false;
}
Ort other = (Ort) obj;
return Objects.equals(this.plz, other.plz) && this.name.equalsIgnoreCase(other.name);
}

@Override
public int hashCode() {
return name.toLowerCase().hashCode();
}

/**
* Liefert den Orstnamen als Ergebnis.
*
* @return Ortsname
*/
@Override
public String toString() {
if (this.plz == null) {
return this.getName();
} else {
return this.plz + " " + this.getName();
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/de/jfachwert/post/PLZ.java
Expand Up @@ -104,7 +104,7 @@ private static String validateNumberOf(String plz) {

private static void validateNumberWith(int length, String kennung, String zahl) {
if (zahl.length() != length) {
throw new IllegalArgumentException(zahl + ": nur " + length + " Ziffern fuer PLZ sind erlaubt in " + kennung);
throw new IllegalArgumentException(zahl + ": nur " + length + " Ziffern f\u00dcr PLZ sind erlaubt in " + kennung);
}
Integer.valueOf(zahl);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/de/jfachwert/FachwertTest.java
@@ -1,4 +1,4 @@
package de.jfachwert;/*
/*
* Copyright (c) 2017 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,6 +15,7 @@
*
* (c)reated 16.03.2017 by oboehm (ob@jfachwert.de)
*/
package de.jfachwert;

import de.jfachwert.bank.BIC;
import de.jfachwert.steuer.Steuernummer;
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/de/jfachwert/post/OrtTest.java
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2017 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 13.04.17 by oliver (ob@oasd.de)
*/
package de.jfachwert.post;

import de.jfachwert.AbstractFachwertTest;
import de.jfachwert.Fachwert;
import org.junit.Test;

import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
* Unit-Tests fuer die Ort-Klasse.
*
* @author oboehm
*/
public class OrtTest extends AbstractFachwertTest {

/**
* Hier nehmen wir zum Testen die kleinste Stadt Deutschlands mit
* etwa 300 Einwohnern.
*
* @return den Ort Arnis in Schleswig Holstein
*/
@Override
protected Fachwert createFachwert() {
return new Ort("Arnis");
}

/**
* Test-Methode fuer {@link Ort#getPLZ()}.
*/
@Test
public void testGetPLZ() {
PLZ plz = new PLZ("73728");
Ort esslingen = new Ort(plz, "Esslingen");
assertEquals(plz, esslingen.getPLZ().get());
}

/**
* Es gibt in Deutschland ueber ein Dutzende Staedten mit dem Namen
* "Neustadt", die sich in der PLZ unterscheiden und daher nicht gleich
* sind.
*/
@Test
public void testNotEquals() {
Ort neustadtHarz = new Ort(new PLZ("99762"), "Neustadt");
Ort neustadtDonau = new Ort(new PLZ("93333"), "Neustadt");
assertThat(neustadtDonau, not(neustadtHarz));
}

/**
* Wenn zwei Ort gleich heissen, aber nur zu einem Ort eine PLZ angegeben
* wurde, kann man nicht 100% sicher sein, ob das auch der gewuenschte
* Ort ist. Sollte fuer diesen Fall vielleicht besser eine Exception
* geschmissen werden?
*/
@Test
public void testEqualsNotSatisfiable() {
Ort neustadtHarz = new Ort(new PLZ("99762"), "Neustadt");
Ort neustadt = new Ort("Neustadt");
assertThat(neustadt, not(neustadtHarz));
}

/**
* Wenn zwei Ort unterschiedlich sind, brauchen wir die PLZ nicht mehr, um
* eine Ungleichheit festzustellen.
*/
@Test
public void testEqualsSatisfiable() {
Ort neustadt = new Ort(new PLZ("99762"), "Neustadt");
Ort altstadt = new Ort("Altstadt");
assertThat(neustadt, not(altstadt));
}

}

0 comments on commit f544198

Please sign in to comment.