There are three Tables named as car_details, bike_details, truck_details the fields are same for all the three tables (RegistrationNo, No.ofYearsOld, OwnerName, ContactNo, VehicleType) the field VehicleType should specify type of the vehicle(car or bike or truck) and its default value is car.
All three tables have their separate POJO and repository
The task is I need to save the data into the corresponding table by considering the vehicleType field.
In My controller
@RequestMapping (value = "/createVehicle/", method = RequestMethod.POST, consumes = "application/json")
@ResponseBody
public ResponseEntity<> createGeneralAuthentication (@RequestBody CarDetails carDetails) {
public void roleSelector(String type) {
switch (type){
case "car":
return new CarRepository();
break;
case "bike":
return new BikeRepository();
break;
case "truk":
return new TruckRepository();
break;
}
}
RolesRepository rolesRepository = new roleSelector(carDetails.getType());
try {
rolesRepository.save(carDetails);
} catch (Exception ex) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(HttpStatus.OK);
}
No if else should be used it need to be a strategy pattern.