본문 바로가기
Code/Console

[Java Code] 배열 - 도서 정보 등록·조회·삭제 프로그램 구현

by 스응 2023. 2. 8.
728x90
728x90

코드

#  Book 클래스

public class Book {

	// 멤버변수
	private int totalPage; // 페이지 수
	private String title; // 책 제목
	private String author; // 작가명

	public Book(String title, String author) {
		this.title = title;
		this.author = author;
	}

	public Book(int totalPage, String title, String author) {
		this(title, author);
		this.totalPage = totalPage;
	}

	public int getTotalPage() {
		return this.totalPage; // 가독성을 높이려면 this
	}

	public String getTitle() {
		return this.title;
	}

	public String getAuthor() {
		return this.author;
	}
	
}

#  프로그램 설계

import java.util.Scanner;

public class BookStoreManager {

    public static void main(String[] args) {

        // 1. 저장 2. 전체 조회 3. 선택 조회 4. 전체 삭제 0. 종료

        // 도구 준비
        Scanner scanner = new Scanner(System.in);

        // 책 배열 
        Book[] books = new Book[100];

        // 메뉴 선택 변수
        String choice = null;

        // 임시로 책 제목과 작가명을 입력 받을 변수
        String tempTitle;
        String tempAuthor;

        // 상수 선언
        final String SAVE = "1";
        final String ALL_SHOW = "2";
        final String SELECT_SHOW = "3";
        final String ALL_DELETE = "4";
        final String BREAK = "0";

        // 프로그램 종료 여부를 판단할 변수 (true : 진행, false : 종료)
        boolean flag = true; 

        // 임시로 선택 조회에서 사용할 변수
        boolean temp = false; 

        // 프로그램 시작 //
        while (flag) {

            System.out.println("원하시는 기능이 무엇인가요?");
            System.out.println("1. 저장\t2. 전체조회\t3. 선택조회\t4. 전체삭제\t0. 종료");
            choice = scanner.nextLine();

            // 0. 종료 // 
            if (choice.equals(BREAK)) {
                flag = false;
                System.out.println("프로그램 종료");

            // 1. 저장 // 
            } else if (choice.equals(SAVE)) {
                System.out.println("1번, 책 저장을 선택하셨습니다.");
                System.out.println("책 제목을 입력해주세요.");
                tempTitle = scanner.nextLine(); // 임시 책 제목 저장
                System.out.println("작가명을 입력해주세요.");
                tempAuthor = scanner.nextLine(); // 임시 작가명 저장

                for (int i = 0; i < books.length; i++) {
                    // 기능 : 가장 처음 나오는 null 배열 공간을 찾아서,
                    // 그 공간에 tempTitle, tempAuthor 할당
                    if (books[i] == null) { 
                        books[i] = new Book(tempTitle, tempAuthor);
                        tempTitle = null; // 사용한 값은 null로 초기화
                        tempAuthor = null; // 사용한 값은 null로 초기화
                        break; // for 빠져 나오기
                    }
                }

            // 2. 전체 조회 //
            } else if (choice.equals(ALL_SHOW)) {
                System.out.println("2번, 전체 조회를 선택하셨습니다.");
                for (int i = 0; i < books.length; i++) {
                    if (books[i] != null) {
                        System.out.println(
                            (i + 1) + ". 책 제목 : " + books[i].getTitle() 
                            + ", 저자 : " + books[i].getAuthor());
                    }
                }

            // 3. 선택 조회 // 
            } else if (choice.equals(SELECT_SHOW)) {
                System.out.println("3번, 선택 조회를 선택하셨습니다.");
                System.out.println("조회할 책 제목을 입력해주세요.");
                tempTitle = scanner.nextLine();
                for (int i = 0; i < books.length; i++) {
                    // 방어적 코드
                    if (books[i] != null) {
                        if (books[i].getTitle().equals(tempTitle)) {
                            System.out.println(
                                "책 제목 : " + books[i].getTitle() 
                                + ", 저자 : " + books[i].getAuthor());
                            temp = true; // 검색한 책이 존재함을 의미함
                            break;
                        }
                    }
                } // end of for

                // 기능 : 입력한 제목에 해당하는 책이 존재하지 않는다고 알림 띄우기
                if (temp == false) {
                    System.out.println("책이 존재하지 않습니다.");
                }
                temp = false; // 임시 변수는 다시 false로 초기화

            // 4. 전체 삭제 //
            } else if (choice.equals(ALL_DELETE)) {
                System.out.println("4번, 전체 삭제를 선택하셨습니다.");
                for (int i = 0; i < books.length; i++) {
                    books[i] = null; // 참조 자료형의 기본값은 null
                } // end of for
                System.out.println("삭제가 완료되었습니다.");

            // 번호를 잘못 입력한 경우 //
            } else {
                System.out.println("번호를 잘못 입력하셨습니다.");
            }
            System.out.println("=================================="); // 구분선
        } // end of while

    } // end of main
} // end of class

 

실행

320x100
반응형

댓글