I am building a MealPlanner application which builds up a menu of meals based on user preferences, and I need some guidance to set 2 fields in DTO (field names: preferenceType, resultState).
Structure of Entity (named Meal):
class Meal {
String itemName; //Such as Pizza, Sandwich
double portion; //such as 1.5
double cost; //such as 15.5, default currency is dollars
}
Structure of DTO:
class MealDTO {
//Identical to Meal
String itemName;
double portion;
double cost;
//Additional fields in DTO
String preferenceType; //FAT FREE, DAIRY FREE, VEGETARIAN
String resultState; //OPTIMAL, FEASIBLE, INFEASIBLE
}
Class to map values between the two classes:
public class FieldsMapper {
// MealDTO -> Meal
public Meal MealDtoToMeal(MealDTO mealDto) {
Meal meal = new Meal();
meal.setItemName(mealDto.getItemName());
return meal;
}
//Meal -> MealDTO
public MealDTO MealToMealDto(Meal meal) {
MealDTO mealDto = new MealDTO();
mealDto.setItemName(meal.getItemName());
mealDto.setPortion(meal.getPortion());
mealDto.setCost(meal.getCost());
return mealDto;
}
}
Please click here: MealPlanner full flow
This way, the values of preferenceType and cost are lost. How do I map preferenceType, resultState to the DTO?
(NOTE: Meal Planner flow as steps
Firstly, I think you should use enums instead of String for your resultState and preferenceType fields as they have fixed values.
What I think you should do is create another DTO which contains the MealDTO and the resultState and preferenceType fields e.g UserMealDTO. So something like:
class UserMealDTO {
MealDTO meal;
MealPreference preferenceType;
ResultState resultState;
}
// Enums
public enum MealPreference {
FAT FREE, DAIRY FREE, VEGETARIAN;
}
public enum ResultState {
OPTIMAL, FEASIBLE, INFEASIBLE;
}