-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentRepository.java
More file actions
113 lines (101 loc) · 3.66 KB
/
StudentRepository.java
File metadata and controls
113 lines (101 loc) · 3.66 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
package repositories;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import services.JsonFileService;
import services.StudentService;
public class StudentRepository implements StudentService {
private final JsonFileService jfs;
public StudentRepository() {
jfs = new JsonFileRepository("resources/databases/users.json");
}
@Override
public boolean getLogin(String username, String password) {
List<String> keys = Arrays.asList(new String[]{"username", "password"});
Map<String, String> conditions = new HashMap<String, String>() {
{
put("username", username);
put("password", password);
}
};
List<Map<String, String>> students = jfs.getAllRowByCondition(keys, conditions);
if (!students.isEmpty() && students.size() >= 2) {
Map<String, String> usernameMap = new HashMap<String, String>() {
{
put("username", username);
}
};
Map<String, String> passwordMap = new HashMap<String, String>() {
{
put("password", password);
}
};
if (students.contains(usernameMap) && students.contains(passwordMap)) {
return true;
}
}
return false;
}
@Override
public boolean addStudent(String username, String password) {
try {
Map<String, String> map = new HashMap<>();
map.put("username", username);
map.put("password", password);
map.put("marks", "");
map.put("role", "student");
return true;
} catch (Exception ex) {
System.out.println("Student Addition Failed. Exception occured");
System.out.println(ex.getMessage());
ex.printStackTrace();
return false;
}
}
@Override
public List<Map<String, String>> getAllStudent() {
try {
List<Map<String, String>> list = new ArrayList<>();
List<String> keys = new ArrayList<>();
keys.add("username");
keys.add("marks");
list = jfs.getAllRow(keys);
if (list.isEmpty()) {
System.out.println("No student found");
return null;
}
return list;
} catch (Exception ex) {
System.out.println("Student Retrival Failed. Exception occured");
System.out.println(ex.getMessage());
ex.printStackTrace();
return null;
}
}
@Override
public boolean updateStudentMarks(String username, String marks) {
try {
List<String> keys = new ArrayList<>();
keys.add("username");
keys.add("marks");
Map<String, String> conditions = new HashMap<>();
conditions.put("username", username);
Map<String, String> updatedValues = new HashMap<>();
updatedValues.put("marks", marks);
System.out.println("Current Marks Is : "+marks);
boolean isUpdated = jfs.updateAllRowByCondition(keys, conditions, updatedValues);
if (isUpdated) {
System.out.println("Student's marks updated");
return true;
}
return false;
} catch (Exception ex) {
System.out.println("Student's marks update Failed. Exception occured");
System.out.println(ex.getMessage());
ex.printStackTrace();
return false;
}
}
}