Skip to content

Commit

Permalink
Merge pull request #226 from mivek/feature/Cardinal_Direction
Browse files Browse the repository at this point in the history
Feature/cardinal direction
  • Loading branch information
mivek authored Nov 29, 2020
2 parents 040a75e + 06fa508 commit 834f4df
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 240 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ checks:
identical-code:
config:
threshold: # language-specific defaults. an override will affect all languag

plugins:
checkstyle:
enabled: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,11 @@
* @author mivek
*/
public final class Converter {

/** North East minimal degrees. */
private static final double NORTH_EAST_MIN = 22.5;
/** North east maximal degrees. */
private static final double NORTH_EAST_MAX = 67.5;
/** East degrees. */
private static final double EAST = 112.5;
/** South East degrees. */
private static final double SOUTH_EAST = 157.5;
/** South degrees. */
private static final double SOUTH = 202.5;
/** North West degrees. */
private static final double NORTH_WEST = 337.5;
/** West degrees. */
private static final double WEST = 292.5;
/** South west degrees. */
private static final double SOUTH_WEST = 247.5;
/**
* Arrays of cardinal directions.
*/
private static final String[] DIRECTIONS = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"};

/**
* Private constructor.
Expand All @@ -42,47 +30,13 @@ private Converter() {
*/
public static String degreesToDirection(final String degreesStr) {
double degrees;
String res;
try {
degrees = Double.parseDouble(degreesStr);
} catch (NumberFormatException e) {
return Messages.getInstance().getString("Converter.VRB");
}

if (isBetween(degrees, NORTH_EAST_MIN, SOUTH)) {
if (isBetween(degrees, NORTH_EAST_MIN, NORTH_EAST_MAX)) {
res = Messages.getInstance().getString("Converter.NE");
} else if (isBetween(degrees, NORTH_EAST_MAX, EAST)) {
res = Messages.getInstance().getString("Converter.E");
} else if (isBetween(degrees, EAST, SOUTH_EAST)) {
res = Messages.getInstance().getString("Converter.SE");
} else {
res = Messages.getInstance().getString("Converter.S");
}
} else {
if (isBetween(degrees, SOUTH, SOUTH_WEST)) {
res = Messages.getInstance().getString("Converter.SW");
} else if (isBetween(degrees, SOUTH_WEST, WEST)) {
res = Messages.getInstance().getString("Converter.W");
} else if (isBetween(degrees, WEST, NORTH_WEST)) {
res = Messages.getInstance().getString("Converter.NW");
} else {
res = Messages.getInstance().getString("Converter.N");
}
}
return res;
}

/**
* Checks if num is between lower and max.
*
* @param num double to test
* @param lower the minimum value, included.
* @param max The maximum value, exluded.
* @return true if num is between lower and max, false otherwise.
*/
static boolean isBetween(final double num, final double lower, final double max) {
return lower <= num && max > num;
return Messages.getInstance().getString("Converter." + DIRECTIONS[(int) ((degrees + 11.5) / 22.5)]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,24 @@ MetarFacade.InvalidIcao=Icao code is invalid.

Converter.D=decreasing
Converter.E=East
Converter.ENE=East North East
Converter.ESE=East South East
Converter.N=North
Converter.NE=North East
Converter.NNE=North North East
Converter.NNW=North North West
Converter.NSC=no significant change
Converter.NW=North West
Converter.S=South
Converter.SE=South East
Converter.SSE=South South East
Converter.SSW=South South West
Converter.SW=South West
Converter.U=up rising
Converter.VRB=Variable
Converter.W=West
Converter.WNW=West North West
Converter.WSW=West South West

WeatherChangeType.FM=From
WeatherChangeType.BECMG=Becoming
Expand Down
Loading

0 comments on commit 834f4df

Please sign in to comment.