NicInfo.from12Digit constructor

NicInfo.from12Digit(
  1. String nicVal
)

Creates a NicInfo object from a 12-digit Sri Lankan NIC number.

  • Example: "199012300123"

  • 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.from12Digit(String nicVal) {
  String gender = "Male";
  String votability = "No";
  String serialNo = nicVal.substring(7);
  int birthYear = int.parse(nicVal.substring(0, 4));
  int birthCode = int.parse(nicVal.substring(4, 7));

  // 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";

  // Determine votability (age-based for 12-digit NICs)
  if (age >= 18) {
    votability = "Yes";
  }

  return NicInfo(
    nicNo: nicVal,
    format: "New",
    serialNo: serialNo,
    birthDate: birthDate,
    weekDay: dayOfWeek,
    age: ageString,
    gender: gender,
    votability: votability,
  );
}