Skip to content

Commit 718843a

Browse files
committed
Day 2 Lab 1
1 parent 01c5965 commit 718843a

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package superiterable;
2+
3+
import students.Student;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class SILab {
9+
private static final SuperIterable<Student> roster =
10+
new SuperIterable<>(List.of(
11+
students.Student.of("Fred", 3.4, "Math", "Physics"),
12+
students.Student.of("Jim", 2.4, "Journalism"),
13+
Student.of("Sheila", 3.9, "Math", "Physics", "Quantum Mechanics", "Astrophysics")
14+
));
15+
private static final Map<String, Integer> courseLevel = Map.of(
16+
"Physics", 100,
17+
"Math", 200,
18+
"Journalism", 300,
19+
"Astrophysics", 300,
20+
"Quantum Mechanics", 400
21+
);
22+
23+
private static final Map<String, Integer> creditHours = Map.of(
24+
"Physics", 4,
25+
"Math", 3,
26+
"Journalism", 4,
27+
"Astrophysics", 3,
28+
"Quantum Mechanics", 2
29+
);
30+
31+
public static void main(String[] args) {
32+
System.out.println("All students");
33+
roster
34+
// Add code HERE ONLY, so as to print
35+
// "Student: <name> has gpa: <gpa>" for all students
36+
37+
.forEach(s -> System.out.println(s));
38+
39+
System.out.println("gpa below 3.5");
40+
roster
41+
// Add code HERE ONLY, so as to print
42+
// "Student: <name> has gpa: <gpa>" for all students with gpa less than 3.5
43+
44+
.forEach(s -> System.out.println(s));
45+
46+
System.out.println("Takes more than 1 course");
47+
roster
48+
// Add code HERE ONLY, so as to print
49+
// "Student: <name> takes <count> courses" for all students taking more than 1 course
50+
51+
.forEach(s -> System.out.println(s));
52+
53+
System.out.println("Studying advanced courses");
54+
roster
55+
// Add code HERE ONLY, so as to print
56+
// "Student: <name> takes advanced courses" for all students taking a course at 300+ level
57+
58+
.forEach(s -> System.out.println(s));
59+
60+
System.out.println("Course load");
61+
roster
62+
// Add code HERE ONLY, so as to print
63+
// "Student: <name> takes <total> credit hours" for all students
64+
65+
.forEach(s -> System.out.println(s));
66+
67+
}
68+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package superiterable;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class Student {
9+
private String name;
10+
private double gpa;
11+
private List<String> courses;
12+
13+
private Student(String name, double gpa, List<String> courses) {
14+
if (!isValidStudent(name, gpa)) throw new IllegalArgumentException();
15+
this.name = name;
16+
this.gpa = gpa;
17+
this.courses = new ArrayList<>(courses);
18+
}
19+
private Student(String name, double gpa, String ... courses) {
20+
if (!isValidStudent(name, gpa)) throw new IllegalArgumentException();
21+
this.name = name;
22+
this.gpa = gpa;
23+
this.courses = List.of(courses);
24+
}
25+
26+
public static boolean isValidStudent(String name, double gpa) {
27+
return ((name != null) && (gpa >= 0 && gpa <= 4.0));
28+
}
29+
public static Student of(String name, double gpa, String ... courses) {
30+
return new Student(name, gpa, courses);
31+
}
32+
public String getName() {
33+
return name;
34+
}
35+
public double getGpa() {
36+
return gpa;
37+
}
38+
public Student withGpa(double gpa) {
39+
if (isValidStudent(this.name, gpa)) {
40+
return new Student(this.name, gpa, this.courses);
41+
} else {
42+
throw new IllegalArgumentException("bad gpa");
43+
}
44+
}
45+
public List<String> getCourses() {
46+
return Collections.unmodifiableList(courses);
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "Student{" +
52+
"name='" + name + '\'' +
53+
", gpa=" + gpa +
54+
", courses=" + courses +
55+
'}';
56+
}
57+
}

0 commit comments

Comments
 (0)