forked from code-differently/code-differently-24-q4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds lesson16 homework by Yemi (code-differently#515)
* feat: adds lesson16 homework by Yemi * feat: adds lesson16 homework by Yemi * feat:adds new package name for lesson16 homework by Yemi * feat:adds new package name for lesson16 homework by Yemi
- Loading branch information
Showing
6 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...bjects/objects_app/src/main/java/com/codedifferently/lesson16/studentinfo/Department.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.codedifferently.lesson16.studentinfo; | ||
|
||
public enum Department { | ||
Sales, | ||
Finance, | ||
Technology, | ||
Research, | ||
Administration | ||
} |
96 changes: 96 additions & 0 deletions
96
...6/objects/objects_app/src/main/java/com/codedifferently/lesson16/studentinfo/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.codedifferently.lesson16.studentinfo; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public class Student { | ||
private long studentNumber; | ||
private String fullName; | ||
private Department department; | ||
private String emailAddress; | ||
private List<String> phoneNumbers; | ||
private LocalDate dateOfBirth; | ||
|
||
public Student( | ||
long studentNumber, | ||
String fullName, | ||
Department department, | ||
String emailAddress, | ||
List<String> phoneNumbers, | ||
LocalDate dateOfBirth) { | ||
this.studentNumber = studentNumber; | ||
this.fullName = fullName; | ||
this.department = department; | ||
this.emailAddress = emailAddress; | ||
this.phoneNumbers = phoneNumbers; | ||
this.dateOfBirth = dateOfBirth; | ||
} | ||
|
||
public long getStudentNumber() { | ||
return studentNumber; | ||
} | ||
|
||
public void setStudentNumber(long studentNumber) { | ||
this.studentNumber = studentNumber; | ||
} | ||
|
||
public String getFullName() { | ||
return fullName; | ||
} | ||
|
||
public void setFullName(String fullName) { | ||
this.fullName = fullName; | ||
} | ||
|
||
public Department getDepartment() { | ||
return department; | ||
} | ||
|
||
public void setDepartment(Department department) { | ||
this.department = department; | ||
} | ||
|
||
public String getEmailAddress() { | ||
return emailAddress; | ||
} | ||
|
||
public void setEmailAddress(String emailAddress) { | ||
this.emailAddress = emailAddress; | ||
} | ||
|
||
public List<String> getPhoneNumbers() { | ||
return phoneNumbers; | ||
} | ||
|
||
public void setPhoneNumbers(List<String> phoneNumbers) { | ||
this.phoneNumbers = phoneNumbers; | ||
} | ||
|
||
public LocalDate getDateOfBirth() { | ||
return dateOfBirth; | ||
} | ||
|
||
public void setDateOfBirth(LocalDate dateOfBirth) { | ||
this.dateOfBirth = dateOfBirth; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Student{" | ||
+ "studentNumber=" | ||
+ studentNumber | ||
+ ", fullName='" | ||
+ fullName | ||
+ '\'' | ||
+ ", department=" | ||
+ department | ||
+ ", emailAddress='" | ||
+ emailAddress | ||
+ '\'' | ||
+ ", phoneNumbers=" | ||
+ phoneNumbers | ||
+ ", dateOfBirth=" | ||
+ dateOfBirth | ||
+ '}'; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
..._app/src/main/java/com/codedifferently/lesson16/studentinfo/StudentNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.codedifferently.lesson16.studentinfo; | ||
|
||
public class StudentNotFoundException extends RuntimeException { | ||
public StudentNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ts/objects_app/src/main/java/com/codedifferently/lesson16/studentinfo/StudentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.codedifferently.lesson16.studentinfo; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class StudentService { | ||
|
||
private final Map<Long, Student> students; | ||
|
||
public StudentService() { | ||
students = new HashMap<>(); | ||
} | ||
|
||
private void checkIfStudentExist(long studentNumber) { | ||
if (students.containsKey(studentNumber)) { | ||
return; | ||
} | ||
throw new StudentNotFoundException("Student with number " + studentNumber + " not found"); | ||
} | ||
|
||
public void deleteStudent(long studentNumber) { | ||
checkIfStudentExist(studentNumber); | ||
students.remove(studentNumber); | ||
} | ||
|
||
public void addStudent(Student student) { | ||
students.put(student.getStudentNumber(), student); | ||
} | ||
|
||
public int countNumberOfTechDeptStudents() { | ||
int count = 0; | ||
for (Student student : students.values()) { | ||
if (student.getDepartment().equals(Department.Technology)) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
public Student findStudentByStudentNumber(long studentNumber) { | ||
return students.get(studentNumber); | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
...bjects_app/src/test/java/com/codedifferently/lesson16/studentinfo/StudentServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package com.codedifferently.lesson16.studentinfo; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class StudentServiceTest { | ||
|
||
StudentService classUnderTest; | ||
Map<Long, Student> students; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
classUnderTest = new StudentService(); | ||
|
||
students = new HashMap<>(); | ||
|
||
List<String> phoneNumbers1 = new ArrayList<>(); | ||
phoneNumbers1.add("301-999-3222"); | ||
phoneNumbers1.add("301-211-3222"); | ||
phoneNumbers1.add("301-212-3777"); | ||
|
||
Student student1 = | ||
new Student( | ||
112453698l, | ||
"Jane Doe", | ||
Department.Finance, | ||
"[email protected]", | ||
phoneNumbers1, | ||
LocalDate.of(2021, 11, 20)); | ||
|
||
List<String> phoneNumbers2 = new ArrayList<>(); | ||
phoneNumbers2.add("301-777-3222"); | ||
phoneNumbers2.add("301-777-3222"); | ||
phoneNumbers2.add("301-777-3777"); | ||
|
||
Student student2 = | ||
new Student( | ||
712453698l, | ||
"Johnie Walker", | ||
Department.Technology, | ||
"[email protected]", | ||
phoneNumbers2, | ||
LocalDate.of(2021, 10, 26)); | ||
|
||
List<String> phoneNumbers3 = new ArrayList<>(); | ||
phoneNumbers2.add("303-999-3222"); | ||
phoneNumbers2.add("303-999-3222"); | ||
phoneNumbers2.add("303-999-3777"); | ||
|
||
Student student3 = | ||
new Student( | ||
912453698l, | ||
"Fannie May", | ||
Department.Technology, | ||
"[email protected]", | ||
phoneNumbers3, | ||
LocalDate.of(2017, 9, 26)); | ||
|
||
students.put(112453698l, student1); | ||
students.put(712453698l, student2); | ||
students.put(912453698l, student3); | ||
|
||
classUnderTest.addStudent(student1); | ||
classUnderTest.addStudent(student2); | ||
classUnderTest.addStudent(student3); | ||
} | ||
|
||
@Test | ||
public void testDeleteStudent_throw_Exception_IFNumber_Not_Found() { | ||
// Arrange | ||
Student student = | ||
new Student( | ||
44444444l, | ||
"Gonnie ByeBye", | ||
Department.Technology, | ||
"[email protected]", | ||
null, | ||
LocalDate.of(2019, 10, 21)); | ||
classUnderTest.addStudent(student); | ||
// Act | ||
classUnderTest.deleteStudent(student.getStudentNumber()); | ||
|
||
// Assert | ||
long studentNumber = 44444444l; | ||
assertThatThrownBy(() -> classUnderTest.deleteStudent(studentNumber)) | ||
.isInstanceOf(StudentNotFoundException.class) | ||
.hasMessage("Student with number " + studentNumber + " not found"); | ||
} | ||
|
||
@Test | ||
public void testCountNumberOfTechDeptStudents_if_any() { | ||
Student student = | ||
new Student( | ||
44444444l, | ||
"Gonnie ByeBye", | ||
Department.Technology, | ||
"[email protected]", | ||
null, | ||
LocalDate.of(2019, 10, 21)); | ||
classUnderTest.addStudent(student); | ||
// Act | ||
int numOfTechStudents = classUnderTest.countNumberOfTechDeptStudents(); | ||
|
||
// Assert | ||
int expectedCount = 3; | ||
assertEquals(expectedCount, numOfTechStudents); | ||
} | ||
|
||
@Test | ||
public void testAddStudent() { | ||
// Arrange | ||
Student student = | ||
new Student( | ||
44444444l, | ||
"Gonnie ByeBye", | ||
Department.Technology, | ||
"[email protected]", | ||
null, | ||
LocalDate.of(2019, 10, 21)); | ||
|
||
// Act | ||
classUnderTest.addStudent(student); | ||
|
||
// Assert | ||
int expectedCount = 3; | ||
assertEquals(expectedCount, students.size()); | ||
} | ||
|
||
@Test | ||
public void testFindStudentByStudentNumber_where_student_exist() { | ||
// Arrange | ||
Student student = | ||
new Student( | ||
44444444l, | ||
"Gonnie ByeBye", | ||
Department.Technology, | ||
"[email protected]", | ||
null, | ||
LocalDate.of(2019, 10, 21)); | ||
classUnderTest.addStudent(student); | ||
|
||
// Act | ||
classUnderTest.findStudentByStudentNumber(student.getStudentNumber()); | ||
|
||
// Assert | ||
long expectedStudentNumber = 44444444l; | ||
assertEquals(expectedStudentNumber, student.getStudentNumber()); | ||
} | ||
} |
Oops, something went wrong.