Skip to content

Commit

Permalink
Merge pull request #61 from Segelzwerg/Select_Result_Units_for_Speed_…
Browse files Browse the repository at this point in the history
…and_Pace

Select result units for speed and pace
  • Loading branch information
Marcel authored Dec 18, 2019
2 parents cf54852 + 7e265ff commit d25aa42
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/main/java/segelzwerg/sporttooolbox/web/pace/PaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import segelzwerg.sporttooolbox.SpeedCalculator;
import segelzwerg.sporttooolbox.web.speed.SpeedForm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
* Pace calculating service
*/
@Component
public class PaceService {

List<String> validPaceUnits = new ArrayList<>(Arrays.asList("minutesPerKilometer", "minutesPerHundredMeters"));

/**
* calulates pace
*
Expand All @@ -23,6 +29,9 @@ public class PaceService {
public Pace calculatePace(SpeedForm form) {
String majorUnit = ((majorUnit = form.getDistanceMajorUnit()) != null) ? majorUnit : "kilometer";
String minorUnit = ((minorUnit = form.getDistanceMinorUnit()) != null) ? minorUnit : "meter";
String resultUnit = ((resultUnit = form.getResultUnit()) != null) ? resultUnit : "minutesPerKilometer";

checkValidUnit(validPaceUnits, resultUnit);
int major = form.getMajor();
int minor = form.getMinor();
Distance distance = Distance.createWithOtherThanSIUnits(major, minor, majorUnit, minorUnit);
Expand All @@ -36,4 +45,10 @@ public Pace calculatePace(SpeedForm form) {

return speedCalculator.computePace();
}

private void checkValidUnit(List<String> validUnits, String unit) {
if (!validUnits.contains(unit)) {
throw new IllegalArgumentException("This is not a valid unit: " + unit);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class SpeedForm {
private int second;
private String distanceMajorUnit;
private String distanceMinorUnit;
private String resultUnit;
}
15 changes: 15 additions & 0 deletions src/main/java/segelzwerg/sporttooolbox/web/speed/SpeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import segelzwerg.sporttooolbox.IUnits.Time;
import segelzwerg.sporttooolbox.SpeedCalculator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Speed calculating service
*/
@Component
public class SpeedService {
List<String> validSpeedUnits = new ArrayList<>(Arrays.asList("kilometerPerHour", "milesPerHour", "knots"));

/**
* Calculate speed
Expand All @@ -19,8 +24,13 @@ public class SpeedService {
* @return calculated speed
*/
public Speed calculateSpeed(SpeedForm form) {

String majorUnit = ((majorUnit = form.getDistanceMajorUnit()) != null) ? majorUnit : "kilometer";
String minorUnit = ((minorUnit = form.getDistanceMinorUnit()) != null) ? minorUnit : "meter";
String resultUnit = ((resultUnit = form.getResultUnit()) != null) ? resultUnit : "kilometerPerHour";

checkValidUnit(validSpeedUnits, resultUnit);

int major = form.getMajor();
int minor = form.getMinor();
Distance distance = Distance.createWithOtherThanSIUnits(major, minor, majorUnit, minorUnit);
Expand All @@ -35,5 +45,10 @@ public Speed calculateSpeed(SpeedForm form) {
return speedCalculator.computeSpeed();
}

private void checkValidUnit(List<String> validUnits, String unit) {
if (!validUnits.contains(unit)) {
throw new IllegalArgumentException("This is not a valid unit: " + unit);
}
}

}
10 changes: 9 additions & 1 deletion src/main/resources/templates/PaceForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ <h1>Pace Calculator</h1>
type="text" value="0">
</label>
</div>
<div class="form-group">
<label> Result Unit</label>
<select class="form-control w-auto" th:field="*{resultUnit}">
<option th:text="'Minutes per Kilometer'" th:value="minutesPerKilometer">kmh</option>
<option th:text="'Minutes per 100 Meter'" th:value="minutesPerHundredMeters">mph
</option>
</select>
</div>
<button class="btn btn-primary" type="submit">Calculate</button>
</form>
</div>
Expand All @@ -72,7 +80,7 @@ <h1>Pace Calculator</h1>
<div class="card-body">
<h5 class="card-title">Pace</h5>
<p class="card-text">
The average pace is <span th:text="${pace?.minutesPerKilometer.toString()}">30</span> Minutes
The average pace is <span th:text="${pace?.__${form.resultUnit}__.toString()}">30</span> Minutes
per
Kilometer
for travelling <span th:text="${form.major}">30</span> <span
Expand Down
13 changes: 11 additions & 2 deletions src/main/resources/templates/SpeedForm.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<link href="../static/bootstrap.min.css" rel="stylesheet"
Expand Down Expand Up @@ -61,6 +61,14 @@ <h1>Speed Calculator</h1>
type="text" value="0">
</label>
</div>
<div class="form-group">
<label> Result Unit</label>
<select class="form-control w-auto" th:field="*{resultUnit}">
<option th:text="kph" th:value="kilometerPerHour">kmh</option>
<option th:text="mph" th:value="milesPerHour">mph</option>
<option th:text="knots" th:value="knots">knots</option>
</select>
</div>
<button class="btn btn-primary" type="submit">Calculate</button>
</form>
</div>
Expand All @@ -73,7 +81,8 @@ <h1>Speed Calculator</h1>
<div class="card-body">
<h5 class="card-title">Speed</h5>
<p class="card-text">
The average speed is <span th:text="${speed?.kilometerPerHour.speed}">30</span> km/h
The average speed is <span th:text="${speed?.__${form.resultUnit}__.speed}">30</span> <span
th:text="${form.resultUnit}">km/H</span>
for travelling <span th:text="${form.major}">30</span> <span
th:text="${form.distanceMajorUnit}">kilometer</span> and <span
th:text="${form.minor}">0</span> <span th:text="${form.distanceMinorUnit}">meter</span> in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ void invalidMinorUnit() {

assertThrows(IllegalArgumentException.class, () -> paceService.calculatePace(paceForm));
}

@Test
void invalidResultUnit() {
paceForm.setResultUnit("abc");

assertThrows(IllegalArgumentException.class, () -> paceService.calculatePace(paceForm));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ void invalidMinorUnit() {

assertThrows(IllegalArgumentException.class, () -> speedService.calculateSpeed(speedForm));
}

@Test
void invalidResultUnit() {
speedForm.setResultUnit("abc");

assertThrows(IllegalArgumentException.class, () -> speedService.calculateSpeed(speedForm));
}
}

0 comments on commit d25aa42

Please sign in to comment.