-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
171 lines (152 loc) · 6.22 KB
/
Person.java
File metadata and controls
171 lines (152 loc) · 6.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.time.*;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Scanner;
public class Person {
private String name; //Поле не может быть null, Строка не может быть пустой
private java.time.ZonedDateTime birthday; //Поле может быть null
private Integer height; //Поле не может быть null, Значение поля должно быть больше 0
private long weight; //Значение поля должно быть больше 0
public Person(String name, ZonedDateTime birthday, Integer height, long weight) {
this.name = name;
this.birthday = birthday;
this.height = height;
this.weight = weight;
}
public static Person insert() {
java.time.ZonedDateTime birthday = null;
Scanner in = new Scanner(System.in);
String input = "", name = "";
Integer h;
long w;
System.out.println("Введите данные автора: ");
do {
System.out.print("Введите имя автора: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(input.isEmpty() || input.contains(";") || Menu.isOnlyTab(input));
name = input;
do {
System.out.print("Введите рост автора: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!Menu.isInteger(input) || Integer.parseInt(input) <= 0);
h = Integer.parseInt(input);
do {
System.out.print("Введите вес автора: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!Menu.isInteger(input) || Integer.parseInt(input) <= 0);
w = Integer.parseInt(input);
do {
System.out.println("Хотите ввести дату рождения автора? (Y/N)");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!input.equals("Y") && !input.equals("N"));
if (input.equals("Y")) {
ZoneId zone = ZoneId.of("Europe/Moscow");
int year, minute, day, hour;
Month month;
do {
System.out.print("Введите год: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!Menu.isInteger(input) || Integer.parseInt(input) <= 1970 || Integer.parseInt(input) >= 2023);
year = Integer.parseInt(input);
String a = Arrays.toString(Month.values());
do {
System.out.println(a);
System.out.print("Выберите месяц: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!a.contains(input));
month = Month.valueOf(input);
Calendar myCalendar = (Calendar) Calendar.getInstance().clone();
myCalendar.set(year, month.getValue(), 1);
int max_date = myCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
do {
System.out.print("Введите день: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!Menu.isInteger(input) || Integer.parseInt(input) < 0 || Integer.parseInt(input) > max_date);
day = Integer.parseInt(input);
do {
System.out.print("Введите час: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!Menu.isInteger(input) || Integer.parseInt(input) < 0 || Integer.parseInt(input) > 23);
hour = Integer.parseInt(input);
do {
System.out.print("Введите минуты: ");
if (in.hasNextLine())
input = in.nextLine();
else {
System.out.println("Плохой символ");
System.exit(0);
}
} while(!Menu.isInteger(input) || Integer.parseInt(input) < 0 || Integer.parseInt(input) >= 60);
minute = Integer.parseInt(input);
LocalDateTime time = LocalDateTime.of(year, month, day, hour, minute);
birthday = ZonedDateTime.of(time, zone);
}
return new Person(name, birthday, h, w);
}
public String parse(char del) {
if (this.birthday == null) {
return name +
del + birthday +
del + height +
del + weight;
} else {
return name +
del + birthday.getYear() +
del + birthday.getMonthValue() +
del + birthday.getDayOfMonth() +
del + birthday.getHour() +
del + birthday.getMinute() +
del + height +
del + weight;
}
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", birthday=" + birthday +
", height=" + height +
", weight=" + weight +
'}';
}
}