object string04 {
def main(args: Array[String]): Unit = {
val idCard = "421023200506011234"
println(s"解析的身份证号:$idCard")
println("-" * 30)
val regionMap = Map(
"42" -> "湖北省", "11" -> "北京市", "31" -> "上海市",
"44" -> "广东省", "51" -> "四川省", "61" -> "陕西省"
)
val provinceCode = idCard.substring(0, 2)
val province = regionMap.getOrElse(provinceCode, "未知省份")
println(s"籍贯:$province")
val genderCode = idCard.charAt(16).asDigit
val gender = if (genderCode % 2 == 1) "男" else "女"
println(s"性别:$gender")
val birthYear = idCard.substring(6, 10).toInt
val currentYear = java.time.LocalDate.now().getYear
val age = currentYear - birthYear
println(s"年龄:$age 岁")
val birthday = s"${idCard.substring(6, 10)}-${idCard.substring(10, 12)}-${idCard.substring(12, 14)}"
println(s"生日:$birthday")
val month = idCard.substring(10, 12).toInt
val day = idCard.substring(12, 14).toInt
val constellation = getConstellation(month, day)
println(s"星座:$constellation")
}
def getConstellation(month: Int, day: Int): String = (month, day) match {
case (1, d) if d >= 20 | (2, d) if d <= 18 => "水瓶座"
case (2, d) if d >= 19 | (3, d) if d <= 20 => "双鱼座"
case (3, d) if d >= 21 | (4, d) if d <= 19 => "白羊座"
case (4, d) if d >= 20 | (5, d) if d <= 20 => "金牛座"
case (5, d) if d >= 21 | (6, d) if d <= 21 => "双子座"
case (6, d) if d >= 22 | (7, d) if d <= 22 => "巨蟹座"
case (7, d) if d >= 23 | (8, d) if d <= 22 => "狮子座"
case (8, d) if d >= 23 | (9, d) if d <= 22 => "处女座"
case (9, d) if d >= 23 | (10, d) if d <= 23 => "天秤座"
case (10, d) if d >= 24 | (11, d) if d <= 22 => "天蝎座"
case (11, d) if d >= 23 | (12, d) if d <= 21 => "射手座"
case _ => "摩羯座"
}
}