-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.java
167 lines (136 loc) · 4.38 KB
/
Database.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
package application;
// Eduardo Cruz
// CS2012-2
// Description: The Database class is a subclass of an ArrayList consisting of the Person class. It can have the superclass
// refer to subclasses through the ArrayList (Polymorphism). This database also contains methods for printing a specific group
// of people such as students, faculty, or staff.
import java.util.ArrayList;
@SuppressWarnings("serial")
public class Database extends ArrayList<Person> {
// Default constructor
public Database() {
}
// Method to print the data of all employees in database
public void printEmployees() {
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Employee) {
System.out.println(get(i));
}
}
}
// Method to print the data of all students in database
public void printStudents() {
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Student) {
System.out.println(get(i));
}
}
}
// Method to print the data of all faculty in database
public void printFaculty() {
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Faculty) {
System.out.println(get(i));
}
}
}
// Method to print the data of all staff in database
public void printStaff() {
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Staff) {
System.out.println(get(i));
}
}
}
// Getter method that returns the number of objects that are an instance of the Person class
public int getNumberOfPeople() {
int numberOfPeople = 0;
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Person) {
numberOfPeople++;
}
}
return numberOfPeople;
}
// Getter method that returns the number of objects that are an instance of the Student class
public int getNumberOfStudents() {
int numberOfStudents = 0;
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Student) {
numberOfStudents++;
}
}
return numberOfStudents;
}
// Another getter method that returns the number of objects that are an instance of the Employees class
public int getNumberOfEmployees() {
int numberOfEmployees = 0;
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Employee) {
numberOfEmployees++;
}
}
return numberOfEmployees;
}
// And another getter method that returns the number of objects that are an instance of the Staff class
public int getNumberOfStaff() {
int numberOfStaff = 0;
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Staff) {
numberOfStaff++;
}
}
return numberOfStaff;
}
// Getter method that returns the number of objects that are an instance of the Faculty class
public int getNumberOfFaculty() {
int numberOfFaculty = 0;
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Faculty) {
numberOfFaculty++;
}
}
return numberOfFaculty;
}
// Unlike the previous getter methods, this getter returns the number of students whose class standing matches
// with the parameter entered by the user
public int getNumberOfStudentsByClassStanding(String classStanding) {
int numberOfStudents = 0;
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Student) {
Student other = (Student)get(i);
if(other.getClassStanding() == classStanding) {
numberOfStudents++;
}
}
}
return numberOfStudents;
}
// This method prints the data of all students who have a GPA that is greater than or equal to the parameter entered by the user
public void printStudentsGreaterThanOrEqualToGPA(double gpa) {
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Student) {
Student other = (Student)get(i);
if(other.getGpa() >= gpa)
System.out.println(get(i));
}
}
}
// This method prints the data of all students who have a GPA that is less than or equal to the parameter entered by the user
public void printStudentsLessThanOrEqualToGPA(double gpa) {
for(int i = 0; i < size(); i++) {
if(get(i) instanceof Student) {
Student other = (Student)get(i);
if(other.getGpa() <= gpa) {
System.out.println(get(i));
}
}
}
}
public String toString() {
String result = "";
for(int i = 0; i < size(); i++)
result += get(i);
return result;
}
}