Add new classes for calculator

This commit is contained in:
cugurlu
2019-03-25 13:32:01 +01:00
parent 99143a8f20
commit 8e58118ac7
3 changed files with 213 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
package greenify.server;
public class InputItem {
private String name;
private Boolean isFloat;
private String defaultValue;
private Boolean isPresentByDefault = true;
/**
* Constructor for input items.
* @param name of the input
* @param isFloat whether it is float or not
* @param defaultValue states the value
*/
public InputItem(String name, boolean isFloat, String defaultValue) {
this.name = name;
this.isFloat = isFloat;
this.defaultValue = defaultValue;
}
/**
* Constructor for input items.
* @param name of the input
* @param isFloat whether it is float or not
* @param defaultValue states the value
* @param isPresentByDefault for different number of cars
*/
public InputItem(String name, Boolean isFloat, String defaultValue,
Boolean isPresentByDefault) {
this.name = name;
this.isFloat = isFloat;
this.defaultValue = defaultValue;
this.isPresentByDefault = isPresentByDefault;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getFloat() {
return isFloat;
}
public void setFloat(Boolean isFloat) {
this.isFloat = isFloat;
}
}

View File

@@ -0,0 +1,87 @@
package greenify.server;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class InputValidator {
private static final List<InputItem> inputItems = Arrays.asList(
new InputItem("input_size", false, "1"),
new InputItem("input_income", false, "40000"),
new InputItem("transportation_num_vehicles", false, "1" ),
new InputItem("transportation_miles1", false, "16100", false),
new InputItem("transportation_fuels1", false, "2" , false),
new InputItem("transportation_mpg1", false, null, false ),
new InputItem("transportation_miles2", false, "13200", false ),
new InputItem("transportation_fuels2", false, "0" , false),
new InputItem("transportation_mpg2", false, "22" ,false),
new InputItem("transportation_publicTrans", false, "436" ),
new InputItem("transportation_air", false, "3900" ),
new InputItem("housing_electricity_kwh_year", false, "12632" ),
new InputItem("housing_cleanPercent", false, "0" ),
new InputItem("housing_naturalGas_therms_year", false, "472" ),
new InputItem("housing_heatingOil_gallons_year", false, "73" ),
new InputItem("housing_square_feet", false, "1850" ),
new InputItem("housing_water_sewage", false, "100" ),
new InputItem("food_meat_fish_eggs", true, "2.4" ),
new InputItem("food_grains", true, "4.1" ),
new InputItem("food_dairy", true, "2.2" ),
new InputItem("food_fruit_vegetables", true, "3.5" ),
new InputItem("food_snacks_drinks", true, "3.4" ),
new InputItem("shopping_goods", false, "1310" ),
new InputItem("shopping_services", false, "2413" )
);
/**
* The method checks that the id is valid or not.
* @param inputName the name of input
* @return true or false
*/
public static Boolean isValidItem(String inputName) {
return inputItems.stream().filter(i -> i.getName() == inputName).findAny().isPresent();
}
/**
* The method checks that the item value is valid or not.
* @param inputName the name of input
* @param value the value of item
* @return true or false
*/
public static boolean isValidItemValue(String inputName, String value) {
InputItem item = null;
for (int i = 0; i < inputItems.size(); i++) {
if (inputItems.get(i).getName() == inputName) {
item = inputItems.get(i);
}
}
if (item.getFloat()) {
try {
Float number = Float.parseFloat(value);
} catch (NumberFormatException | NullPointerException nfe) {
return false;
}
return true;
} else {
try {
Integer number = Integer.parseInt(value);
} catch (NumberFormatException | NullPointerException nfe) {
return false;
}
return true;
}
}
/**
* getter for default values.
* @return the map of default values
*/
public static Map<String, String> getDefaultValues() {
Map<String,String> map = new HashMap<String, String>(){};
for (int i = 0; i < inputItems.size(); i++) {
map.put(inputItems.get(i).getName(), inputItems.get(i).getDefaultValue());
}
return map;
}
}

View File

@@ -0,0 +1,67 @@
package greenify.server.service;
import greenify.server.data.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Map;
@Service
public class CalculatorService {
Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
RestTemplate restTemplate;
@Bean
RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
protected Float invokeExternalService(Map<String, String> map) {
/**
* curl -X GET "https://apis.berkeley.edu/coolclimate/footprint-sandbox?input_location_mode=1
* &input_location=48001&input_income=1&input_size=0&input_footprint_transportation_miles1=3
* &input_footprint_transportation_mpg1=5&input_footprint_transportation_fuel1=0"
* -H "accept: application/json" -H "app_id: a98272e3"
* -H "app_key: b9167c4918cb2b3143614b595065d83b"
*/
HttpHeaders headers = new HttpHeaders();
headers.set("accept", MediaType.APPLICATION_JSON_VALUE);
headers.set("app_id", "a98272e3");
headers.set("app_key", "b9167c4918cb2b3143614b595065d83b");
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint-sandbox");
for (String inputId : map.keySet()) {
builder = builder.queryParam(inputId, map.get(inputId));
}
ResponseEntity<String> response = restTemplate
.exchange(builder.build().encode().toUri(), HttpMethod.GET,
entity, String.class);
logger.info(response.getStatusCode().toString());
logger.info(response.getBody());
String result = response.getBody().substring(response.getBody()
.indexOf("<result_grand_total>")
+ 20, response.getBody().indexOf("</result_grand_total>"));
// to do: in not HTTP 200 or exception case throws exception
System.out.println(Float.parseFloat(result));
return Float.parseFloat(result);
}
public Float calculateFootprint(User user) {
return invokeExternalService(user.getFootPrintInputs());
}
}