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,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;
}
}