-
Notifications
You must be signed in to change notification settings - Fork 0
Main task for module5. #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
675fef4
Main task for module5.
5b226c5
Ignore .iml file
dab66ff
Correction without testing data tast
0793357
Module5 task with corrections
9b3e7c6
Merge branch 'main' into module5
Fentezi3 c27a5ad
Resolved conflict and add corrects
fc6f310
Merge branch 'module5' of github.com:Fentezi3/Java into module5
2a0c13c
Variate facultyLists for all exceptions
24cfc65
Lists refactoring
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,3 +11,5 @@ buildNumber.properties | |
| .mvn/wrapper/maven-wrapper.jar | ||
| .idea/ | ||
| **/*.iml | ||
| **/*.log | ||
|
|
||
This file contains hidden or 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,23 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>org.example</groupId> | ||
| <artifactId>module5</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>8</source> | ||
| <target>8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
This file contains hidden or 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,232 @@ | ||
| import bean.Faculty; | ||
| import bean.Group; | ||
| import bean.Student; | ||
| import exceptions.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| List<List> facultiesList = new ArrayList() {{ | ||
| add(createFacultyList()); | ||
| add(emptyFacultyList()); | ||
| add(facultyWithoutGroup()); | ||
| add(groupWithoutStudents()); | ||
| add(studentWithoutSubjects()); | ||
| }}; | ||
| for (List faculty : facultiesList) { | ||
| try { | ||
| validateFacultyGroupStudentsSubjectIsEmpty(faculty); | ||
| calculateAverageGradeForEachStudent(faculty); | ||
| System.out.println(); | ||
| calculateAverageGradeInOneGroup(faculty, "Math", 1, "English"); | ||
| System.out.println(); | ||
| calculateAverageGradeForOneSubjectInUniversity(faculty, "Math"); | ||
| } catch (MyException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validation what this data is not null. | ||
dKatechev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @param facultyList all faculties in university. | ||
| * @throws EmptyFacultyListException if faculty list is empty. | ||
| * @throws EmptyFacultyGroupsException if faculty doesn't have groups. | ||
| * @throws EmptyStudentListException if group doesn't have students. | ||
| * @throws EmptySubjectException if student doesn't have subjects. | ||
| */ | ||
| private static void validateFacultyGroupStudentsSubjectIsEmpty(List<Faculty> facultyList) throws MyException { | ||
dKatechev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (facultyList.isEmpty()) { | ||
| throw new EmptyFacultyListException("There is no faculties in university."); | ||
| } | ||
| for (Faculty faculty : facultyList) { | ||
| if (faculty.getGroups().isEmpty()) { | ||
| throw new EmptyFacultyGroupsException("There are no group in " + faculty.getName() + " faculty"); | ||
| } | ||
| for (Group group : faculty.getGroups()) { | ||
| if (group.getStudentList().isEmpty()) { | ||
| throw new EmptyStudentListException("There are no students in " + group.getName() + " group."); | ||
| } | ||
| for (Student students : group.getStudentList()) { | ||
| if (students.getSubjects().isEmpty()) { | ||
| throw new EmptySubjectException(students.getName() + " has no subjects"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calculate average grade for each student for all subjects. | ||
| * | ||
| * @param facultyList all faculties in university. | ||
| */ | ||
| private static void calculateAverageGradeForEachStudent(List<Faculty> facultyList) { | ||
dKatechev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| List<Student> studentsList = new ArrayList(); | ||
| for (Faculty faculty : facultyList) { | ||
| for (Group group : faculty.getGroups()) { | ||
| studentsList.addAll(group.getStudentList()); | ||
| } | ||
| } | ||
| for (Student student : studentsList) { | ||
| Map<String, Integer> subjects = student.getSubjects(); | ||
| int sum = student.getSubjects().values().stream().mapToInt(Integer::intValue).sum(); | ||
| float average = sum / (float) subjects.size(); | ||
| String textPrint = String.format("%s's average grade: %s%n", student.getName(), average); | ||
| System.out.println(textPrint); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calculation average grade in given faculty and group, on a given subject. | ||
| * | ||
| * @param facultyList all faculty in university. | ||
| * @param facultyName given faculty for calculation. | ||
| * @param groupName given group for calculation. | ||
| * @param subjectName given subject for calculation. | ||
| */ | ||
| private static void calculateAverageGradeInOneGroup(List<Faculty> facultyList, String facultyName, | ||
| int groupName, String subjectName) { | ||
| float sum = 0; | ||
| float average; | ||
| int counter = 0; | ||
| List<Student> studentList = new ArrayList<>(); | ||
| for (Faculty faculty : facultyList) { | ||
| if (faculty.getName().equals(facultyName)) { | ||
| for (Group group : faculty.getGroups()) { | ||
| if (group.getName() == groupName) { | ||
| studentList = group.getStudentList(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| for (Student student : studentList) { | ||
| Map<String, Integer> subjects = student.getSubjects(); | ||
| if (subjects.containsKey(subjectName)) { | ||
| sum += subjects.get(subjectName); | ||
| counter++; | ||
| } | ||
| } | ||
| average = sum / counter; | ||
| System.out.println("Average grade in " + facultyName + " faculty, " + groupName + | ||
| " group, for " + subjectName + " : " + average); | ||
| } | ||
|
|
||
| /** | ||
| * Calculation average grade for given subject in all university. | ||
| * | ||
| * @param facultyList all faculty in university. | ||
| * @param subjectName given subject. | ||
| */ | ||
| private static void calculateAverageGradeForOneSubjectInUniversity(List<Faculty> facultyList, String subjectName) { | ||
| float sum = 0; | ||
| int counter = 0; | ||
| for (Faculty faculty : facultyList) { | ||
| for (Group group : faculty.getGroups()) { | ||
| for (Student students : group.getStudentList()) { | ||
| Map<String, Integer> subjects = students.getSubjects(); | ||
| if (subjects.containsKey(subjectName)) { | ||
| sum += subjects.get(subjectName); | ||
| counter++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| float average = sum / counter; | ||
| System.out.println("Average grade for " + subjectName + " in all university is - " + average); | ||
| } | ||
|
|
||
| private static List<Faculty> createFacultyList() { | ||
| Map<String, Integer> subjects1 = new HashMap<String, Integer>(); | ||
| subjects1.put("Math", 7); | ||
| subjects1.put("English", 6); | ||
| subjects1.put("Physic", 4); | ||
| Map<String, Integer> subjects2 = new HashMap<String, Integer>(); | ||
| subjects2.put("Math", 5); | ||
| subjects2.put("English", 6); | ||
| subjects2.put("Physic", 8); | ||
| Map<String, Integer> subjects3 = new HashMap<String, Integer>(); | ||
| subjects3.put("Math", 3); | ||
| subjects3.put("English", 3); | ||
| subjects3.put("Physic", 9); | ||
| Map<String, Integer> subjects4 = new HashMap<String, Integer>(); | ||
| subjects4.put("Math", 9); | ||
| subjects4.put("English", 9); | ||
| subjects4.put("Physic", 6); | ||
|
|
||
| Student student1 = new Student("John", subjects1); | ||
| Student student2 = new Student("Bob", subjects2); | ||
| Student student3 = new Student("Sue", subjects3); | ||
| Student student4 = new Student("Charlie", subjects4); | ||
|
|
||
| List<Student> group1Students = new ArrayList<Student>(); | ||
| group1Students.add(student1); | ||
| group1Students.add(student2); | ||
| List<Student> group2Students = new ArrayList<Student>(); | ||
| group2Students.add(student3); | ||
| List<Student> group3Students = new ArrayList<Student>(); | ||
| group3Students.add(student4); | ||
|
|
||
| Group group1 = new Group(1, group1Students); | ||
| Group group2 = new Group(2, group2Students); | ||
| Group group3 = new Group(3, group3Students); | ||
|
|
||
| List<Group> mathFacultyGroups = new ArrayList<Group>(); | ||
| mathFacultyGroups.add(group1); | ||
| List<Group> humanityFacultyGroups = new ArrayList<Group>(); | ||
| humanityFacultyGroups.add(group2); | ||
| humanityFacultyGroups.add(group3); | ||
|
|
||
| Faculty mathFaculty = new Faculty("Math", mathFacultyGroups); | ||
| Faculty humanityFaculty = new Faculty("Humanity", humanityFacultyGroups); | ||
|
|
||
| List<Faculty> facultyList = new ArrayList<Faculty>(); | ||
| facultyList.add(mathFaculty); | ||
| facultyList.add(humanityFaculty); | ||
|
|
||
| return facultyList; | ||
| } | ||
|
|
||
| private static List<Faculty> emptyFacultyList() { | ||
| List<Faculty> emptyFacultyList = new ArrayList<>(); | ||
| return emptyFacultyList; | ||
| } | ||
|
|
||
| private static List<Faculty> facultyWithoutGroup() { | ||
| List<Group> historyFacultyGroups = new ArrayList<>(); | ||
| Faculty historyFaculty = new Faculty("Historical", historyFacultyGroups); | ||
| List<Faculty> facultyWithoutGroup = new ArrayList<>(); | ||
| facultyWithoutGroup.add(historyFaculty); | ||
| return facultyWithoutGroup; | ||
| } | ||
|
|
||
| private static List<Faculty> groupWithoutStudents() { | ||
| List<Student> group1Students = new ArrayList<Student>(); | ||
| Group group1 = new Group(1, group1Students); | ||
| List<Group> mathFacultyGroups = new ArrayList<Group>(); | ||
| mathFacultyGroups.add(group1); | ||
| Faculty mathFaculty = new Faculty("Math", mathFacultyGroups); | ||
| List<Faculty> groupWithoutStudents = new ArrayList<>(); | ||
| groupWithoutStudents.add(mathFaculty); | ||
| return groupWithoutStudents; | ||
| } | ||
|
|
||
| private static List<Faculty> studentWithoutSubjects() { | ||
| Map<String, Integer> subjects1 = new HashMap<String, Integer>(); | ||
| Student student1 = new Student("John", subjects1); | ||
| List<Student> group1Students = new ArrayList<Student>(); | ||
| group1Students.add(student1); | ||
| Group group1 = new Group(1, group1Students); | ||
| List<Group> mathFacultyGroups = new ArrayList<Group>(); | ||
| mathFacultyGroups.add(group1); | ||
| Faculty mathFaculty = new Faculty("Math", mathFacultyGroups); | ||
| List<Faculty> studentWithoutSubjects = new ArrayList<>(); | ||
| studentWithoutSubjects.add(mathFaculty); | ||
| return studentWithoutSubjects; | ||
| } | ||
| } | ||
This file contains hidden or 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,37 @@ | ||
| package bean; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Faculty { | ||
| private String name; | ||
| private List<Group> groups; | ||
|
|
||
| public Faculty(String facultyName, List<Group> groups) { | ||
| this.name = facultyName; | ||
| this.groups = groups; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public List<Group> getGroups() { | ||
| return groups; | ||
| } | ||
|
|
||
| public void setGroups(List<Group> groups) { | ||
| this.groups = groups; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Faculty{" + | ||
| "facultyName='" + name + '\'' + | ||
| ", groups=" + groups + | ||
| '}'; | ||
| } | ||
| } |
This file contains hidden or 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,37 @@ | ||
| package bean; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Group { | ||
| private int name; | ||
| private List<Student> studentList; | ||
|
|
||
| public Group(int groupName, List<Student> studentsList) { | ||
| this.name = groupName; | ||
| this.studentList = studentsList; | ||
| } | ||
|
|
||
| public int getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(int name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public List<Student> getStudentList() { | ||
| return studentList; | ||
| } | ||
|
|
||
| public void setStudentList(List<Student> studentList) { | ||
| this.studentList = studentList; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Group{" + | ||
| "groupName=" + name + | ||
| ", studentsList=" + studentList + | ||
| '}'; | ||
| } | ||
| } |
This file contains hidden or 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,47 @@ | ||
| package bean; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class Student { | ||
| private String name; | ||
| private Map<String, Integer> subjects; | ||
|
|
||
| public Student(String studentName, Map<String, Integer> subjects) { | ||
| validateSubjects(subjects); | ||
| this.name = studentName; | ||
| this.subjects = subjects; | ||
| } | ||
|
|
||
| private void validateSubjects(Map<String, Integer> subjects) { | ||
| for (Integer grade : subjects.values()) { | ||
| if (grade < 0 || grade > 10) { | ||
| throw new IllegalStateException("Grade is less than 0 or more than 10"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Map<String, Integer> getSubjects() { | ||
| return subjects; | ||
| } | ||
|
|
||
| public void setSubjects(Map<String, Integer> subjects) { | ||
| validateSubjects(subjects); | ||
| this.subjects = subjects; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Student{" + | ||
| "studentName='" + name + '\'' + | ||
| ", subject=" + subjects + | ||
| '}'; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
module5/src/main/java/exceptions/EmptyFacultyGroupsException.java
This file contains hidden or 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 exceptions; | ||
|
|
||
| public class EmptyFacultyGroupsException extends MyException { | ||
| public EmptyFacultyGroupsException(String message) { | ||
| super(message); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.