TheRightGuy

Advice on storing

Feb 12th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package Student;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class App {
  8.     private static final Map<String, Integer> studentScores = new HashMap<>();
  9.     private static final ArrayList<Student> studentArrayList = new ArrayList<>();
  10.  
  11.     public static void main(String[] args) {
  12.         Student student0 = new Student("okay", getStudentScore(), getStudentAverageScore(), getStudentGrade());
  13.         Student student1 = new Student("okay", getStudentScore(), getStudentAverageScore(), getStudentGrade());
  14.         Student student2 = new Student("okay", getStudentScore(), getStudentAverageScore(), getStudentGrade());
  15.         studentArrayList.add(student0);
  16.         studentArrayList.add(student1);
  17.         studentArrayList.add(student2);
  18.         for (Student s : studentArrayList) {
  19.             System.out.printf("Student name %s | student scores %s  | student average score %.3f | student grade %.2f \n", s.getName(), s.getAllScores(), s.getStudentAverageScore(), s.getGrade());
  20.         }
  21.     }
  22.  
  23.     private static Map<String, Integer> getStudentScore() {
  24.         for (int i = 0; i < 5; i++) {
  25.             studentScores.put("Test " + i, (int) (Math.random() * 100) + 1);
  26.         }
  27.         return studentScores;
  28.     } // 42 38 24 95 58 , 51.4, 2.57
  29.     // 18 11 26 1 72 25.6, 1.28
  30.     // 14 28 25 15 71 30.6, 1.53
  31.  
  32.  
  33.     public static double getStudentAverageScore() {
  34.         int sum = 0;
  35.         for (Map.Entry<String, Integer> entry : studentScores.entrySet()) {
  36.             sum += entry.getValue();
  37.         }
  38.         return sum / 5F;
  39.     }
  40.  
  41.     public static double getStudentGrade() {
  42.         int sum = 0;
  43.         for (Map.Entry<String, Integer> entry : studentScores.entrySet()) {
  44.             sum += entry.getValue();
  45.         }
  46.         return sum / 100D;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment