-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.java
More file actions
305 lines (264 loc) · 11.2 KB
/
DataBase.java
File metadata and controls
305 lines (264 loc) · 11.2 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package org.cct.DataBase;
import java.io.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Vector;
import java.util.regex.Pattern;
/**
* Created by taioyui on 09/03/16.
*/
public class DataBase {
//create an empty Vector employeesList of type Person
Vector<Person> employeesList = new Vector<Person>();
String separator = ",";
public static void main(String[] args) throws IOException, ClassNotFoundException {
DataBase newBase = new DataBase();
newBase.readList();
newBase.mainMenu();
}
public void createNewPerson() throws IOException, ClassNotFoundException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a first name: ");
String firstName = checkInputForLetters();
System.out.println("Please enter a last name: ");
String lastName = checkInputForLetters();
System.out.println("Please enter id: ");
int id = checkInputDigits();
System.out.println("Please enter a department: ");
String department = checkInputForLetters();
System.out.println("Please enter age: ");
int age = checkInputDigits();
//convert input department to lowercase and compare with "it"
if (department.toLowerCase().equals("it")) {
/*step-by-step object creation way
ITperson iTperson = new ITperson(); ITperson values are passed as arguments to the constructor
iTperson.setAge(age); iTperson object is created with them
iTperson.setLastName(lastName);
iTperson.setFirstName(firstName);
iTperson.setId(id);
iTperson.setDepartment(department);
employeesList.add(iTperson); add object to vector */
//add object to vector (simplified creation way)
employeesList.add(new ITperson(firstName, lastName, age, id, department));
System.out.println(firstName + " " + lastName + " " + age + " " + id + " " + department);
} else if (department.toLowerCase().equals("finance")) {
/*add object to vector(simplified creation way)
BSperson bSperson = new BSperson(id,age,firstName,lastName,department);*/
employeesList.add(new FINperson(firstName, lastName, age, id, department));
System.out.println(firstName + " " + lastName + " " + age + " " + id + " " + department);
System.out.println("press Enter for back to Menu");
}else {
System.out.println("wRONG DEPARTMENT, INPUT NOT ADDED");
}writeCsvFile();
}
private void printMainMenu() {
System.out.println("=======================================");
System.out.println("| MAIN MENU SELECTION |");
System.out.println("=======================================");
System.out.println("| Options: |");
System.out.println("| 1. Add new employee |");
System.out.println("| 2. Print list of employees |");
System.out.println("| 3. Exit |");
System.out.println("=======================================");
System.out.println("Please, make a selection [1-3] ");
}
public void mainMenu() throws IOException, ClassNotFoundException {
// set pick any value not equals exit choice
int pick = 0;
Scanner sc = new Scanner(System.in);
while (pick != 3) {
printMainMenu();
pick = checkInputDigits();
if (pick == 2) {
SortMenu();
} else if (pick == 1) {
createNewPerson();
} else if (pick == 3) {
System.out.println("Bye Bye");
System.exit(0);
} else {
System.out.println("Input digits 1-3");
}
}
}
public void SortMenu() throws IOException {
// set pick any value not equals exit choice
int pick = -1;
Scanner sc = new Scanner(System.in);
while (pick != 0) {
printSortMenu();
pick = checkInputDigits();
if (pick == 1) {
sortByFirstName();
printList();
} else if (pick == 2) {
sortByLastName();
printList();
} else if (pick == 3) {
sortByAge();
printList();
} else if (pick == 4) {
sortByIdAscending();
printList();
} else if (pick == 5) {
sortByIdDescending();
printList();
} else if (pick == 6) {
sortByDepartment();
printList();
} else if (pick == 0) {
printMainMenu();
} else {
System.out.println("Please, make a selection [0-6]");
}
}
}
public void printSortMenu() {
System.out.println("====================================");
System.out.println("| SORT MENU |");
System.out.println("====================================");
System.out.println("| [1] Sort by first name |");
System.out.println("| [2] Sort by last name |");
System.out.println("| [3] Sort by age |");
System.out.println("| [4] Sort by id descending |");
System.out.println("| [5] Sort by id ascending |");
System.out.println("| [6] Sort by department |");
System.out.println("====================================");
System.out.println("Please, make a selection [0-6]");
System.out.println("Press 0 for back to MAIM MENU");
}
public void readList() throws IOException {
try{
BufferedReader ordersReader = new BufferedReader(new FileReader("employees.log"));
String line;
ordersReader.readLine();
while ((line = ordersReader.readLine()) != null) {
//split string line to get fields without separator
if (line.substring(line.lastIndexOf(separator) + 1).toLowerCase().equals("it")) {
employeesList.add(new ITperson(line, separator));
} else {
employeesList.add(new FINperson(line, separator));
}
}
}catch (Exception e) {
System.out.println("FILE NOT FOUND");
}
}
public int checkInputDigits() {//check input symbols are digits
Scanner sc = new Scanner(System.in);
Integer pick = null;//create empty object Integer type
while (pick == null) {//do while object is empty
try {
String str = sc.nextLine();//read string from console
pick = Integer.valueOf(str);//convert string to int
} catch (Exception e) {
System.out.println("Only digits allowed!");
}
}
return pick;
}
public String checkInputForLetters() throws IOException, ClassNotFoundException {
//scan string input
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//Create a Pattern object
Pattern p = Pattern.compile("[a-zA-Z]+");
//use to match the regular expression used to create it against different input
while (!p.matcher(str).matches()) {//while str not (!) match pattern
System.out.println("Should contain letters only!");
str = sc.nextLine();
}
return str;
}
public void printList() throws IOException {
Scanner scn = new Scanner(System.in);
if (employeesList.size() == 0) {
System.out.println("LIST IS EMPTY");
}
else {for (Person list : employeesList) {
System.out.println(list.toString());
}
System.out.println("press any key for back to SORT MENU");
scn.nextLine();//line for input enter (pause before read enter)
}
}
public void writeCsvFile() throws IOException {
String NEW_LINE_SEPARATOR = "\n";
String FILE_HEADER = "first_name,last_name,age,id,department";
FileWriter fileWriter = null;
fileWriter = new FileWriter("employees.log");
fileWriter.append(FILE_HEADER);
fileWriter.append(NEW_LINE_SEPARATOR);
for (Person person : employeesList) {
fileWriter.append(person.toCSV(separator));
fileWriter.append(NEW_LINE_SEPARATOR);
}
fileWriter.flush();
fileWriter.close();
}
/*--- Start Sort using comparator and collections
Compare 2 objects with arguments they both inherit from parent class*/
class AgeComparator implements Comparator<Person> {
public int compare(Person emp1, Person emp2) {
int emp1Age = emp1.age;
int emp2Age = emp2.age;
return emp1Age - emp2Age;
}
}
class LastNameComparator implements Comparator<Person> {
public int compare(Person emp1, Person emp2) {
return emp1.lastName.compareTo(emp2.lastName);
}
}
class FirstNameComparator implements Comparator<Person> {
public int compare(Person emp1, Person emp2) {
return emp1.firstName.compareTo(emp2.firstName);
}
}
class DepartmentComparator implements Comparator<Person> {
public int compare(Person emp1, Person emp2) {
//compare 2 objects (ITperson and FINperson)
return emp1.getClass().toString().compareTo(emp2.getClass().toString());
}
}
// Sorts the vector list using comparator
public void sortByDepartment() {
Collections.sort(employeesList, new DepartmentComparator());
}
public void sortByFirstName() {
Collections.sort(employeesList, new FirstNameComparator());
}
public void sortByLastName() {
Collections.sort(employeesList, new LastNameComparator());
}
public void sortByAge() {
Collections.sort(employeesList, new AgeComparator());
}
//--- End Sort using comparator and collections
//Bubble Sort
public void sortByIdDescending() {
for (int i = 1; i < employeesList.size(); i++) {
for (int x = 0; x < employeesList.size() - 1; x++) {
//compare elements, swap the elements
//Repeat the same steps for vector[x] to vector[size-1]
if (employeesList.elementAt(x).id < employeesList.elementAt(x + 1).id) {
Person temp2 = employeesList.elementAt(x);
employeesList.set(x, employeesList.elementAt(x + 1));
employeesList.set(x + 1, temp2);
}
}
}
}
public void sortByIdAscending() {
for (int i = 1; i < employeesList.size(); i++) {
for (int x = 0; x < employeesList.size() - 1; x++) {
if (employeesList.elementAt(x).id > employeesList.elementAt(x + 1).id) {
Person temp2 = employeesList.elementAt(x);
employeesList.set(x, employeesList.elementAt(x + 1));
employeesList.set(x + 1, temp2);
}
}
}
}
}