-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.java
More file actions
141 lines (122 loc) · 3.81 KB
/
Group.java
File metadata and controls
141 lines (122 loc) · 3.81 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
package com.gmail.safordog;
import javax.swing.JOptionPane;
public class Group implements Voenkom {
private String groupName;
private Student[] group = new Student[10];
public Group(String groupName) {
super();
this.groupName = groupName;
}
public Group() {
}
public void addSt(Student student) throws MyException {
for (int i = 0; i < getGroup().length; i++) {
if (getGroup()[i] == null) {
student.setGroup(this.groupName);
getGroup()[i] = student;
System.out.println("Student " + getGroup()[i].getSurname() + " added.");
break;
} else if (i == getGroup().length - 1) throw new MyException();
}
}
public void delSt(Student student) {
for (int i = 0; i < getGroup().length; i++) {
if (getGroup()[i].getSurname().equals(student.getSurname())) {
getGroup()[i] = null;
System.out.println("Student " + student.getSurname() + " deleted!");
break;
}
}
}
private void sort() {
for (int i = 0; i < getGroup().length - 1; i++) {
for (int j = i + 1; j < getGroup().length; j++) {
if (compSt(getGroup()[i], getGroup()[j]) > 0) {
Student temp = getGroup()[i];
getGroup()[i] = getGroup()[j];
getGroup()[j] = temp;
}
}
}
}
public int compSt(Student one, Student two) {
if (one != null && two == null) {
return 1;
}
if (one == null && two != null) {
return -1;
}
if (one == null && two == null) {
return 0;
}
return one.getSurname().compareTo(two.getSurname());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sort();
for (int i = 0; i < getGroup().length; i++) {
if (getGroup()[i] != null) {
sb.append((i + 1) + ") ").append(getGroup()[i].toString());
sb.append(System.lineSeparator());
}
}
return sb.toString();
}
public void findSt(String surname) {
for (int i = 0; i < getGroup().length; i++) {
if (getGroup()[i].getSurname().equals(surname)) {
System.out.println((i + 1) + ") " + getGroup()[i].getSurname() + " "
+ getGroup()[i].getName() + ": " + getGroup()[i].getGender() + "; " + getGroup()[i].getAge()
+ "; " + getGroup()[i].getAverageMark() + ".");
}
}
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public void addStInteract() throws MyException {
Student student = new Student();
student.setSurname(JOptionPane.showInputDialog(null, "Input surname: "));
student.setName(JOptionPane.showInputDialog(null, "Input name: "));
student.setGender(JOptionPane.showInputDialog(null, "Input gender: "));
student.setAge(Integer.parseInt(JOptionPane.showInputDialog(null, "Input age: ")));
student.setAverageMark(Double.parseDouble(JOptionPane.showInputDialog(null, "Input average mark: ")));
student.setGroup(JOptionPane.showInputDialog(null, "Input group: "));
for (int i = 0; i < getGroup().length; i++) {
if (getGroup()[i] == null) {
student.setGroup(this.groupName);
getGroup()[i] = student;
System.out.println("Student " + getGroup()[i].getSurname() + " added.");
return;
}
}
throw new MyException();
}
@Override
public Student[] getNextSoilder(Student[] group) {
int n = 0;
for (int i = 0; i < group.length; i++) {
if ((group[i] != null) && (group[i].getGender().equals("male")) && (group[i].getAge() >= 18)) {
n++;
}
}
Student[] groupForVoencom = new Student[n];
int i = 0;
for (Student st : group) {
if ((st != null) && (st.getGender().equals("male")) && (st.getAge() >= 18)) {
groupForVoencom[i++] = st;
}
}
return groupForVoencom;
}
public Student[] getGroup() {
return group;
}
public void setGroup(Student[] group) {
this.group = group;
}
}