-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManagement.java
249 lines (227 loc) · 8.98 KB
/
Management.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package management;
import java.lang.Integer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import app.MyDbApp;
import utils.TextIO;
public class Management {
private String sql;
private String editInput;
public void setEditInput(String editInput) {
this.editInput = editInput;
}
public String getEditInput() {
return editInput;
}
public void menuManagement()
{
try {
int select;
TextIO.putln("---------------- MANAGEMENT MENU ----------------");
TextIO.putln("1. Manage Student");
TextIO.putln("2. Manage Course");
TextIO.putln("3. Manage Enrolment");
TextIO.putln("4. Report");
TextIO.putln("0. Exit");
TextIO.put("Your choice is: ");
select = TextIO.getlnInt();
Course_Management course = new Course_Management();
Student_Management student = new Student_Management();
Enrolment_Management enrol = new Enrolment_Management();
Reports reports = new Reports();
switch (select){
case 1:TextIO.putln("---------------------------------------------------");
this.displayConsole("student");
student.menuStudent();
break;
case 2:TextIO.putln("---------------------------------------------------");
this.displayConsole("course");
course.menuCourse();
break;
case 3:TextIO.putln("---------------------------------------------------");
this.displayConsole("enrolment");
enrol.menuEnrolment();
break;
case 4:TextIO.putln("---------------------------------------------------");
reports.menuReports();
break;
case 0:System.exit(0);
break;
default:
TextIO.putln("Invalid input. Please enter again.\n");
this.menuManagement();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean addRow(String tableName) throws SQLException {
MyDbApp app = new MyDbApp();
String[] column = app.getColumnName(tableName);
String[] table = new String[column.length];
String input="";
this.sql = "INSERT INTO " + tableName + " VALUES (";
for (int i = 0; i < column.length; i++) {
if (table[i] == null)
table[i] = "";
try{
do{
TextIO.putln("INPUT " + column[i] + ": ");
input = TextIO.getlnString();
} while (!app.validateAdd(input, column[i], tableName));
} catch (SQLException e)
{
e.getMessage();
}
table[i] += input;
if (i == column.length - 1)
this.sql += "'" + table[i] + "'";
else
this.sql += "'" + table[i] + "'" + ",";
}
this.sql += ")";
TextIO.putln("-----------------------------SUCCESSFUL------------------------------");
return app.insert(this.sql);
}
public boolean deleteRow(String tableName) throws SQLException
{
MyDbApp app = new MyDbApp();
String ID;
do {
TextIO.putln("INPUT StudentID: ");
ID = TextIO.getln();
}while (!app.validateDelete(ID,tableName));
if (!tableName.equals("enrolment")) {
this.sql = "DELETE FROM " + tableName + " WHERE " + tableName + "id = " + "'"+ID+"'";
}
if (tableName.equals("enrolment")) {
this.sql = "DELETE FROM " + tableName + " WHERE " + "student = " + ID;
}
TextIO.putln("-----------------------------SUCCESSFUL------------------------------");
return app.delete(this.sql);
}
public boolean editRow(String tableName) throws SQLException
{
MyDbApp app = new MyDbApp();
String ID;
String input="";
do {
TextIO.putln("INPUT " + tableName + "ID: ");
ID = TextIO.getln();
}while (!app.validateInput(ID,tableName));
String[] column = app.getColumnName(tableName);
String[] table = new String[column.length];
this.sql = "UPDATE "+tableName+" SET ";
for (int i = 1; i < column.length; i++) {
TextIO.putln(column[i] + ": ");
if (table[i] == null)
table[i] = "";
try{
do{
TextIO.putln("INPUT " + column[i] + ": ");
input = TextIO.getlnString();
} while (!app.validateEdit(input, column[i], tableName));
} catch (SQLException e)
{
e.getMessage();
}
table[i] += input;
if (i == column.length - 1)
this.sql +=column[i]+ "=" + "'" + table[i] + "'";
else
this.sql +=column[i]+ "=" + "'" + table[i] + "'" + ",";
}
this.sql+=" WHERE "+tableName+"id = "+ID;
TextIO.putln("-----------------------------SUCCESSFUL------------------------------");
return app.update(this.sql);
}
public String displayAll(String tableName)
{
this.sql="SELECT * FROM "+tableName+ " ORDER BY "+tableName+"id";
return this.sql;
}
public String OrderbyDESC(String tableName,String order)
{
this.sql ="SELECT * FROM "+tableName+" ORDER BY "+ order+" DESC";
return this.sql;
}
public void writeToHTML(String sql,String filename) throws SQLException {
MyDbApp app = new MyDbApp();
app.writetoHTML(sql,filename);
app.close();
}
public int getMaxLength(String tableName,String columnName) throws SQLException
{
MyDbApp app = new MyDbApp();
return app.getMaxLength(tableName,columnName);
}
public void writeToTXT(String tableName) throws SQLException {
String userDir = System.getProperty("user.dir");
String fileChar = System.getProperty("file.separator");
String file = userDir+fileChar+ "src"+"\\"+"app"+"\\"+ tableName+".txt";
TextIO.putln("Written result to file " + file);
TextIO.writeFile(file);
displayConsole(tableName);
TextIO.writeStandardOutput();
}
public void displayConsole(String tableName) throws SQLException
{
MyDbApp app = new MyDbApp();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0;i<this.getColumnName(tableName).length;i++) {
String[] s =getColumnName(tableName);
int a = this.getMaxLength(tableName,s[i]);
list.add(a);
}
app.displayConsole(tableName, list);
}
public String[] getColumnName(String tableName) throws SQLException
{
MyDbApp app = new MyDbApp();
return app.getColumnName(tableName);
}
public String enrolStudent() throws SQLException
{
MyDbApp app = new MyDbApp();
String id;
do {
TextIO.putln("INPUT StudentID: ");
id = TextIO.getln();
}while (!app.validateInput(id,"student"));
this.sql="SELECT enrolment.student, CONCAT(student.lastname ||' '|| student.firstname) AS fullname, " +
"enrolment.course,course.name, enrolment.semester, enrolment.finalgrade " +
"FROM ((enrolment INNER JOIN student ON enrolment.student = student.studentid)" +
"INNER JOIN course ON enrolment.course = course.courseid) " +
"WHERE student.studentid="+id;
return this.sql;
}
public String enrolCourse() throws SQLException
{
MyDbApp app = new MyDbApp();
String id;
do {
TextIO.putln("INPUT CourseID: ");
id = TextIO.getln();
}while (!app.validateInput(id,"course"));
this.sql="SELECT enrolment.student, CONCAT(student.lastname ||' '|| student.firstname) AS fullname, " +
"enrolment.course,course.name, enrolment.semester, enrolment.finalgrade " +
"FROM ((enrolment INNER JOIN student ON enrolment.student = student.studentid)" +
"INNER JOIN course ON enrolment.course = course.courseid) WHERE course.courseid="+"'"+id+"'";
return this.sql;
}
public String failGrade()
{
this.sql="SELECT student.studentid, student.lastname||' '||student.firstname AS \"Student Full Name\", \n" +
"COUNT(enrolment.course) AS \"Number of Fail Courses\"\n" +
"FROM enrolment\n" +
"LEFT JOIN student\n" +
"ON student.studentid = enrolment.student\n" +
"WHERE enrolment.finalgrade = 'F'\n" +
"GROUP BY student.studentid\n" +
"ORDER BY student.studentid";
return this.sql;
}
}