-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassroom.java
More file actions
77 lines (69 loc) · 1.69 KB
/
Classroom.java
File metadata and controls
77 lines (69 loc) · 1.69 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
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;
public class Classroom
{
private int _lastStudentId = 0;
private ArrayList<Student> _students = new ArrayList<Student>();
ReentrantLock lock = new ReentrantLock();
public Classroom()
{
AddStudent("Bob", 2.75);
AddStudent("Steve", 2.75);
AddStudent("Mike", 2.75);
AddStudent("Dave", 2.75);
}
public int AddStudent(String name, double gpa)
{
int id = IncrementId();
Student student = new Student(name, id, gpa);
_students.add(student);
return id;
}
public Student FindStudentById(int id)
{
for (Student student : _students)
{
if (student.getId()==id)
{
return student;
}
}
return null;
}
public int AddStudent(Student student)
{
return AddStudent(student.getName(), student.getGPA());
}
private int IncrementId()
{
lock.lock();
_lastStudentId++;
lock.unlock();
return _lastStudentId;
}
public ArrayList<Student> getStudents()
{
return _students;
}
public Student getStudent(int id)
{
for (Student student : _students)
{
if (student.getId() == id)
{
return student;
}
}
return null;
}
public String getAverageGpa()
{
double averageGpa = 0.0;
for (Student student : _students)
{
averageGpa += student.getGPA();
}
averageGpa = averageGpa / _students.size();
return Double.toString(averageGpa);
}
}