-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathEducationSystem.java
More file actions
134 lines (100 loc) · 3.63 KB
/
EducationSystem.java
File metadata and controls
134 lines (100 loc) · 3.63 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
import java.util.ArrayList;
interface EducationalInstitution {
String displayInfo();
}
abstract class University implements EducationalInstitution {
public String name;
public int studentCount;
public double tuitionFee;
public ArrayList<Student> students;
public boolean transferStudent(Student student, University toUniversity) {
if (this.students.contains(student)) {
this.students.remove(student);
this.studentCount--;
toUniversity.students.add(student);
toUniversity.studentCount++;
student.setUniversity(toUniversity);
return true;
}
return false;
}
University(String name, double tuitionFee) {
this.name = name;
this.tuitionFee = tuitionFee;
this.students = new ArrayList<>();
this.studentCount = 0;
}
}
class Student implements EducationalInstitution {
private String name;
public University university;
private double balance;
public Student(String name, double balance, University university) {
setName(name);
this.balance=balance;
if(balance >= university.tuitionFee){
this.university=university;
university.students.add(this);
university.studentCount++;
this.balance -= university.tuitionFee;
} else {System.out.println("Not enough balance");}
}
@Override
public String displayInfo() {
if (getUniversity() == null){return "Name: " + getName() + "\nUniversity: " + "Not Enrolled" + "\nBalance: " + getBalance();}
else return "Name: " + this.name + "\nUniversity: " + getUniversity().name + "\nBalance: " + getBalance();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public University getUniversity() {
return university;
}
public void setUniversity(University toUniversity) {
this.university=toUniversity;
}
public double getBalance() {
return balance;
}
}
class SbuUniversity extends University {
public SbuUniversity(String name, double tuitionFee) {
super(name,tuitionFee);
}
@Override
public String displayInfo() {
return "SBU University\nTuition Fee: " + String.format("%.1f", this.tuitionFee) + "\nStudent Count: " + this.studentCount; }
}
class SutUniversity extends University {
SutUniversity(String name, double tuitionFee) {
super(name,tuitionFee);
}
@Override
public String displayInfo() {
return "SUT University\nTuition Fee: " + String.format("%.1f", this.tuitionFee) + "\nStudent Count: " + this.studentCount;
}
}
public class EducationSystem {
public static void main(String[] args) {
SbuUniversity sbu = new SbuUniversity("Shahid Beheshti University", 1500.0);
SutUniversity sut = new SutUniversity("Sharif University of Technology", 2000.0);
Student s1 = new Student("Ali", 3000.0, sbu);
Student s2 = new Student("Sara", 1000.0, sut);
System.out.println(s1.displayInfo());
System.out.println(s2.displayInfo());
System.out.println(sbu.displayInfo());
System.out.println(sut.displayInfo());
boolean transferred = sbu.transferStudent(s1, sut);
if (transferred) {
System.out.println("Student transferred successfully.");
} else {
System.out.println("Student transfer failed.");
}
System.out.println(s1.displayInfo());
System.out.println(sbu.displayInfo());
System.out.println(sut.displayInfo());
}
}