SHARE
TWEET

Untitled

a guest Dec 14th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {"gitTestSource":"https:/gitlab.cs.ttu.ee/iti0202-2019/ex","testingPlatform":"java","source":[{"path":"EX01IdCode/src/ee/taltech/iti0202/idcode/IDCode.java","contents":"package ee.taltech.iti0202.idcode;\n\npublic class IDCode {\n\n\tpublic static final int ZERO = 0;\n\tprivate static final int YEAR_LOCATION = 3;\n\tprivate static final int MONTH_LOCATION = 5;\n\tprivate static final int DAY_LOCATION = 7;\n\tprivate static final int ID_CODE_LENGTH = 11;\n\tprivate static final int MONTHS_IN_A_YEAR = 12;\n\tprivate static final int LEAP_FEB = 29;\n\tprivate static final int NO_LEAP_FEB = 28;\n\tprivate static final int DAYS_IN_JAN = 31;\n\tprivate static final int DAYS_IN_APRIL = 30;\n\tprivate static final int[] MULTIPLIERS1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1};\n\tprivate static final int[] MULTIPLIERS2 = {3, 4, 5, 6, 7, 8, 9, 1, 2, 3};\n\tprivate static final int LEAP_MAX = 400;\n\tprivate static final int YEAR_MAX = 99;\n\tprivate static final int QUEUE_MAX = 999;\n\tprivate static final int EPOCH_YEAR = 1700;\n\n\tpublic static boolean isIDCodeCorrect(String idCode) {\n\n\t\treturn idCode.length() == ID_CODE_LENGTH && idCode.matches(\"[0-9]+\")\n\t\t\t\t&& isGenderNumberCorrect(idCode) && isYearNumberCorrect(idCode)\n\t\t\t\t&& isMonthNumberCorrect(idCode) && isDayNumberCorrect(idCode)\n\t\t\t\t&& isQueueNumberCorrect(idCode) && isControlNumberCorrect(idCode);\n\n\t}\n\n\tprivate static boolean isGenderNumberCorrect(String idCode) {\n\t\treturn idCode.substring(0, 1).matches(\"^[1-6]$\");\n\t}\n\n\tprivate static boolean isYearNumberCorrect(String idCode) {\n\t\tint year = Integer.parseInt(idCode.substring(YEAR_LOCATION - 2, YEAR_LOCATION));\n\t\treturn year >= 0 && year <= YEAR_MAX;\n\t}\n\n\tprivate static boolean isMonthNumberCorrect(String idCode) {\n\t\tint month = Integer.parseInt(idCode.substring(YEAR_LOCATION, MONTH_LOCATION));\n\t\treturn month > 0 && month <= MONTHS_IN_A_YEAR;\n\t}\n\n\tprivate static boolean isDayNumberCorrect(String idCode) {\n\t\tint year = getFullYear(idCode);\n\t\tboolean leap = isLeapYear(year);\n\t\tString month = idCode.substring(YEAR_LOCATION, MONTH_LOCATION);\n\t\tint day = Integer.parseInt(idCode.substring(MONTH_LOCATION, DAY_LOCATION));\n\t\tif (Integer.parseInt(month) == 2) {\n\t\t\tif (leap) {\n\t\t\t\treturn day > 0 && day <= LEAP_FEB;\n\t\t\t} else {\n\t\t\t\treturn day > ZERO && day <= NO_LEAP_FEB;\n\t\t\t}\n\t\t} else if (month.equals(\"04\") || month.equals(\"06\") || month.equals(\"09\") || month.equals(\"11\")) {\n\t\t\treturn day > 0 && day <= DAYS_IN_APRIL;\n\t\t} else {\n\t\t\treturn day > 0 && day <= DAYS_IN_JAN;\n\t\t}\n\n\t}\n\n\tprivate static boolean isQueueNumberCorrect(String idCode) {\n\t\tint queue = Integer.parseInt(idCode.substring(DAY_LOCATION, 10));\n\t\treturn queue > 0 && queue <= QUEUE_MAX;\n\t}\n\n\tprivate static boolean isControlNumberCorrect(String idCode) {\n\t\tint controlNumber = 0;\n\t\tfor (int i = 0; i < 10; i++) {\n\t\t\tcontrolNumber += MULTIPLIERS1[i] * Character.getNumericValue(idCode.charAt(i));\n\t\t}\n\t\tcontrolNumber %= ID_CODE_LENGTH;\n\t\tif (controlNumber == 10) {\n\t\t\tcontrolNumber = 0;\n\t\t\tfor (int i = 0; i < 10; i++) {\n\t\t\t\tcontrolNumber += MULTIPLIERS2[i] * Character.getNumericValue(idCode.charAt(i));\n\t\t\t}\n\t\t\tcontrolNumber %= ID_CODE_LENGTH;\n\t\t\tif (controlNumber == 10) {\n\t\t\t\tcontrolNumber = 0;\n\t\t\t}\n\t\t}\n\n\t\treturn controlNumber == Integer.parseInt(idCode.substring(10));\n\t}\n\n\tprivate static boolean isLeapYear(int fullYear) {\n\t\tif (fullYear % LEAP_MAX == 0) {\n\t\t\treturn true;\n\t\t} else if (fullYear % 100 == 0) {\n\t\t\treturn false;\n\t\t}\n\t\treturn fullYear % 4 == 0;\n\t}\n\n\tpublic static String getInformationFromIDCode(String idCode) {\n\t\tif (!isIDCodeCorrect(idCode)) {\n\t\t\treturn \"Given invalid ID code!\";\n\t\t}\n\t\treturn \"This is a \" + getGender(idCode).toString().toLowerCase() + \" born on \"\n\t\t\t\t+ idCode.substring(5, DAY_LOCATION) + \".\" + idCode.substring(3, 5) + \".\" + getFullYear(idCode);\n\t}\n\n\tpublic static Gender getGender(String idCode) {\n\t\tString genderNumber = idCode.substring(0, 1);\n\t\tif (genderNumber.matches(\"^[1|3|5]$\")) {\n\t\t\treturn Gender.MALE;\n\t\t}\n\t\treturn Gender.FEMALE;\n\t}\n\n\tpublic static int getFullYear(String idCode) {\n\t\treturn Integer.parseInt(idCode.substring(1, 3)) + EPOCH_YEAR\n\t\t\t\t+ ((Integer.parseInt(idCode.substring(0, 1)) + 1) / 2) * 100;\n\t}\n\n\tpublic static void main(String[] args) {\n\n\t}\n\n\tprivate enum Gender {\n\t\tMALE, FEMALE\n\t}\n}\n"}]}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top