Backend/Spring & JPA
[Spring] 220721 3차 과제 : 학생들의 점수를 관리하고 평균 값을 내는 프로그램
JOKUN
2022. 7. 24. 18:41
학생들의 점수를 관리하고 평균 값을 내는 프로그램✍️
📑문제
문제 은행 [3]에 있는 1번, 4번을 Spring Boot의 Controller와 연결시켜서 풀어보자!
4번의 경우엔 URL 맵핑을 "/homework2"
문제은행[3] - 4번 문제 ::
4. 반 학생이 30명이 있다.
이들은 모두 시험을 치렀고 모든 학생들은 60점 미만이 없다고 한다.
이 상태에서 학생들의 점수를 임의로 배치하고
학급의 평균값을 구해보도록 한다.
위와 같이 만들어서 문제를 풀어보자!
이번에는 클래스를 활용하여 재사용성을 높이는 부분까지 한 번 고려해보자!
[출처] 문제 은행 [ 8 ] (에디로봇아카데미) | 작성자 링크쌤
https://cafe.naver.com/eddicorp/325
문제 은행 [ 8 ]
문제 은행 [ 3 ] 에 있는 1번, 4번을 Spring Boot의 Controller와 연결시켜서 풀어보자! 1번의 경우엔 URL 맵핑을 "/homework1" 4번의 경우...
cafe.naver.com
🔍풀이
ThirdRestController
package com.example.demo.controller.basic.third;
import com.example.demo.entity.basic.third.ClassRoom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/third")
public class ThirdRestController {
@GetMapping("/homework")
public float calcClassRoomMeanScore(){
log.info("학급의 평균을 계산합니다 : calcClassRoomMeanScore()");
ClassRoom classRoom = new ClassRoom();
classRoom.setRoomName("빅데이터 반");
classRoom.makeRandomStudents();
classRoom.takeTest(60);
return classRoom.calcMean();
}
}
ClassRoom
package com.example.demo.entity.basic.third;
import com.example.demo.utillity.basic.third.CustomRandom;
import lombok.*;
import java.util.Random;
@Getter
@Setter
@NoArgsConstructor
public class ClassRoom {
private String roomName;
private Student[] students;
private final int MIN = 10;
private final int MAX = 30;
public void makeRandomStudents(){
int randomStudentsNum = CustomRandom.makeIntCustomRandom(MIN, MAX);
students = new Student[randomStudentsNum];
for (int i = 0; i < randomStudentsNum; i++) {
students[i] = new Student();
}
}
public void takeTest() {
for (int i = 0; i < students.length; i++) {
students[i].takeTest();
}
}
public void takeTest(int min){
for (int i = 0; i < students.length; i++) {
students[i].takeTest(min);
}
}
public float calcMean(){
float mean = 0;
for (int i = 0; i < students.length; i++) {
mean += students[i].score.getScore();
}
return mean / students.length;
}
}
Score
package com.example.demo.entity.basic.third;
import com.example.demo.utillity.basic.third.CustomRandom;
import lombok.Getter;
import lombok.Setter;
import java.util.Random;
@Getter
@Setter
public class Score {
private int score;
private Random random;
private final int MIN = 0;
private final int MAX = 100;
public void makeRandomScore() {
score = CustomRandom.makeIntCustomRandom(MIN, MAX);
}
public void makeRandomScore(int min) {
score = CustomRandom.makeIntCustomRandom(min, MAX);
}
}
Student
package com.example.demo.entity.basic.third;
import com.example.demo.utillity.basic.third.CustomRandom;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Student {
private String name;
private String major;
private int age;
private final String[] names = {
"가가가", "나나나", "다다다", "라라라", "마마마",
"바바바", "사사사", "아아아", "자자자", "차차차"
};
private final String[] majors = {
"물리학", "수학", "화학", "영문학", "중어중문학",
"일어일문학", "컴퓨터공학", "전자공학", "반도체공학", "인공지능"
};
private final int MIN = 20;
private final int MAX = 39;
Score score;
public Student(){
score = new Score();
name = names[CustomRandom.makeIntCustomRandom(0, 10)];
major = majors[CustomRandom.makeIntCustomRandom(0, 10)];
age = CustomRandom.makeIntCustomRandom(MIN, MAX);
}
public void takeTest(){
score.makeRandomScore();
}
public void takeTest(int min){
score.makeRandomScore(min);
}
}
CustomRandom
package com.example.demo.utillity.basic.third;
import java.util.Random;
public class CustomRandom {
public static int makeIntCustomRandom(int min, int max){
Random random = new Random();
return random.nextInt(max - min) + min;
}
}