본문 바로가기
교재 실습/자바 웹 프로그래밍 Next Step

2.4.4.2 숫자 하나를 문자열로 입력할 경우 해당 숫자를 반환한다

by Jint 2025. 1. 7.
HINT)
int number = Integer.parseInt(text);

 

public class StringCalculatorTest {

    ...

    @Test
    public void add_숫자하나() throws Exception {
        assertEquals(0, cal.add("1"));
    }

}

 

public class StringCalculator {

    public int add(String text) {
        if (text == null || text.isEmpty()) {
            return 0;
        }
        return Integer.parseInt(text);
    }

}


두 번째 요구사항에 대한 구현 후에도 복잡도가 낮아 특별히 리팩토링할 부분이 없다.


참고도서 : https://roadbook.co.kr/169

 

[신간안내] 자바 웹 프로그래밍 Next Step

● 저자: 박재성 ● 페이지: 480 ● 판형: 사륙배변형(172*225) ● 도수: 1도 ● 정가: 30,000원 ● 발행일: 2016년 9월 19일 ● ISBN: 978-89-97924-24-0 93000 [강컴] [교보] [반디] [알라딘] [예스24] [인터파크] [샘

roadbook.co.kr

댓글