forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggregation.java
More file actions
114 lines (67 loc) · 1.9 KB
/
Aggregation.java
File metadata and controls
114 lines (67 loc) · 1.9 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
package oopsconcepts;
import java.util.ArrayList;
import java.util.List;
class Student {
String name;
int id;
String dept;
Student(String name, int id, String dept) {
this.name = name;
this.id = id;
this.dept = dept;
}
}
class Department {
String name;
private List<Student> students;
Department(String name, List<Student> students) {
this.name = name;
this.students = students;
}
public List<Student> getStudents() {
return students;
}
}
class Institute {
String instituteName;
private List<Department> departments;
Institute(String instituteName, List<Department> departments) {
this.instituteName = instituteName;
this.departments = departments;
}
public int getTotalStudentsInInstitute() {
int noOfStudents = 0;
List<Student> students;
for(Department dept: departments) {
students = dept.getStudents();
for(Student s: students) {
noOfStudents++;
}
}
return noOfStudents;
}
}
public class Aggregation {
public static void main(String[] args) {
Student s1 = new Student("Mia", 1, "CSE");
Student s2 = new Student("Priya", 2, "CSE");
Student s3 = new Student("John", 1, "EE");
Student s4 = new Student("Rahul", 1, "EE");
// Making list of CSE Students
List<Student> cse_students = new ArrayList<Student>();
cse_students.add(s1);
cse_students.add(s2);
// Making List of EE Students
List<Student> ee_students = new ArrayList<Student>();
ee_students.add(s3);
ee_students.add(s4);
Department CSE = new Department("CSE", cse_students);
Department EE = new Department("EE", ee_students);
List<Department> departments = new ArrayList<Department>();
departments.add(CSE);
departments.add(EE);
// creating an instance of Institute.
Institute institute = new Institute("BITS", departments);
System.out.println("Total students in institute are: "+ institute.getTotalStudentsInInstitute());
}
}