Repository: CorporationX/god_bless
Pull request #19164: finished
Author: login: pirojok-yt
Created: 2025-04-13T13:03:31Z | Merged: 2025-04-17T06:24:04Z



Diff:
diff --git a/src/main/java/school/faang/Main.java b/src/main/java/school/faang/Main.java
deleted file mode 100644
index 1df1ac32f83..00000000000
--- a/src/main/java/school/faang/Main.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package school.faang;
-
-public class Main{
-
-}
diff --git a/src/main/java/school/faang/bjs2_68925/Main.java b/src/main/java/school/faang/bjs2_68925/Main.java
new file mode 100644
index 00000000000..e834d85d751
--- /dev/null
+++ b/src/main/java/school/faang/bjs2_68925/Main.java
@@ -0,0 +1,38 @@
+package school.faang.bjs2_68925;
+
+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) {
+        final Student vasya = new Student("вася");
+        final Student anton = new Student("антон");
+        final Student carolina = new Student("каролина");
+        Subject math = new Subject("Математика");
+        Subject english = new Subject("Английский");
+        StudentDataBase studentDataBase = new StudentDataBase();
+        Map<Subject, Integer> test = new HashMap<>();
+        test.put(math, 5);
+        studentDataBase.removeStudentFromSubject(vasya, english);
+        //тесты
+        studentDataBase.newStudent(vasya, test);
+        studentDataBase.newSubjectForStudent(vasya, english, 4);
+        studentDataBase.printAllStudents();
+        studentDataBase.removeStudent(vasya);
+        studentDataBase.newStudent(anton, test);
+        studentDataBase.newSubjectForStudent(anton, english, 12);
+        studentDataBase.printAllStudents();
+        List<Student> studList = new ArrayList<>();
+        studList.add(anton);
+        studList.add(vasya);
+        studentDataBase.addNewSubject(english, studList, List.of(3, 4));
+        studentDataBase.newStudentForSubj(carolina, english, 5);
+        studentDataBase.newSubjectForStudent(anton, math, 5);
+        studentDataBase.printAllSubj();
+        studentDataBase.removeStudentFromSubject(anton, english);
+        studentDataBase.printAllSubj();
+        studentDataBase.printAllStudents();
+    }
+}
diff --git a/src/main/java/school/faang/bjs2_68925/Student.java b/src/main/java/school/faang/bjs2_68925/Student.java
new file mode 100644
index 00000000000..6def22bffd4
--- /dev/null
+++ b/src/main/java/school/faang/bjs2_68925/Student.java
@@ -0,0 +1,4 @@
+package school.faang.bjs2_68925;
+
+public record Student(String name) {
+}
diff --git a/src/main/java/school/faang/bjs2_68925/StudentDataBase.java b/src/main/java/school/faang/bjs2_68925/StudentDataBase.java
new file mode 100644
index 00000000000..3c197de840e
--- /dev/null
+++ b/src/main/java/school/faang/bjs2_68925/StudentDataBase.java
@@ -0,0 +1,72 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrades) {
+        studentSubjects.put(student, subjectGrades);
+        for (Subject subject : subjectGrades.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStudent(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(existedStudent -> existedStudent.equals(student));
+        }
+    }
+
+    public void printAllStudents() {
+        for (Map.Entry<Student, Map<Subject, Integer>> entry : studentSubjects.entrySet()) {
+            System.out.println(entry.getKey().name() + ": ");
+            for (Map.Entry<Subject, Integer> subjGrade : entry.getValue().entrySet()) {
+                System.out.println("Subject: " + subjGrade.getKey().name() + '\n'
+                        + "Grade: " + subjGrade.getValue());
+            }
+        }
+    }
+
+    public void addNewSubject(Subject subject, List<Student> students, List<Integer> grades) {
+        subjectStudents.put(subject, students);
+        for (int i = 0; i < students.size(); i++) {
+            studentSubjects.computeIfAbsent(students.get(i), a -> new HashMap<>()).put(subject, grades.get(i));
+        }
+    }
+
+    public void newStudentForSubj(Student student, Subject subject, int grade) {
+        subjectStudents.get(subject).add(student);
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+    }
+
+    public void removeStudentFromSubject(Student student, Subject subject) {
+        List<Student> students = subjectStudents.get(subject);
+        if (students == null) {
+            System.out.println("Subject does not exist in this database");
+        } else {
+            students.remove(student);
+            studentSubjects.get(student).remove(subject);
+        }
+    }
+
+    public void printAllSubj() {
+        for (Map.Entry<Subject, List<Student>> entry : subjectStudents.entrySet()) {
+            System.out.println("Урок: " + entry.getKey().name());
+            System.out.println("Ученики: ");
+            for (Student student : entry.getValue()) {
+                System.out.println(student.name());
+            }
+        }
+    }
+}
diff --git a/src/main/java/school/faang/bjs2_68925/Subject.java b/src/main/java/school/faang/bjs2_68925/Subject.java
new file mode 100644
index 00000000000..1752b0a7add
--- /dev/null
+++ b/src/main/java/school/faang/bjs2_68925/Subject.java
@@ -0,0 +1,4 @@
+package school.faang.bjs2_68925;
+
+public record Subject(String name) {
+}


Reviews:
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T09:56:23Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T09:57:19Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T09:57:55Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T09:58:38Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T09:59:06Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T10:00:07Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T10:01:08Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T10:02:43Z
user: login: SnurnicynKonstantin
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T10:04:18Z
user: login: pirojok-yt
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-16T12:38:49Z
user: login: SnurnicynKonstantin
body: Молодец, что рекорды использовал. С классами, у которых много полей будет получится очень кратко
state: APPROVED
author_association: NONE
submitted_at: 2025-04-17T06:23:56Z

Review comments:
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: subjectGrade -> subjectGrades
created_at: 2025-04-16T09:56:23Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: Не сокращай в именах функций. Stud -> Student
created_at: 2025-04-16T09:57:19Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(s -> s.equals(student));
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: s -> existedStudent
created_at: 2025-04-16T09:57:55Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(s -> s.equals(student));
+        }
+    }
+
+    public void printAllStud() {
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: Stud -> Students
created_at: 2025-04-16T09:58:38Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(s -> s.equals(student));
+        }
+    }
+
+    public void printAllStud() {
+        for (Map.Entry<Student, Map<Subject, Integer>> entry : studentSubjects.entrySet()) {
+            System.out.println(entry.getKey().getName() + ": ");
+            for (Map.Entry<Subject, Integer> subjGrade : entry.getValue().entrySet()) {
+                System.out.println("Предмет: " + subjGrade.getKey().getName() + '\n'
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: Используй латиницу
created_at: 2025-04-16T09:59:06Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(s -> s.equals(student));
+        }
+    }
+
+    public void printAllStud() {
+        for (Map.Entry<Student, Map<Subject, Integer>> entry : studentSubjects.entrySet()) {
+            System.out.println(entry.getKey().getName() + ": ");
+            for (Map.Entry<Subject, Integer> subjGrade : entry.getValue().entrySet()) {
+                System.out.println("Предмет: " + subjGrade.getKey().getName() + '\n'
+                        + "Оценка: " + subjGrade.getValue());
+            }
+        }
+    }
+
+    public void newSubject(Subject subject, List<Student> students, List<Integer> grades) {
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: newSubject -> addNewSubject
created_at: 2025-04-16T10:00:07Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(s -> s.equals(student));
+        }
+    }
+
+    public void printAllStud() {
+        for (Map.Entry<Student, Map<Subject, Integer>> entry : studentSubjects.entrySet()) {
+            System.out.println(entry.getKey().getName() + ": ");
+            for (Map.Entry<Subject, Integer> subjGrade : entry.getValue().entrySet()) {
+                System.out.println("Предмет: " + subjGrade.getKey().getName() + '\n'
+                        + "Оценка: " + subjGrade.getValue());
+            }
+        }
+    }
+
+    public void newSubject(Subject subject, List<Student> students, List<Integer> grades) {
+        subjectStudents.put(subject, students);
+        for (int i = 0; i < students.size(); i++) {
+            studentSubjects.computeIfAbsent(students.get(i), a -> new HashMap<>()).put(subject, grades.get(i));
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: А почему не все грэйды сохраняешь, а только grades.get(i)?
created_at: 2025-04-16T10:01:08Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(s -> s.equals(student));
+        }
+    }
+
+    public void printAllStud() {
+        for (Map.Entry<Student, Map<Subject, Integer>> entry : studentSubjects.entrySet()) {
+            System.out.println(entry.getKey().getName() + ": ");
+            for (Map.Entry<Subject, Integer> subjGrade : entry.getValue().entrySet()) {
+                System.out.println("Предмет: " + subjGrade.getKey().getName() + '\n'
+                        + "Оценка: " + subjGrade.getValue());
+            }
+        }
+    }
+
+    public void newSubject(Subject subject, List<Student> students, List<Integer> grades) {
+        subjectStudents.put(subject, students);
+        for (int i = 0; i < students.size(); i++) {
+            studentSubjects.computeIfAbsent(students.get(i), a -> new HashMap<>()).put(subject, grades.get(i));
+        }
+    }
+
+    public void newStudentForSubj(Student student, Subject subject, int grade) {
+        subjectStudents.get(subject).add(student);
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+    }
+
+    public void removeStudentFromSubj(Student student, Subject subject) {
+        subjectStudents.get(subject).remove(student);
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: SnurnicynKonstantin
body: А если `subjectStudents.get(subject)` вернет null? Когда такого объекта нет в коллекции вернется null. Тогда получишь ошибку NullPointerException.
Надо проверку на null сделать. Если subjectStudents.get(subject) == null {return}, то продолжай операцию. Это как пример. Лучше сразу в переменную положить что из гета получишь и везде ее использовать
created_at: 2025-04-16T10:02:43Z
author_association: NONE
diff_hunk: @@ -0,0 +1,11 @@
+package school.faang.bjs2_68925;
+
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class Subject {
path: src/main/java/school/faang/bjs2_68925/Subject.java
user: login: SnurnicynKonstantin
body: Можно на record переделать
created_at: 2025-04-16T10:04:18Z
author_association: NONE
diff_hunk: @@ -0,0 +1,67 @@
+package school.faang.bjs2_68925;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class StudentDataBase {
+    Map<Student, Map<Subject, Integer>> studentSubjects = new HashMap<>();
+    Map<Subject, List<Student>> subjectStudents = new HashMap<>();
+
+    public void newStudent(Student student, Map<Subject, Integer> subjectGrade) {
+        studentSubjects.put(student, subjectGrade);
+        for (Subject subject : subjectGrade.keySet()) {
+            subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+        }
+    }
+
+    public void newSubjectForStud(Student student, Subject subject, int grade) {
+        studentSubjects.computeIfAbsent(student, a -> new HashMap<>()).put(subject, grade);
+        subjectStudents.computeIfAbsent(subject, a -> new ArrayList<>()).add(student);
+    }
+
+    public void removeStudent(Student student) {
+        studentSubjects.remove(student);
+        for (List<Student> students : subjectStudents.values()) {
+            students.removeIf(s -> s.equals(student));
+        }
+    }
+
+    public void printAllStud() {
+        for (Map.Entry<Student, Map<Subject, Integer>> entry : studentSubjects.entrySet()) {
+            System.out.println(entry.getKey().getName() + ": ");
+            for (Map.Entry<Subject, Integer> subjGrade : entry.getValue().entrySet()) {
+                System.out.println("Предмет: " + subjGrade.getKey().getName() + '\n'
+                        + "Оценка: " + subjGrade.getValue());
+            }
+        }
+    }
+
+    public void newSubject(Subject subject, List<Student> students, List<Integer> grades) {
+        subjectStudents.put(subject, students);
+        for (int i = 0; i < students.size(); i++) {
+            studentSubjects.computeIfAbsent(students.get(i), a -> new HashMap<>()).put(subject, grades.get(i));
path: src/main/java/school/faang/bjs2_68925/StudentDataBase.java
user: login: pirojok-yt
body: тут же только оценку для каждого ученика надо, по одной
created_at: 2025-04-16T12:38:49Z
author_association: NONE

PR comments:
user: login: github-actions[bot]
created_at: 2025-04-13T13:04:17Z
body: ⚠️ **ОШЫБКА**: Сборка завалилась: либо ошибки компиляции, либо не прошли тесты, либо возникли ошибки в стиле кода. Пожалуйста, проверь логи и внеси соответствующие изменения в ПР. После этого красная ошибка ниже должна пропасть, но этот комментарий останется - так и должно быть.
author_association: NONE
user: login: github-actions[bot]
created_at: 2025-04-16T12:49:00Z
body: ⚠️ **ОШЫБКА**: Сборка завалилась: либо ошибки компиляции, либо не прошли тесты, либо возникли ошибки в стиле кода. Пожалуйста, проверь логи и внеси соответствующие изменения в ПР. После этого красная ошибка ниже должна пропасть, но этот комментарий останется - так и должно быть.
author_association: NONE