Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop/0.2' into develop/0.2
- Loading branch information
Showing
5 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |