Java

배열 의 활용(도서관리프로그램)

H_u 2024. 4. 22. 17:32
728x90
반응형
SMALL

package basic.ch15;

 

public class BookTest {

public static void main(String[] args) {

 

//배열이란 - 연관된 데이터를 하나의 변수의 통으로 관리하고 싶다면 - 자료구조

//배열을 사용할 때 반드시 크기가 지정되어야 한다.

Book[] books = new Book[10]; //배열에 메모리 공간 선언

 

books[0] = new Book("플러터UI실전", "김근호");

books[1] = new Book("무궁화꽃이피었습니다", "김진명");

books[2] = new Book("흐르는강물처럼", "파울로코엘료");

books[3] = new Book("리딩으로리드하라", "이지성");

books[4] = new Book("사피엔스", "유발하라리");

books[9] = new Book("홍길동전", "허균");

 

//배열은 반복문과 함께 많이 활용 된다.

//배열의 크기와 요소의 크기는 꼭 동일한것은 아니다.

 

//System.out.println(books[3].getTitle());

//System.out.println(books[3].getAuthor());

 

//System.out.println(books[5].getTitle()); <--null pointer 에러

 

//book[0].

//book[1].

//book[2].

//book[3].

//book[4].

//book[5]. < - 에러

for(int i = 0; i < books.length; i++) {

//방어적 코드를 작성해주어야 한다

if(books[i] != null) {

 

System.out.println(books[i].getTitle());

System.out.println(books[i].getAuthor());

System.out.println("-----------------------");

}

}

 

 

}//end of main

}//end of class

 

 

실행에 흐름 만들어 보기

package basic.ch15;

 

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(String title, String author, int totalPage) {

this(title, author);

this.totalPage = totalPage;

}

 

//getter

public int gettotalPage() {

return totalPage;

}

 

public String getTitle() {

return title;

}

 

public String getAuthor() {

return author;

}

 

public void showInfo() {

System.out.println(">>>책 정보<<<");

System.out.println("제목 : " + title);

System.out.println("저자 : " + author);

}

 

}//end of class

 

package basic.ch15;

 

import java.util.Scanner;

 

/**

* author - 김근호 모든 프로그래밍의 기본은 C R U D 이다.

*/

public class MyBookStore {

static int LAST_INDEX_NUMBER = 0;

 

public static void main(String[] args) {

 

// 준비물

Scanner sc = new Scanner(System.in);

// 배열 선언

Book[] books = new Book[100];

 

// 샘플 데이터 만들어 놓기

books[0] = new Book("플러터UI실전", "김근호");

books[1] = new Book("무궁화꽃이피었습니다", "김진명");

books[2] = new Book("흐르는강물처럼", "파울로코엘료");

books[3] = new Book("리딩으로리드하라", "이지성");

books[4] = new Book("사피엔스", "유발하라리");

LAST_INDEX_NUMBER = 5;

 

final String SAVE = "1";

final String SEARCH_ALL = "2";

final String SEARCH_BY_TITLE = "3";

final String DELETE_ALL = "4";

final String END = "0";

boolean flag = true;

 

while (flag) {

System.out.println("**메뉴 선택**");

System.out.println("1.저장 2.전체조회 3.선택조회 4.전체삭제 0.프로그램종료");

 

// 문자열 + 다음줄로 이동 처리

String selectedNumber = sc.nextLine();

if (selectedNumber.equals(SAVE)) {

System.out.println(">>저장하기<<");

save(sc, books);

} else if (selectedNumber.equals(SEARCH_ALL)) {

System.out.println(">>전체 조회 하기<<");

readAll(books);

} else if (selectedNumber.equals(SEARCH_BY_TITLE)) {

System.out.println(">>선택 조회 하기<<");

readByTitle(sc, books);

} else if (selectedNumber.equals(DELETE_ALL)) {

System.out.println(">>전체 삭제 하기<<");

deleteAll(books);

} else if (selectedNumber.equals(END)) {

System.out.println(">>프로그램 종료<<");

flag = false;

} else {

System.out.println(">>잘못된 선택 입니다<<");

}

} // end of while

 

}// end of main

 

// 함수를 활용해보자

public static void readAll(Book[] books) {

System.out.println("----------전체 조회 하기-----------");

 

for (int i = 0; i < books.length; i++) {

// 방어적 코드 작성

if (books[i] != null) {

System.out.println(books[i].getTitle() + "," + books[i].getAuthor());

}

}

}

 

// 전체 삭제하기

public static void deleteAll(Book[] books) {

System.out.println("-----------전체 삭제 하기------------");

for (int a = 0; a < books.length; a++) {

books[a] = null;

System.out.println(books[a]);

}

}

 

// 저장하기 (하나의 북 객체를 저장)

public static void save(Scanner sc, Book[] books) {

System.out.println("------------저장 하기-------------");

System.out.println("책의 제목을 입력 하세요");

String bookTitle = sc.nextLine();

System.out.println("책의 저자를 입력 하세요");

String bookAuthor = sc.nextLine();

Book book = new Book(bookTitle, bookAuthor);

 

if (LAST_INDEX_NUMBER >= books.length) {

System.out.println("더이상 책을 저장할 공간이 없습니다.");

return;

}

// [0] <--

// [1] <--

// [2] <--

// [3] <-- null = new Book(bookTitle, bookAuthor) 이라면 저장할수있다

for (int i = 0; i < books.length; i++) {

if (books[i] == null) {

books[i] = book;

break;

}

}

System.out.println("책이 저장 되었습니다");

}

 

public static void readByTitle(Scanner sc, Book[] books) {

System.out.println("-----------선택 조회 하기------------");

System.out.println(">>>책의 제목을 입력 하세요<<<");

String bookTitle = sc.nextLine();

 

boolean isFind = false;

 

//만약 사용자가 입력한 책 제목과 배열 요소안에 title 값이 같다면

//화면에 책 제목, 책저자 이름을 출력 하고

//아니라면

//해당하는 책이 없습니다. 라는 문구를 출력하시오

 

//심화 -- 반복문을 전부 돌리면 안됩니다.

//사용자가 입력한 책 제목

//books 전부 조사 books[0] -->객체 --> 객체.getTitle()

//문자열 --> equals

//books[0].getTitle().equals()

//배열안에 객체가 없다면 --> null pointer --> 방어적 코드

for(int i = 0; i < books.length; i++) {

//방어적 코드 작성

// t and t

if(books[i] != null) {

if(books[i].getTitle().equals(bookTitle)) {

System.out.println(books[i].getTitle() + ", " + books[i].getAuthor());

isFind = true;

break;

}

}

}

//만약 선택된 값이 없다면

//해당 제목의 책은 존재하지 않습니다.

if(isFind == false) {

System.out.println("해당 제목의 책은 존재하지 않습니다");

}

 

}

 

}// end of class

728x90
반응형
SMALL