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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
| public class People {
private Name name;
private Date birthDate;
public People(Name name, Date birthDate) {
this.name = name;
this.birthDate = birthDate;
}
public Name getName() {
return name;
}
public Date getBirthDate() {
return birthDate;
}
@Override
public String toString(){
return name.toString()+ ", дата рождения: " +birthDate.toString();
}
static void sortByName(People[] peoples){
int min_index;
for (int i = 0; i < peoples.length-1; i++) {
min_index=i;
for (int j = i+1; j < peoples.length; j++) {
int flag=peoples[min_index].getName().getFirstName().compareToIgnoreCase(peoples[j].getName().getFirstName());
if (flag>0){
min_index=j;
}
else if (flag==0){
flag=peoples[min_index].getName().getSecondName().compareToIgnoreCase(peoples[j].getName().getSecondName());
if (flag>0){
min_index=j;
}
else if (flag==0){
flag=peoples[min_index].getName().getLastName().compareToIgnoreCase(peoples[j].getName().getLastName());
if (flag>0){
min_index=j;
}
}
}
}
People buf=peoples[i];
peoples[i]=peoples[min_index];
peoples[min_index]=buf;
}
}
static void sortByDate(People[] peoples){
int max_index;
for (int i = 0; i < peoples.length-1; i++) {
max_index=i;
for (int j = i + 1; j < peoples.length; j++) {
int dateMax=peoples[max_index].getBirthDate().getYear();
int dateJ=peoples[j].getBirthDate().getYear();
if (dateMax<dateJ){
max_index=j;
}
else if (dateMax==dateJ){
dateMax=peoples[max_index].getBirthDate().getMonth();
dateJ=peoples[j].getBirthDate().getMonth();
if (dateMax<dateJ){
max_index=j;
}
else if (dateMax==dateJ){
dateMax=peoples[max_index].getBirthDate().getDay();
dateJ=peoples[j].getBirthDate().getDay();
if (dateMax<dateJ){
max_index=j;
}
}
}
}
People buf=peoples[i];
peoples[i]=peoples[max_index];
peoples[max_index]=buf;
}
}
static class Name{
private String firstName;
private String secondName;
private String lastName;
public Name(String firstName,String secondName, String lastName){
this.firstName=firstName;
this.secondName=secondName;
this.lastName=lastName;
}
public String getFirstName() {
return firstName;
}
public String getSecondName() {
return secondName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString(){
return firstName+ " " +secondName+ " " +lastName;
}
}
static class Date{
private int year;
private int month;
private int day;
public Date(int year,int month,int day){
if(year<0)throw new IllegalArgumentException("Неверно введен год!");
if (month<1 || month>12)throw new IllegalArgumentException("Неверно введен месяц!");
this.year=year;
this.month=month;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day<1 || day>31)throw new IllegalArgumentException("Неверно введен день!");
this.day=day;
break;
case 4:
case 6:
case 9:
case 11:
if (day<1 || day>30)throw new IllegalArgumentException("Неверно введен день!");
this.day=day;
break;
default: if (day>=1 && day<29 ){
this.day=day;
break;
}
else if (day==29 && ((year%4 == 0 && year%100 != 0) || (year%400 == 0))){
this.day=day;
break;
}
else throw new IllegalArgumentException("Неверно введен день!");
}
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
@Override
public String toString(){
return year+ " / " +month+ " / " +day;
}
}
public static void main(String[] args) {
People[] peoples = {
new People(new Name("A","B","C"),new Date(2000,01,01)),
new People(new Name("A","B","C"),new Date(1996,01,02)),
new People(new Name("A","B","D"),new Date(1996,02,02)),
new People(new Name("A","C","C"),new Date(2000,02,02)),
new People(new Name("B","B","C"),new Date(2000,01,02)),
new People(new Name("AA","B","C"),new Date(1996,01,01)),
new People(new Name("AB","B","C"),new Date(2000,01,01))
};
for (People p:peoples
) {
System.out.println(p);
}
System.out.println("\n AFTER SORTING BY DATE: \n");
sortByDate(peoples);
for (People p:peoples
) {
System.out.println(p);
}
System.out.println("\n AFTER SORTING BY NAME: \n");
sortByName(peoples);
for (People p:peoples
) {
System.out.println(p);
}
System.out.println(new Date(2016,2,1));
}
} |