Skip to content

Commit

Permalink
Homework dirkriehle#9: still didnt work ??
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Koch committed Dec 16, 2017
1 parent 546994e commit 575a2a0
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 9 deletions.
29 changes: 29 additions & 0 deletions src/main/java/org/wahlzeit/model/Kleinbaer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public enum kleinbaerengenus {
* @param genus for which the Kleinbaerfacts are set
*/
public Kleinbaer(kleinbaerengenus genus){
assertGenusNotNull(genus);

updateKleinbaerfacts(genus);
}

Expand All @@ -74,6 +76,8 @@ public String getSCIENTIFIC_NAME() {
* @return genus of the Kleinbaer
*/
public kleinbaerengenus getGenus(){
assertGenusNotNull(genus);

return genus;
}

Expand All @@ -82,6 +86,8 @@ public kleinbaerengenus getGenus(){
* @param newGenus Kleinbaer-Genus which is to be set
*/
public void setGenus(kleinbaerengenus newGenus){
assertGenusNotNull(genus);

updateKleinbaerfacts(newGenus);
genus = newGenus;
}
Expand All @@ -99,6 +105,8 @@ public int getNumberOfTeeth(){
* @param genus for which the typical number of teeth get set
*/
private void setNumberOfTeeth(kleinbaerengenus genus){
assertGenusNotNull(genus);

if(genus == kleinbaerengenus.WICKELBAER){
this.numberOfTeeth = 36;
return;
Expand Down Expand Up @@ -147,6 +155,8 @@ public int getMaxSnoutVentLength(){
* @param genus sets the maxWeight for this genus in gramms
*/
private void setMaxWeight(kleinbaerengenus genus){
assertGenusNotNull(genus);

switch (genus){
case MAKIBAER:
this.maxWeight = 1_500;
Expand Down Expand Up @@ -176,6 +186,8 @@ private void setMaxWeight(kleinbaerengenus genus){
* @param genus sets the max tail length for this genus in cm
*/
private void setMaxTailLength(kleinbaerengenus genus){
assertGenusNotNull(genus);

switch (genus){
case MAKIBAER:
this.maxTailLength = 48;
Expand Down Expand Up @@ -205,6 +217,8 @@ private void setMaxTailLength(kleinbaerengenus genus){
* @param genus sets the max length(without the tail) for this genus in cm
*/
private void setMaxSnoutVentLength(kleinbaerengenus genus){
assertGenusNotNull(genus);

switch (genus){
case MAKIBAER:
this.maxSnoutVentLength = 48;
Expand Down Expand Up @@ -235,11 +249,26 @@ private void setMaxSnoutVentLength(kleinbaerengenus genus){
* @param newGenus the genus for which the global variables are updated
*/
private void updateKleinbaerfacts(kleinbaerengenus newGenus){
assertGenusNotNull(newGenus);

setNumberOfTeeth(newGenus);
setMaxWeight(newGenus);
setMaxSnoutVentLength(newGenus);
setMaxTailLength(newGenus);
}
/**
* ------------------------ Assertions ------------------------
*/

/**
* Asserts that the given genus is not null
* @methodtype assertion
* @throws KleinbaerException if given kleinbaerengenus is null
*/
private void assertGenusNotNull(kleinbaerengenus genus){
if(genus == null){
throw new KleinbaerException("kleinbaerengenus mustn't be null");
}
}

}
23 changes: 22 additions & 1 deletion src/main/java/org/wahlzeit/model/KleinbaerPhoto.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public KleinbaerPhoto(){
* @param kleinbaer which is in the Photo
*/
public KleinbaerPhoto(Kleinbaer kleinbaer){
this();
super();

assertKleinbaerNotNull(kleinbaer);
this.kleinbaer = kleinbaer;
}

Expand All @@ -53,6 +55,7 @@ public KleinbaerPhoto(PhotoId id){
*/
public KleinbaerPhoto(Kleinbaer kleinbaer, PhotoId id){
super(id);
assertKleinbaerNotNull(kleinbaer);
this.kleinbaer = kleinbaer;
}

Expand All @@ -61,6 +64,8 @@ public KleinbaerPhoto(Kleinbaer kleinbaer, PhotoId id){
* @return the Kleinbaer.class which is in the photo
*/
public Kleinbaer getKleinbaer(){
assertKleinbaerNotNull(kleinbaer);

return kleinbaer;
}

Expand All @@ -69,8 +74,24 @@ public Kleinbaer getKleinbaer(){
* @param kleinbaer is newly set for the photo
*/
public void setKleinbaer(Kleinbaer kleinbaer){
assertKleinbaerNotNull(kleinbaer);

this.kleinbaer = kleinbaer;
}

/**
* ------------------------ Assertions ------------------------
*/

/**
* Asserts that the given Kleinbaer-class is not null
* @methodtype assertion
* @throws KleinbaerPhotoException if given Kleinbaer-class is null
*/
private void assertKleinbaerNotNull(Kleinbaer kleinbaer){
if(kleinbaer == null){
throw new KleinbaerPhotoException("Kleinbaer-class mustn't be null");
}
}

}
15 changes: 15 additions & 0 deletions src/main/java/org/wahlzeit/model/KleinbaerPhotoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.wahlzeit.model;

public class KleinbaerPhotoException extends RuntimeException {

public String errorMsg;

public KleinbaerPhotoException(){
super();
}

public KleinbaerPhotoException(String msg){
super(msg);
this.errorMsg = msg;
}
}
24 changes: 20 additions & 4 deletions src/main/java/org/wahlzeit/model/KleinbaerPhotoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public static synchronized KleinbaerPhotoFactory getInstance(){
* Method to set the singleton instance of PhotoFactory
*/
protected static synchronized void setInstance(KleinbaerPhotoFactory photoFactory){
if(instance != null){
throw new IllegalStateException("attempt to initalize PhotoFacotry twice");
}
assertInstanceIsNull();

instance = photoFactory;
}
Expand All @@ -65,6 +63,7 @@ protected static synchronized void setInstance(KleinbaerPhotoFactory photoFactor
* @return creates a KleinbaerPhoto
*/
public KleinbaerPhoto createPhoto(){
KleinbaerPhoto photo = null;
return new KleinbaerPhoto();
}

Expand All @@ -78,11 +77,28 @@ public KleinbaerPhoto createPhoto(PhotoId id){
}

public KleinbaerPhoto createPhoto(Kleinbaer.kleinbaerengenus genus){
return new KleinbaerPhoto(new Kleinbaer(genus));
KleinbaerPhoto photo = new KleinbaerPhoto(new Kleinbaer(genus));
return photo;
}


public KleinbaerPhoto loadPhoto(PhotoId id){
return null;
}

/**
* ------------------------ Assertions ------------------------
*/

/**
* Asserts that the KleinbaerPhotoFactory instance is null
* @methodtype assertion
* @throws KleinbaerPhotoFactoryException if KleinbaerPhotoFactory instance is null
*/
static private void assertInstanceIsNull(){
if(instance != null){
throw new KleinbaerPhotoException("KleinbaerPhotoFactory instance mustn't be initialized twice!");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.wahlzeit.model;

public class KleinbaerPhotoFactoryException extends RuntimeException {

public String errorMsg;
public RuntimeException propagatedException;

public KleinbaerPhotoFactoryException(){
super();
}

public KleinbaerPhotoFactoryException(String msg){
super(msg);
this.errorMsg = msg;
}

public KleinbaerPhotoFactoryException(String msg, RuntimeException exception){
super(msg);
this.errorMsg = msg;
this.propagatedException = exception;
}
}
20 changes: 19 additions & 1 deletion src/main/java/org/wahlzeit/model/KleinbaerPhotoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
public class KleinbaerPhotoManager extends PhotoManager {


protected static final KleinbaerPhotoManager instance = new KleinbaerPhotoManager();

private static final Logger log = Logger.getLogger(PhotoManager.class.getName());
Expand Down Expand Up @@ -76,7 +77,7 @@ public Collection<KleinbaerPhoto> run() {
}


public Photo getPhotoFromId(PhotoId id){
public Photo getPhotoFromId(PhotoId id) throws KleinbaerPhotoManagerException{
if(id == null){
return null;
}
Expand All @@ -89,6 +90,23 @@ public Photo getPhotoFromId(PhotoId id){
}
}

assertPhotoIsNotNull(result);
return result;
}

/**
* ------------------------ Assertions ------------------------
*/

/**
* Asserts that Photo is not null
* @methodtype assertion
* @throws KleinbaerPhotoFactoryException if Photo is null
*/
static private void assertPhotoIsNotNull(Photo photo){
if(photo != null){
throw new KleinbaerPhotoManagerException("Loaded Photo mustn't be null!");
}
}

}
5 changes: 2 additions & 3 deletions src/main/java/org/wahlzeit/model/SphericCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public double getLongitude(){
* @methodtype get
* @return RADIUS of the earth
*/
public int getRadius(){
assertClassInvariants();
public static int getRadius(){
return RADIUS;
}

Expand Down Expand Up @@ -157,7 +156,7 @@ public int hashCode(){
* @methodtype assertion
* @throws IllegalStateException if Latitude or Longitude are set to an invalid value
*/
void assertClassInvariants(){
private void assertClassInvariants(){
if(Math.abs(this.latitude) > 90 || Math.abs(this.longitude) > 180){
throw(new IllegalStateException("Latitude or Longitude of coordinate are set to an invalid value!"));
}
Expand Down

0 comments on commit 575a2a0

Please sign in to comment.