1)calculate() 역할
-BF: calculate() 안에서 저장까지 함
arryList.add(result);
-AF: calculate()는 계산만 하고 저장 안 함
return result;
2)Setter
-BF: 리스트 통째로 교체 + return까지 함(캡슐화 깨짐)
public List setArryList(List arryList) {
return this.arryList = arryList;
}
-AF: 결과 1개만 추가, void(정상)
public void setArryList(int result) {
arryList.add(result);
}
3)main()에서 리스트 생성
-BF: main에서 쓸데없이 새 리스트 만듦
List arryList = new ArrayList<>();
-AF: main에서 리스트 생성 제거
(삭제)
4)main()에서 저장 호출 방식
-BF: setArryList(arryList)로 리스트를 꽂아넣음(이상함)
System.out.println(calculator.setArryList(arryList));
-AF: 계산 결과를 저장 메서드로 1번만 추가
calculator.setArryList(result);
5)출력 방식
-BF: setter를 출력용으로 사용함(목적 틀림)
System.out.println("Set: " + calculator.setArryList(arryList));
-AF: 저장은 set, 확인은 get으로 분리
System.out.println("Get: " + calculator.getArryList());
결론
-BF: 계산+저장 섞임 + 외부 리스트로 내부 리스트 교체(캡슐화 깨짐)
-AF: calculate=계산만, set=저장만, get=조회만(캡슐화 유지)
깃허브 프로필 만들다가 바탕화면이 너무 어지러워서 계산기 파일을 다른 곳으로 옮겼다.
C:\Users\kj822\OneDrive\바탕 화면
->
C:\Users\kj822\OneDrive\바탕 화면\혼자 공부-인제\CalculatorProject
이랬더니 터미널이 오류가 생김
1. pwd를 이용해서 파일 위치를 확인
2. is –a를 통해 파일이 다 있는지 확인
./ ../ .git/ .gitignore .idea/ CalculatorProject.iml out/ src/
3. status 확인
new file: src/step3/ArithmeticCalculator.java(초록색)
다행히 고친것 같다.
private enum OperatorType {
Plus, Minus, Multiply, Divide;
}
OperatorType op = new OperatorType(); // 불가능
OperatorType op = OperatorType.Plus; // 가능
char operator = sc.next().charAt(0);
private enum OperatorType {
Plus, Minus, Multiply, Divide;
static OperatorType fromChar(char c) {
switch (c) {
case '+': return Plus;
case '-': return Minus;
case '*': return Multiply;
case '/': return Divide;
default: throw new IllegalArgumentException("지원하지 않는 연산자: " + c);
}
}
}
연산이 그냥 넘어가던 이유: main 조건문이 /를 막아서 calculate가 호출되지 않았음.
-> try문에 calculate 호출을 안넣었음;;
예외를 제대로 잡으려면 calculate 호출을 try 밖에서 하지 말고 try 안에서 1번만 해야한다.
오류가 나도 계속 실행되게 하려면 catch에서 메시지 출력 후 continue로 while 처음으로 되돌리기.
catch 상황에서 값이 저장되던 문제는 실패를 0으로 반환하거나 성공/실패와 상관없이 저장해서 생겼고, 성공했을 때만 저장.
//main함수
ArithmeticCalculator<> calculator = new ArithmeticCalculator<>();
List<Double> ret1 = calculator.getArryList().stream()
.map(num -> num * 10)
.collect(Collectors.toList());
System.out.println("람다ret: "+ret1);

이렇게 작성 해서 해결
ArithmeticCalculator<Double> calculator = new ArithmeticCalculator<>();
변경 후 오류 해결-정상 출력

double num1 = scannerRead("첫 번째 숫자를 입력하세요: ",sc);
double num2 = scannerRead("두 번째 숫자를 입력하세요: ",sc);
private static double scannerRead(String str1,Scanner scanner ){
while(true){
System.out.print(str1);
try{
double num = scanner.nextDouble();
return num;
}catch (RuntimeException e){
System.out.println("숫자를 입력하세요!!");
scanner.next();
}
}
}