forked from greasymolue/SF-Int-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
58 lines (53 loc) · 1.52 KB
/
Student.java
File metadata and controls
58 lines (53 loc) · 1.52 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
package students2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Student {
private String name;
private double gpa;
private List<String> courses;
private Student(String name, double gpa, List<String> courses) {
if (!isValidStudent(name, gpa)) throw new IllegalArgumentException();
this.name = name;
this.gpa = gpa;
this.courses = new ArrayList<>(courses);
}
private Student(String name, double gpa, String ... courses) {
if (!isValidStudent(name, gpa)) throw new IllegalArgumentException();
this.name = name;
this.gpa = gpa;
// this.courses = List.of(courses);
this.courses = Arrays.asList(courses);
}
public static boolean isValidStudent(String name, double gpa) {
return ((name != null) && (gpa >= 0 && gpa <= 4.0));
}
public static Student of(String name, double gpa, String ... courses) {
return new Student(name, gpa, courses);
}
public String getName() {
return name;
}
public double getGpa() {
return gpa;
}
public Student withGpa(double gpa) {
if (isValidStudent(this.name, gpa)) {
return new Student(this.name, gpa, this.courses);
} else {
throw new IllegalArgumentException("bad gpa");
}
}
public List<String> getCourses() {
return Collections.unmodifiableList(courses);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", gpa=" + gpa +
", courses=" + courses +
'}';
}
}