Skip to content

Instantly share code, notes, and snippets.

@hongsw
Last active September 25, 2024 02:41
Show Gist options
  • Save hongsw/6a4e37bca8c1e8ed98c7d3da2865669e to your computer and use it in GitHub Desktop.
Save hongsw/6a4e37bca8c1e8ed98c7d3da2865669e to your computer and use it in GitHub Desktop.
Class를 활용한 도서관리 프로그램 (문제)
class Book {
String title;
String? author; // 저자 정보를 선택적으로 추가
Book(this.title, [this.author]);
@override
String toString() {
return '$title';
}
}
void main() {
List<Book> books = [];
// 미리 정의된 사용자 입력을 시뮬레이션합니다.
List<String?> userInputs = [
'1', '해리 포터', 'J.K. 롤링', // 책 추가 (저자 포함)
'1', '반지의 제왕', null, // 책 추가 (저자 없음)
'3', // 책 목록 보기
'4', // 종료
];
int inputIndex = 0;
while (true) {
print('\n\n### 기본 도서 관리 시스템\n');
print('1. 책 추가');
print('2. 책 삭제');
print('3. 책 목록 보기');
print('4. 종료');
print('선택하세요: ');
String? choice = userInputs[inputIndex++];
print('사용자 입력: $choice'); // 선택된 입력을 출력
if (choice == '1') {
String? title = userInputs[inputIndex++];
String? author = inputIndex < userInputs.length ? userInputs[inputIndex++] : null;
addBook(books, title, author);
} else if (choice == '2') {
String? title = userInputs[inputIndex++];
removeBook(books, title);
} else if (choice == '3') {
viewBooks(books);
} else if (choice == '4') {
break;
}
}
print('프로그램을 종료합니다.');
}
void addBook(List<Book> books, String? title, [String? author]) {
// TODO
print('$title${author != null ? " by $author" : ""} 책이 추가되었습니다.');
}
void removeBook(List<Book> books, String? title) {
// TODO
print('$title 책이 삭제되었습니다.');
}
void viewBooks(List<Book> books) {
if (books.isEmpty) {
print('책 목록이 비어 있습니다.');
} else {
print('책 목록:');
for (int i = 0; i < books.length; i++) {
print('${i + 1}. ${books[i]}');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment