1. 함수의 3요소
input, output, 기능
2. 함수화를 하는 이유
1) 중복이 없는 코드를 작성하고,
2) 코드를 재사용하기 용이하며,
3) 개발 시간이 단축되고,
4) 개발 비용이 절감되며,
5) 오류의 파급효과가 줄어든다.
3. MVC - VIEW가 하는 역할
VIEW가 사용자가 입력하는 정보를 안전하게 걸러서 컨트롤러에게 전해줘야 함 => 유효성 검사
메뉴 입력, 학생전체출력, 학생번호 입력, 학생 정보 출력, 추가할 학생 이름 입력, 학생 추가 완료 안내 등
->> (뷰가 해야 하는 일(뷰의 기능))
4. test() 라는 같은 이름을 가지는 함수를 또 선언할 수 있나? YES
- 오버로딩 : 매개변수와 인자 타입을 다르게 해서 같은 이름의 메소드를 여러 개 선언할 수 있다.
(= 메서드 시그니쳐가 달라야 한다.)
5. 과제하면서 느낀 점 : 코드 작성 후 코드리뷰 하면서 다같이 코드 리팩토링 했는데 다같이 검토하니 더 효율적인 코드로 바꿀 수 있었고, 스터디하면서 모델과 뷰에 대한 이해도가 높아졌다.
[학생부 프로그램 - MVC 패턴에 맞춰 코드 작성]
package Team;
import java.util.Random;
import java.util.Scanner;
public class Test01 {
public static int stuList[] = {0,0,0};
//Model
public static int[] selectAll() {
return stuList;
}
public static int selectOne(int num) {
return stuList[num];
}
public static int randNum(int num) { //중복 없이 난수 생성
Random rand=new Random();
int score;
while(true) {
score=rand.nextInt(101); //0~100
if(score != selectOne(num-1)){
break;
}
}
return score;
}
public static int findMax() { //데이터를 가져와 비교하기 때문에 Model
int max=selectOne(0);
int maxIndex=0;
for(int i=1;i<selectAll().length;i++) {
if(max<selectOne(i)) {
max=selectOne(i);
maxIndex=i;
}
}
return maxIndex;
}
//View
public static int inputAct() {//메뉴 선택 입력받기
Scanner sc=new Scanner(System.in);
System.out.println("=====학생부 프로그램=====");
System.out.println("1. 전체 출력");
System.out.println("2. 1등 출력");
System.out.println("3. 정보 추가");
System.out.println("4. 정보 변경");
System.out.println("0. 프로그램 종료");
System.out.println("=======================");
System.out.print(">> ");
int action=sc.nextInt();
return action;
}
public static boolean emptyList(int cnt) { //배열이 비어있는지 확인
if(cnt<=0) { //UI/UX
System.out.println("출력할 데이터가 없습니다!");
return false;
}
return true;
}
public static void printStuList(int cnt, int[] stuList) {
for(int i=0;i<cnt;i++) { //stuList.length는 학생부 자체의 크기
System.out.println((i+1)+"번 학생의 점수는 : "+stuList[i]+"점");
}
}
public static void printFirst(int maxIndex, int max) {
System.out.println("1등은 "+(maxIndex+1)+"번 학생,"+max+"점 입니다");
}
public static boolean overStu(int cnt, int LEN) {
if(cnt>=LEN) { //최대 학생수만큼 저장된 상황이라면
System.out.println("학생부에 저장공간이 부족합니다");
return false;
}
return true;
}
public static int inputScore() { //점수 입력
Scanner sc=new Scanner(System.in);
int score;
while(true) {
System.out.print("추가할 학생의 점수 입력>>");
score=sc.nextInt();
if(0<=score && score<=100) {
break;
}
System.out.println("0에서 100점 사이만 입력 가능합니다");
}
return score;
}
public static void printSuccess() { //성공메시지
System.out.println("작업 완료!");
}
public static int inputNum(int cnt) { //정보 변경 할 학생 번호 입력
Scanner sc=new Scanner(System.in);
int num;
while(true) {
System.out.print("정보 변경 할 학생의 번호 입력>>");
num=sc.nextInt();
if(1<=num && num<=cnt) { //종료조건
break;
}
System.out.println("해당 번호 학생은 존재하지 않습니다");
}
return num;
}
public static void printErr() { //에러메시지
System.out.println("잘못된 입력입니다. 다시 입력해주세요");
}
public static void printStu(int num, int index) { // 1등 점수랑 1등 자리
System.out.println("1등은 "+(index+1)+"번, "+num+"점 입니다");
}
public static void main(String[] args) {
int cnt=0; // 현재 학생부에 저장된 학생 수
while(true) {
int action= inputAct();
if(action==0) { //종료조건
break;
}
else if(action==1) { //전체 출력
//현재 저장된 학생들의 점수정보를 출력
if(emptyList(cnt)) {
printStuList(cnt, selectAll());
}
}
else if(action==2) { //1등 출력
emptyList(cnt);
//최댓값 찾기 알고리즘
int maxIndex=findMax();
if(cnt>0) { //비어있지 않을 때
printStu(selectOne(maxIndex), maxIndex);
}
}
else if(action==3) { //점수 추가
if(overStu(cnt,selectAll().length)) {
//사용자로부터 학생의 점수를 입력
//입력받은 점수 정보를 배열에 저장
selectAll()[cnt]=inputScore();
cnt++;
//저장완료! 안내
printSuccess();
}
}
else if(action==4) { //정보 변경
if(!emptyList(cnt)) {
continue;
}
//정보 변경 할 학생의 번호를 입력
//유효성 검사
int num=inputNum(cnt);
//어떤 점수로 변경할지 결정//랜덤으로 변경
selectAll()[num-1]=randNum(num); //U
//정보 변경 완료 안내
printSuccess();
}
else { //유효성 검사
printErr();
}}
}
}
[피드백]
1. 과제 제출할 때 파일명 Test01 이런거 말고 과제임을 명확히 알 수 있는 파일명으로 변경할 것
2. 웹 개발 할 때는 샘플 데이터를 하나하나 선언하기 때문에, public static int[] stuList = {0,0,0}; 보다는 stuList[0] = 1; 처럼 하나하나 초기화 해주기.
3. 변수명 등 단어를 짧게 줄이지말고 알아보기 쉽게 명확히 쓰기
4. 1등 출력 시 100점이 2명이라면 selectAll()을 써야할까 selectOne()을 써야할까? 고민해보기.
(100점이 2명-3명 나올 수도 있으므로 selectAll()을 사용
5. if문 안에 들어가는 함수 ->>> boolean 함수명 is~~~, has~~~~
6. v, m 코드는 절대 겹칠 수 x. 서로 호출할 수 없다.
7. selectOne()을 할 시 무조건 데이터가 있다고 생각하는데 예상치 못한 에러값 입력, 서버 외부 공격 주의
조건문을 통해 이상한 값이 들어올 경우 막아줄 필요 O (모든 곳에서 유효성 검사를 다 해줄 필요 o)
-> 로그인, 비밀번호 등.. 보안 빡세게!!
8. 컨트롤러는 메인에..
9. 출력/입력 기능 나누면 더 좋을듯?!
함수명 지을 때 고민해보기... view는 input ~ print~ 이런 식으로, 로직코드는 딱 봐도 로직코드같아보이는 이름으로 (print, input 이런거 x)
'IT > 코딩 스터디' 카테고리의 다른 글
24.12.28(토) 스터디 기록 (1) | 2025.01.05 |
---|