NicInfo.from9Digit constructor
- String nicVal
Creates a NicInfo
object from a 9-digit Sri Lankan NIC number.
-
Example:
"901234567V"
-
Extracts birth year, birth date, and gender.
-
Determines the serial number and votability.
-
Calculates age and weekday of birth.
Returns a NicInfo
object containing the extracted information.
Implementation
factory NicInfo.from9Digit(String nicVal) {
String gender = "Male";
String votability = "No";
String serialNo = nicVal.substring(5, 9);
String nicLetter = nicVal.substring(9).toLowerCase();
int birthYear = int.parse("19${nicVal.substring(0, 2)}");
int birthCode = int.parse(nicVal.substring(2, 5));
// Check gender
if (birthCode > 500) {
birthCode -= 500;
gender = "Female";
}
// Determine birth month and day
int remainingDays = birthCode;
int month = 1;
while (remainingDays > daysInMonths[month - 1]) {
remainingDays -= daysInMonths[month - 1];
month++;
}
int day = remainingDays;
// Handle 29th feb in non-leap years
if (month == 2 && day == 29 && !isLeapYear(birthYear)) {
month = 3;
day = 1;
}
String birthDate =
'$birthYear/${month.toString().padLeft(2, '0')}/${day.toString().padLeft(2, '0')}';
// Determine weekday of birth
DateTime date = DateTime.parse(birthDate.replaceAll("/", "-"));
String dayOfWeek = weekdays[date.weekday - 1];
// Calculate age
DateTime today = DateTime.now();
int age = today.year - birthYear;
if (today.month < month || (today.month == month && today.day < day)) {
age--;
}
String ageString = "${age.toString()} Years";
// Check votability
if (nicLetter == "v") {
votability = "Yes";
}
return NicInfo(
nicNo: nicVal,
format: "Old",
serialNo: serialNo,
birthDate: birthDate,
weekDay: dayOfWeek,
age: ageString,
gender: gender,
votability: votability,
);
}