Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mvc: bind hash map with same/different value depending on user input

I have a requirement where I am asked to capture a fee applicable for a certain course. But the problem is for few courses the same fee is applicable to all type of users and for some courses profession-wise fee is applicable. eg if the user is a teacher then he has fewer fees than if he is an industry professional.

There are predefined professions in database.

Here is my current logic with code.

Depending on the radio button, I am toggling the proper div and particular logic at the back end.

<input type="radio"  name="professionWiseFees" value="true">
<input type="radio"  name="professionWiseFees" value="false"> 

<div id="totalAmount" class="hidden">
   <input class="feeAmountOnly" name="feeAmount" type="text"  />
</div>

<div id="professionWiseAmount" class="hidden">
  <c:forEach items="${applicationScope.professionList}" var = "profession" >
     Course Fee For ${profession.profession}
     <input type="hidden" value="${profession.id}" name="profession" type="text"/>
     <input class="feeAmoun2t" name="profession" type="text" />
  </c:forEach>
<div>

Now I am checking which type of fees is applicable and filling the hashmap accordingly. if prefessionWiseFees is not applicable then adding the same fees to all the profession.

Boolean isProfessionWiseFeesApplicable = Boolean.parseBoolean(reqParams.get("professionWiseFees")[0]);
Map<Integer,Double> feeProfessionMap = new HashMap<>();

List<Profession> professions = (List<Profession>) servletContext.getAttribute("professionList");

if(isProfessionWiseFeesApplicable) {
    String[] propfessionWiseFees = reqParams.get("profession");
    // [1(profession_id),1000(fees),2,2000,3,7000], hence i+2 i=profession_id, i+1=fees
    for(int i=0; i < propfessionWiseFees.length-1 ;i=i+2){
        feeProfessionMap.put(Integer.valueOf(propfessionWiseFees[i]),Double.parseDouble(propfessionWiseFees[i+1]));
    }
}
else {
    double feeAmount = Double.parseDouble(reqParams.get("feeAmount")[0]);
    for(Profession profession: professions){
        feeProfessionMap.put(Integer.valueOf(profession.getId()),feeAmount);
    }
}

courseBean.setProfessionWiseFees(feeProfessionMap);
courseBean.setProfessionWiseFees(isProfessionWiseFeesApplicable);

Model class:

public class CourseBean {
    // few fields
    private Map<Integer, Double> professionWiseFees; // <profession_id ,fees>
    // all setters and getters
}

So How can I solve this problem elegantly? requestParam.get is making the code cluttery

like image 591
Govinda Sakhare Avatar asked Aug 16 '17 09:08

Govinda Sakhare


1 Answers

If you think that

use of requestParam.get reducing readability and maintainability of code.

You may use Spring web binding.

First define a FeeForm bean:

public class FeeForm {
    boolean isProfessionWiseFees;
    ProfessionAmount[] professionAmounts;
    Double feeAmount;
}

Then define the ProfessionAmount bean:

public class ProfessionAmount {
    int profession;
    double amount;
}

Then change the controller method in this way:

    @RequestMapping(value = { "/test.html" }, method = RequestMethod.POST)
    public String testOverflow(Map<String, Object> model, 
            FeeForm form)
    {
        Map<Integer,Double> feeProfessionMap = new HashMap<>();

        List<Profession> professions = (List<Profession>) servletContext.getAttribute("professionList");

        if(form.isProfessionWiseFees()) {
            for(ProfessionAmount f : form.getProfessionAmounts()){
                feeProfessionMap.put(f.getProfession(),f.getAmount());
            }
        }
        else {
            for(Profession profession: professions){
                feeProfessionMap.put(profession.getId(),form.getFeeAmount());
            }
        }

HTML:

   <input type="radio"  name="professionWiseFees" value="true"> true
    <input type="radio"  name="professionWiseFees" value="false">  false

    <div id="totalAmount" class="hidden">
       <input class="feeAmountOnly" name="feeAmount" type="text"  />
    </div>

    <div id="professionWiseAmount" class="hidden">
      <c:forEach items="${applicationScope.professionList}" var = "profession" varStatus="status" >
         Course Fee For ${profession.profession}
         <input type="hidden" value="${profession.id}" name="professionAmounts[${status.index}].profession" type="text"/>
         <input class="feeAmoun2t" value='0' name="professionAmounts[${status.index}].fee" type="text" />
      </c:forEach>
    <div>
like image 93
Testo Testini Avatar answered Oct 22 '22 04:10

Testo Testini