Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.servicecomb.samples.bmi.calculator;
package io.servicecomb.samples.bmi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* 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 or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.servicecomb.samples.bmi;

/**
* {@link CalculatorEndpoint} provides the common interface for different endpoint implementations.
* It needs to be declared as public.
*/
public interface CalculatorEndpoint {
/**
* Calculate the BMI(Body Mass Index).
*/
double calculate(double height, double weight);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.servicecomb.samples.bmi.calculator;
package io.servicecomb.samples.bmi;


import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.servicecomb.samples.bmi.calculator;
package io.servicecomb.samples.bmi;

/**
* {@link CalculatorService} provides interface of actual BMI calculation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.servicecomb.samples.bmi.calculator;
package io.servicecomb.samples.bmi;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand All @@ -29,15 +29,15 @@ public class CalculatorServiceImpl implements CalculatorService {
*/
@Override
public double calculate(double height, double weight) {
if (height <= 0 || weight <= 0) {
throw new IllegalArgumentException("Arguments must be above 0");
}
double heightInMeter = height / 100;
double bmi = weight / (heightInMeter * heightInMeter);
return roundToOnePrecision(bmi);
}

private double roundToOnePrecision(double value) {
if (Double.isInfinite(value) || Double.isNaN(value)) {
return value;
}
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.servicecomb.samples.bmi.webapp;
package io.servicecomb.samples.bmi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
56 changes: 29 additions & 27 deletions samples/bmi/webapp/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,46 @@ <h3>Your BMI result is: <span id="bmi_result"></span></h3>
<div class="alert alert-secondary" role="alert">
<b>Note: </b>Range of healthy weight is between <b>18.5</b> and <b>24.9</b>.
</div>
<div id="error" class="alert alert-danger" role="alert"><p id="error_message"></p></div>
</div>
</div>

</body>
<script>
<script>
$("#error").hide();
$("#submit").click(function () {
if ( !$("#height").val() || !$("#weight").val() ) {
alert("Please input both the height and weight");
return;
}
if ( $("#height").val() <= 0 ) {
alert("Please input a value greater than 0");
$("#height").val('');
return;
}
if ( $("#weight").val() <= 0 ) {
alert("Please input a value greater than 0");
$("#weight").val('');
return;
}
var jqxhr = $.get("/calculator/bmi?height=" + $("#height").val() + "&weight=" + $("#weight").val());
jqxhr.done(function (data) {
$("#bmi_result").text(data);
if ( data < 18.5 || (data < 30 && data >= 25) ) {
$("#bmi").removeClass();
$("#bmi").addClass("alert");
$("#bmi").addClass("alert-warning");
} else if ( data < 25 && data >= 18.5 ) {
$("#bmi").removeClass();
$("#bmi").addClass("alert");
$("#bmi").addClass("alert-success");
} else {
$("#bmi").removeClass();
$("#bmi").addClass("alert");
$("#bmi").addClass("alert-danger");
$.ajax({
url: "/calculator/bmi?height=" + $("#height").val() + "&weight=" + $("#weight").val(),
type: "GET",
success: function (data) {
$("#error").hide();
$("#bmi_result").text(data);
if ( data < 18.5 || (data < 30 && data >= 25) ) {
$("#bmi").removeClass().addClass("alert").addClass("alert-warning");
} else if ( data < 25 && data >= 18.5 ) {
$("#bmi").removeClass().addClass("alert").addClass("alert-success");
} else {
$("#bmi").removeClass().addClass("alert").addClass("alert-danger");
}
},
error: function (xhr) {
$("#bmi").removeClass().addClass("alert").addClass("alert-light");
$("#bmi_result").text('');
if (xhr.responseText.length == 0) {
$("#error_message").text("empty fallback called");
$("#error").removeClass().addClass("alert").addClass("alert-success").show();
return;
}
var resp = JSON.parse(xhr.responseText);
$("#error_message").text(resp.error + ": " + resp.exception)
$("#error").removeClass().addClass("alert").addClass("alert-danger").show();
}
});
});
</script>
</body>

</html>