-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonInfo.java
More file actions
54 lines (45 loc) · 1.89 KB
/
Copy pathPersonInfo.java
File metadata and controls
54 lines (45 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Lab3;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;
public class PersonInfo {
public static void main(String [] args) throws IndexOutOfBoundsException{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your surname, name, patronymic" +
" and the date of birth in dd.mm.yyyy format");
String [] info = scanner.nextLine().split(" ");
String surname = info[0], name = info[1], patronymic = info[2];
String birthday = info[3];
int age = calculateAge(birthday);
String yearEnding = getYearEnding(age);
char sex = defineSex(patronymic);
System.out.printf("%s %c.%c. %c %d %s%n", surname, name.charAt(0),
patronymic.charAt(0), sex, age, yearEnding);
}
private static int calculateAge(String birthday){
String [] parts = birthday.split("\\.");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
int age = 0;
try{
LocalDate date = LocalDate.of(year, month, day);
age = Period.between(date, LocalDate.now()).getYears();
}
catch (DateTimeException e){
System.out.println("Something went wrong. Try again");
}
return age;
}
private static String getYearEnding(int age){
if (age % 10 == 1 && age != 11) return "год";
else if ((age % 10 == 2 || age % 10 == 3 || age % 10 == 4) && (age < 10 || age > 20)) return "года";
else return "лет";
}
private static char defineSex(String patronymic){
String end = patronymic.substring(Math.max(0, patronymic.length() - 2));
if (end.equalsIgnoreCase("на")) return 'Ж';
return 'М';
}
}