일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Tags
- Java
- 체인호출
- 스프링부트 쇼핑몰 프로젝트
- querydsl
- 부트캠프
- 반복문 탈출
- orm
- 알고리즘
- GitFlow
- VB
- Hibernate
- RESPONSEBODY
- JPA
- 연관관계
- 멋쟁이사자처럼
- Git
- break-label
- 테킷
- responsepart
- spring jpa
- 프로그래머스
- PDF병합
- Visual Basic
- PDFBOX
- 코딩테스트
- 값 타입
- springboot
- restapi
- 커밋 컨벤션
- 제작기
Archives
- Today
- Total
섭섭한 개발일지
[명언게시판] level 5 본문
구현 요구사항
== 명언 앱 ==
명령) 등록
명언 : 현재를 사랑하라.
작가 : 작자미상
1번 명언이 등록되었습니다.
명령) 등록
명언 : 현재를 사랑하라.
작가 : 작자미상
2번 명언이 등록되었습니다.
명령) 목록
번호 / 작가 / 명언
----------------------
2 / 작자미상 / 과거에 집착하지 마라.
1 / 작자미상 / 현재를 사랑하라.
명령) 종료
: 단순 리스트 출력이다. 사용자를 통해 "목록" 이라는 명령만 받으면 추가적으로 받을 명령이 없기에
output에서 출력을 해주면 된다.
구현
// main
public class Application {
public static void main(String[] args) {
SayingRepository repo = SayingRepository.getInstance();
Input input = new Input();
Output output = new Output();
while (true) {
System.out.print("명령) ");
String request = InputView.read();
if (request.equals("등록")) {
Saying saying = input.addSaying(new Saying());
repo.save(saying);
output.printAddSaying(saying);
}
if (request.equals("목록")) {
output.printSayings(repo.findAll());
}
if (request.equals("종료")) {
break;
}
}
}
}
// repository
public class SayingRepository {
private static final Map<Long, Saying> sayings = new HashMap<>();
private static Long id = 0L;
private static final SayingRepository instance = new SayingRepository();
private SayingRepository() {
testDataInit();
}
public static SayingRepository getInstance() {
return instance;
}
public Saying save(Saying saying) {
saying.setId(++id);
sayings.put(saying.getId(), saying);
return saying;
}
public List<Saying> findAll() {
return new ArrayList<>(sayings.values());
}
// 기능 테스트를 위한 명언 자동 생성
private void testDataInit() {
for (int i = 1; i <= 10; i++) {
Saying saying = new Saying();
saying.setContent("test content " + i);
saying.setAuthor("test author " + i);
save(saying);
}
}
}
// output
public class Output {
public void printAddSaying(Saying saying) {
System.out.println(saying.getId() + "번 명언이 저장되었습니다.");
}
public void printSayings(List<Saying> sayings) {
System.out.println("번호\t\t작가\t\t명언");
System.out.println("---------------------------");
for (Saying saying : sayings) {
System.out.printf("%d \t\t %s \t\t %s\n", saying.getId(), saying.getAuthor(),
saying.getContent());
}
}
}
'멋쟁이사자처럼 > Project' 카테고리의 다른 글
[명언게시판] level 6 (0) | 2023.10.27 |
---|---|
[명언게시판] level 4 (0) | 2023.10.27 |
[명언게시판] level 3 (0) | 2023.10.27 |
[명언게시판] level 2 (0) | 2023.10.27 |
[명언게시판] level 1 (0) | 2023.10.27 |
Comments