-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
212 lines (176 loc) · 7.32 KB
/
Main.java
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
200
201
202
203
204
205
206
207
208
209
package com.company;
import java.sql.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static String db_url ="jdbc:postgresql://localhost:5432/university";
public static String db_user ="postgres";
public static String db_pass ="freeman1997";
private static void FindHeadOfDepartment(String str){
try {
Connection con = DriverManager.getConnection(db_url,
db_user, db_pass);
PreparedStatement stmt = con.prepareStatement("SELECT departments.name, departments.head_id, lectors.name FROM lectors, departments WHERE departments.head_id = lectors.id AND departments.name = ?");
stmt.setString(1, str);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println("Head of " + str + " department is " + rs.getString(3));
}
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void ShowDepartmentStatistic(String str){
List<String> degreeList = new ArrayList<String>();
try {
Connection con = DriverManager.getConnection(db_url,
db_user, db_pass);
PreparedStatement stmt = con.prepareStatement("SELECT departments.name, departments.id, lectors.id, lectors.degree, lectors_departments.department_id, lectors_departments.lector_id " +
"FROM lectors, departments, lectors_departments " +
"WHERE departments.id = lectors_departments.department_id " +
"AND lectors.id = lectors_departments.lector_id " +
"AND departments.name = ?");
stmt.setString(1, str);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
degreeList.add(rs.getString(4));
}
int countAssistant = 0;
int countAssociateProfessor = 0;
int countProfessor = 0;
for (String s : degreeList) {
switch (s){
case "assistant":
countAssistant++;
break;
case "associated professor":
countAssociateProfessor++;
break;
case "professor":
countProfessor++;
break;
}
}
System.out.println("assistans - " + countAssistant);
System.out.println("associated professors - " + countAssociateProfessor);
System.out.println("professors - " + countProfessor);
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void AverageSalary(String str){
try {
Connection con = DriverManager.getConnection(db_url,
db_user, db_pass);
PreparedStatement stmt = con.prepareStatement("SELECT departments.name, departments.id, lectors.id, lectors.salary, lectors_departments.department_id, lectors_departments.lector_id " +
"FROM lectors, departments, lectors_departments " +
"WHERE departments.id = lectors_departments.department_id " +
"AND departments.name = ? " +
"AND lectors.id = lectors_departments.lector_id");
stmt.setString(1, str);
ResultSet rs = stmt.executeQuery();
int sum = 0;
int counter = 0;
while (rs.next()) {
sum += rs.getInt(4);
counter++;
}
System.out.println("The avarage salary of " + str + " department is "+ sum/counter);
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void CountEmployee(String str){
try {
Connection con = DriverManager.getConnection(db_url,
db_user, db_pass);
PreparedStatement stmt = con.prepareStatement("SELECT departments.name, departments.id, lectors_departments.department_id " +
"FROM lectors_departments, departments " +
"WHERE departments.id = lectors_departments.department_id " +
"AND departments.name = ?");
stmt.setString(1, str);
ResultSet rs = stmt.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
System.out.println("There are " + count +" employees");
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void SearchByTemplate(String str){
try {
Connection con = DriverManager.getConnection(db_url,
db_user, db_pass);
PreparedStatement stmt = con.prepareStatement("SELECT lectors.name FROM lectors WHERE lectors.name LIKE '%" + str + "%'");
// stmt.setString(1, str);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main (String[] args){
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Scanner inAction = new Scanner(System.in);
Scanner in = new Scanner(System.in);
Integer action;
String str;
do {
System.out.println("1 - Who is head of department");
System.out.println("2 - Show department statistic");
System.out.println("3 - Show the average salary for department");
System.out.println("4 - Show count of employee for department");
System.out.println("5 - Global search by template");
System.out.println("6 - Exit");
System.out.println("Enter the number of action: ");
action = inAction.nextInt();
switch (action) {
case 1:
System.out.println("Enter name of department:");
str = in.nextLine();
FindHeadOfDepartment(str);
break;
case 2:
System.out.println("Enter name of department:");
str = in.nextLine();
ShowDepartmentStatistic(str);
break;
case 3:
System.out.println("Enter name of department:");
str = in.nextLine();
AverageSalary(str);
break;
case 4:
System.out.println("Enter name of department:");
str = in.nextLine();
CountEmployee(str);
break;
case 5:
System.out.println("Enter the template: ");
str = in.nextLine();
SearchByTemplate(str);
break;
case 6:
return;
}
} while (action != 6);
in.close();
inAction.close();
}
}