'How to keep track of how much profile a user has completed in spring boot?

I have a Spring Boot project in which a user can create his/her profile, which may not be completed in one sitting.

I want to keep track of how much profile is completed, probably in percentage, and show it to the user.

I was thinking of checking all the fields for null but that seems tedious as there are already about 25 fields, which might increase in future.

While researching about this, I found some answers on SO about using java reflection, but I also came to know that refleciton is slow, and it creates fragile code. (I am using java 11)

Other than checking nulls in the fields of each user, is there a better way to keep track of profile completeness?



Solution 1:[1]

You have n fields. Assuming that 0 <= k <= n fields were already filled, you have (100 * k / n)% completed. Now, you only need to:

  • initialize k with 0 upon form completion start
  • increment k whenever a field is filled
  • decrement k whenever a filled field gets unfilled
  • display the new percentages whenever k changes
  • if a new field is created for the forms, increment n
  • if a field no longer exists, decrement n
  • you need to decide whether you count all your fields into the completion percentage or only the required fields
  • upon page reload, you might want to preserve the form values. You can use localStorage to store the current status whenever a field changes, storing all the filled values
  • upon page reload the number of filled values stored into localStorage will be the value of k

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lajos Arpad