[Java] ์ž๋ฐ”์˜ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-Oriented Programming) - ์˜ˆ์‹œ ์ฝ”๋“œ ํ’€์ด

2021. 3. 1. 01:31ใ†๊ฐœ๋… ์ •๋ฆฌ ์ž‘์—…์‹ค/Java

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ์ฒด์ง€ํ–ฅ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค์€ [Java] ์ž๋ฐ”์˜ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-Oriented Programming)์— ๊ด€์‹ฌ์„ ์ฃผ์„ธ์š”.

 

[Java] ์ž๋ฐ”์˜ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-Oriented Programming)

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์ฃผ๋‹ˆํ•˜๋ž‘์€ ๊ตญ๋น„์ง€์›์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๊ณ  ์žˆ๊ณ , ๊ทธ ๊ณต๋ถ€ ๋‚ด์šฉ์„ ๋‹ค์‹œ ๋ณต์Šตํ• ๊ฒธ ๋˜ํ•œ, ์ž๋ฐ” ๊ณต๋ถ€๋ฅผ ํ•˜์‹œ๋Š” ๋ถ„๋“ค๊ป˜ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ

junyharang.tistory.com

 

 

๐Ÿ“Œ ๋„์„œ ๊ฒ€์ƒ‰ ํ”„๋กœ๊ทธ๋žจ (๊ฐ์ฒด ์‚ฌ์šฉ)

   ๐Ÿ“ ConsoleApp.java (Main)


 

 

package oopconcept_210225;

import java.util.Scanner;
import oopconcept_210225.bookstore.*;
/**
 * ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ: ์ฑ…(Book)
 */

public class ConsoleApp {

	public static void main(String[] args) {
		// ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๊ฒ€์ƒ‰๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์„œ ์ถœ๋ ฅํ•œ๋‹ค....
		Scanner kb = new Scanner(System.in);
		SearchService service = new SearchService();
		
		while(true) {
			System.out.println("[1]๋„์„œ๋ช… [2]์ถœํŒ์‚ฌ๋ช… [0]์ข…๋ฃŒ> ");
			String menu = kb.nextLine();// "1" or "0"
			// ์ž…๋ ฅ์— ๋”ฐ๋ผ์„œ ์‹คํ–‰๋œ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌ,์ถœ๋ ฅํ•œ๋‹ค..
			if( menu.equals("0")) {	// ๋ฌธ์ž์—ด์ด ๊ฐ™์œผ๋ฉด true
				break;
			}
			String key;
			switch( menu ) {
			case "2":
				System.out.println("๊ฒ€์ƒ‰ํ•  ์ถœํŒ์‚ฌ๋ช… > ");
				key = kb.nextLine();
				Publisher[] pubs = service.searchPubsName(key);
				if( pubs != null) {
					for(int i=0; i<pubs.length;++i) {
						// null ์ธ ์š”์†Œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ
						if( pubs[i] != null) {
							System.out.println(i+":"+pubs[i]);
							// Book ์ค‘์—์„œ ์ถœํŒ์‚ฌ๋ช…์ด pubs[i].getName() ์ธ ๊ฒƒ ์ฐพ๊ธฐ
							String pubname = pubs[i].getName();
							Book[] list = service.searchBookByPubName(pubname);
							if(list != null) {
								for(int b=0; b<list.length;++b) {
									if( list[b] != null) {
										System.out.println(b+":"+list[i]);
									}
								}
							}
						}
					}
				}else {
					System.out.println("๋“ฑ๋ก๋œ ๊ฑฐ ์—†๋‹ค");
				}
				break;
			case "1":
				System.out.println("๊ฒ€์ƒ‰ํ•  ๋„์„œ๋ช… > ");
				key = kb.nextLine();
				// ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด key ๋ฅผ ์ „๋‹ฌํ•ด์„œ ๊ฒ€์ƒ‰๋œ ๋„์„œ ๋ชฉ๋ก ๋ฐ›๋Š”๋‹ค
				Book[] list = service.searchBookName(key);
				// ์ถœ๋ ฅํ•˜๊ธฐ
				if( list == null) {
					System.out.println("๋“ฑ๋ก๋œ ๋„์„œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
				}else {
					for(int i=0; i< list.length; ++i) {
						Book book = list[i];
						if( book != null) {
							System.out.println(i+":"+book);// ์ฐธ์กฐ๊ฐ’.toString()
						}
					}
				}
				break;
			}
		}

		kb.close();// ํ‚ค๋ณด๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์„ ๋Š๋Š”๋‹ค... ์ด์ œ ์•ˆ ์“ธ๊ฑฐ๋‹ˆ๊นŒ
	} // Main End

} // Class End

 

 

   ๐Ÿ“ SearchSercvice.java


package oopconcept_210225.bookstore;

/**
 * ๋„์„œ๋ช…์„ ๊ฒ€์ƒ‰ํ•˜๋Š” Method ํ•„์š”
 */

public class SearchService {
	
	private Book[] bookStorage;
	private Publisher[] pubStorage;

	public SearchService() {
		// ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ํ™•์ธ ์œ„ํ•ด ๋ฏธ๋ฆฌ ๋„์„œ ์ •๋ณด๋ฅผ ์ €์žฅํ•ด ๋†“๊ณ  ์‹ถ๋‹ค.
		// ์ด ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ๋•Œ ์ €์žฅํ•˜๊ฒ ๋‹ค....
		String[] pubnames = {"ํ”„๋ฆฌ๋ ‰","ํ•œ๋น›","์—์ด์ฝ˜","์„ฑ์•ˆ๋‹น","๋ฏธ๋””์–ด์•„ํŠธ"};
		// ๋„์„œ ์ •๋ณด ๋ฏธ๋ฆฌ ๋„ฃ์–ด๋†“๊ธฐ
		this.bookStorage = new Book[10];
		for(int i=0; i<5; ++i) {
			Book book = new Book();
			book.setTitle("bitcamp"+i);
			book.setPubName(pubnames[i]);
			bookStorage[i] = book;
		}
		// ์ถœํŒ์‚ฌ ์ •๋ณด ๋ฏธ๋ฆฌ ๋„ฃ์–ด๋†“๊ธฐ
		this.pubStorage = new Publisher[5];
		for( int i=0; i< pubStorage.length; ++i) {
			Publisher p = new Publisher();
			p.setName(pubnames[i]);
			pubStorage[i] = p;
		}
		
	}
	
	public Book[] searchBookName(String bookTitle) {
		// ๋ฐฐ์—ด ์‚ฌ์šฉ ์‹œ -. ๊ณ ์ • ๊ธธ์ด : ์‹คํ–‰ ์ค‘ ๊ธธ์ด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค
		Book[] temp = new Book[bookStorage.length];
		int count = 0;// ๊ฒ€์ƒ‰๋œ ๊ฐœ์ˆ˜
		// ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์€ contains -> ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰(ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜)
		for(int i=0; i<bookStorage.length;++i) {
			if( bookStorage[i] != null ) {
				Book book = bookStorage[i];
				if( book.getTitle().contains(bookTitle)) {
					temp[count] = book;
					++count;
				}
			}
		}
		
		if( count > 0 ) {
			Book[] list = new Book[count];// ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์ž„์„ ์žŠ์ง€ ๋ง์ž NullPointerException
			// temp ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์š”์†Œ๋“ค์„ list ๋ฐฐ์—ด๋กœ ๋ณต์‚ฌํ•˜๊ฒ ๋‹ค
			for(int i=0; i< count;++i) {
				list[i] = temp[i];
			}
	//		System.arraycopy(temp, 0, list, 0, count);
			return list;
		}
		
		return null;// ๊ฒ€์ƒ‰๋œ๊ฒƒ์ด ์—†๋‹ค...
	}
	
	public Publisher[] searchPubsName(String pubsTitle) {
		// ๋ฐฐ์—ด ์‚ฌ์šฉ ์‹œ -. ๊ณ ์ • ๊ธธ์ด : ์‹คํ–‰ ์ค‘ ๊ธธ์ด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค
		Publisher[] temp = new Publisher[pubStorage.length];
		int count = 0;// ๊ฒ€์ƒ‰๋œ ๊ฐœ์ˆ˜
		for(int i=0; i<pubStorage.length;++i) {
			if( pubStorage[i] != null ) {
				Publisher pub = pubStorage[i];
				if( pub.getName().contains(pubsTitle)) {
					temp[count] = pub;
					++count;
				}
			}
		}
		
		if( count > 0 ) {
			return temp;
		}
		return null;// ๊ฒ€์ƒ‰๋œ๊ฒƒ์ด ์—†๋‹ค...
	}

	public Book[] searchBookByPubName(String pubname) {
		// ์ถœํŒ์‚ฌ๋ช…์„ ์ „๋‹ฌ๋ฐ›์•„์„œ Book[] ํ•„๋“œ๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค.
		Book[] temp = new Book[ bookStorage.length ];
		int count = 0;
		// ๋ฐ˜๋ณต๋ฌธ์„ for-each ๋ฅผ ์‚ฌ์šฉํ•ด๋ณธ๋‹ค...
		for(Book book : bookStorage) {
			if( book != null ) {
				if( book.getPubName().equals(pubname)) {
					temp[count] = book;
					++count;
				}
			}
		}
		return (count > 0)? temp : null;
	}


} 

 

 

   ๐Ÿ“ Book.java


package oopconcept_210225.bookstore;

public class Book {
	
	private String title;//๋„์„œ๋ช…
	private String pubName;//์ถœํŒ์‚ฌ๋ช… <- ์ด ์ฑ…์„ ์ถœํŒํ•œ ํšŒ์‚ฌ๋ช…
	
	public String getPubName() {
		return this.pubName;
	}
	public void setPubName(String name) {
		this.pubName = name;
	}

	public String getTitle() {
		return this.title;// title
	}
	public Book setTitle(String title) {
		this.title = title;
		return this;
	}
	
	@Override
	public String toString() {
	return "Book [title=" + title + ", pubName=" + pubName + "]";
	}	
	// ๊ฐ์ฒด๋ช…์œผ๋กœ ์ถœ๋ ฅํ•  ๋•Œ toString() ํ˜ธ์ถœ๋œ๋‹ค...
	

	}

 

 

   ๐Ÿ“ Publisher.java


package oopconcept_210225.bookstore;

public class Publisher {
	
	private String name;//์ถœํŒ์‚ฌ๋ช…

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Publisher [name=" + name + "]";
	}

	
	

}

 

 

 

 

 

 

๐Ÿ“Œ ์ฝ”๋“œ ๋ถ„์„


Scnner ๊ฐ์ฒด์— Systme.in ์ธ์ž ๊ฐ’์„ ๋„ฃ์–ด ํ˜ธ์ถœํ•˜์—ฌ Scnner ์ž๋ฃŒํ˜• kb๋ณ€์ˆ˜์— ๋„ฃ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ‚ค๋ณด๋“œ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

SearchService Type์˜ service ๋ณ€์ˆ˜์— SerchService(); ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋„ฃ์Šต๋‹ˆ๋‹ค.

 

while๋ฌธ์— true๋ฅผ ๋„ฃ์–ด ํ•ญ์ƒ ๋ฐ˜๋ณต์ด ์‹œ์ž‘๋˜๋„๋ก ์„ค์ •ํ•œ ๋’ค ์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋–ค ์ž‘์—…์„ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๋ฌป์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋’ค ์œ„์—์„œ ๋งŒ๋“  Scnnerํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ kb์•ˆ์— netxtLineํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฌธ์ž์—ด ํƒ€์ž… menu๋ณ€์ˆ˜์— ๋„ฃ๋Š”๋ฐ, ์ด๊ฒƒ์€ ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’ ๋ฌธ์ž์—ด์„ menu์— ๋„ฃ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋’ค if๋ฌธ์„ ํ†ตํ•ด menu๋ณ€์ˆ˜๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” Object ๊ฐ์ฒด ์ค‘ equalsํ•จ์ˆ˜์— ์ธ์ž ๊ฐ’ 0์„ ๋„ฃ์–ด ์ž…๋ ฅ๊ฐ’์ด ๋ฌธ์ž์—ด 0๊ณผ ๊ฐ™์€์ง€๋ฅผ ํ™•์ธํ•˜๊ณ , ๊ทธ๊ฒƒ์ด ์ฐธ์ด๋ผ๋ฉด if๋ฌธ์ด ์‹คํ–‰๋˜์–ด while๋ฌธ์„ ์ข…๋ฃŒํ•˜๊ณ , ํ”„๋กœ๊ทธ๋žจ์€ ์ข…๋ฃŒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

๋จผ์ € ์‚ฌ์šฉ์ž๊ฐ€ 2๋ฒˆ์„ ๋ˆ„๋ฅด๊ฒŒ ๋˜๋ฉด ์ถœํŒ์‚ฌ๋ช…์„ ๊ฒ€์ƒ‰ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๊ฒƒ๋ถ€ํ„ฐ ๋ถ„์„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

while๋ฌธ์—์„œ 0๋ฒˆ์„ ๋ˆ„๋ฅด์ง€ ์•Š์•˜๋‹ค๋ฉด shich๋ฌธ์œผ๋กœ ๋“ค์–ด์˜ค๊ณ , switch๋ฌธ์— menu ์ธ์ž๋ฅผ ๋„ฃ์–ด ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๊ฐ€์ง€๊ณ , case ๋ฌธ๊ณผ ๋น„๊ตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. case "2"๋กœ ์ธํ•ด case๋ฌธ ์•ˆ์— ๋‚ด์šฉ์ด ์‹คํ–‰๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋’ค ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ถœํŒ์‚ฌ๋ช…์ด ๋ฌด์—‡์ธ์ง€ ๋ฌผ์–ด๋ณด๊ณ , ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’์„ key๋ผ๋Š” ๋ฌธ์ž์—ด ๋ณ€์ˆ˜์— ๋„ฃ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. switch๋ฌธ ์œ„์— String key;๋ฅผ ์„ ์–ธํ•œ ๊ฒƒ์€ switch๋ฌธ์—์„œ ์ „์—ญ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

31๋ฒˆ ๋ผ์ธ์€ Publisher ์ฐธ์กฐํ˜• ๋ฐฐ์—ดํ˜• pubs๋ณ€์ˆ˜์— service.searchPubsNameํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ธ์ž ๊ฐ’(๋งค๊ฐœ๋ณ€์ˆ˜)์œผ๋กœ ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ๋„ค์š”.

SerchService Class์˜ serchPubsName ๋ฉ”์„œ๋“œ

์ž! ๊ทธ๋Ÿผ ํ˜ธ์ถœ์„ ํ•˜์˜€์œผ๋‹ˆ ์ด ๊ณณ์œผ๋กœ ์ž‘์—…์ด ๋„˜์–ด์˜ค๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”. ์ด๋•Œ Memory์—์„œ๋Š” main Method์œ„์— serchPubsName Method๊ฐ€ ์˜ฌ๋ผ๊ฐ€๊ณ , ์ด๊ฒƒ์ด Run์ƒํƒœ๊ฐ€ ๋˜๊ณ , main Method๋Š” ๋Œ€๊ธฐ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

66๋ฒˆ ์ค„์„ ๋ณด๋‹ˆ ๋˜ ๋‹ค๋ฅธ ์ฐธ์กฐํ˜• ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  ์žˆ๋„ค์š”. Publisher ๋ฐฐ์—ดํ˜• ์ฐธ์กฐ ๋ณ€์ˆ˜ temp์— publisher์ด๋ผ๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ๊ทธ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” pubStorage๋งŒํผ ์ž…๋‹ˆ๋‹ค.

SerchService Class์˜ pubStorage ๋ฐฐ์—ด ์กฐ์‚ฌ

์ž ๊ทธ๋Ÿผ ์ด ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ์–ผ๋งŒํผ์ธ์ง€ ๋ด์•ผ๊ฒ ์ฃ ? ๋จผ์ € ์ด ๋ฐฐ์—ด์€ ํ•„๋“œ์— private์œผ๋กœ ์„ ์–ธ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ Class ์™ธ์—๋Š” ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๊ฒ ๋„ค์š”! Publisher ๋ฐฐ์—ดํ˜• ์ฐธ์กฐ ๋ณ€์ˆ˜ pubStorage๋ฅผ 12๋ฒˆ์งธ ์ค„์—์„œ ์„ ์–ธ์„ ํ•˜๊ณ , 19๋ฒˆ์งธ ์ค„์—์„œ this.bookStorage (ํ•„๋“œ๋ช…)์— Book ๋ฐฐ์—ด์„ 10์˜ ๊ธธ์ด๋กœ ์ƒ์„ฑ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด pubStorage์˜ ๊ธธ์ด๋Š” 10์ด๊ฒ ๋„ค์š”!

์ด๋ฒˆ์—๋Š” serchPubsName ํ•จ์ˆ˜์˜ 67๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ถ„์„์„ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜ํ˜• count๋ณ€์ˆ˜์— 0์„ ๋„ฃ์–ด ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด ์ฃผ๊ณ  ์žˆ๋„ค์š”.

๊ทธ๋Ÿฐ ๋’ค for๋ฌธ (๋ฐ˜๋ณต๋ฌธ)์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ˜๋ณต๋ฌธ์€ pbuStorage ์ฐธ์กฐํ˜• ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ์ธ๋ฐ, ์•„๊นŒ 10์ด๋ผ๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์ง€์š”? ๊ทธ๋Ÿผ 0๋ฒˆ๋ถ€ํ„ฐ 10 ๋ณด๋‹ค ์ž‘์„ ๋•Œ๊นŒ์ง€์ด๋‹ˆ 10๋ฒˆ ๋Œ๊ฒ ๋„ค์š”!

๊ทธ๋Ÿฐ ๋’ค if๋ฌธ์„ ๋งŒ๋‚ฉ๋‹ˆ๋‹ค. if๋ฌธ์€ pubStorage์— Index ๋ฒˆํ˜ธ์— i๋ฅผ ์ฃผ๋Š”๋ฐ, i๋Š” 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์˜ฌ๋ผ๊ฐˆ ํ…Œ๋‹ˆ ๊ฐ ๋ฐฐ์—ด์ด null์ด ์•„๋‹ˆ๋ผ๋ฉด ์ด๋ผ๊ณ  ๋ฌป๊ณ  ์žˆ๋„ค์š”. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด if๋ฌธ์€ pubStorage๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐ์„ ํ•  ๋•Œ, NPE (Null Point exception)์„ ๊ฒ€์‚ฌํ•˜๋Š” ์š”์†Œ ๊ฒ€์‚ฌ ๋ถ€๋ถ„์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž! pubStorage ๋ฐฐ์—ด์— null์ด ์—†๋‹ค๋ฉด if๋ฌธ ์•ˆ์œผ๋กœ ๋“ค์–ด์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 70๋ฒˆ ์ค„์„ ๋ณด๋ฉด Publisher ์ฐธ์กฐํ˜• ํƒ€์ž… pub ๋ณ€์ˆ˜์— pubStorage i๋ฒˆ Index์˜ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋„ค์š”. ๊ทธ๋Ÿผ ์ด ๊ณณ์— ๋ฌด์–ธ๊ฐ€ ๊ฐ’์„ ๋„ฃ์—ˆ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ๋ฌด์—‡์ด ๋“ค์–ด ์žˆ๋Š”์ง€ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

SerchService Class์˜ SerchService Method

์ด ๋‚ด์šฉ์€ SearchService Method์—์„œ ํ™•์ธ์„ ํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”. ๋ณด๋‹Œ๊นŒ String ํƒ€์ž… ๋ฐฐ์—ด pubnames์— ์ถœํŒ์‚ฌ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ 5๊ฐœ ๋„ฃ์–ด์คฌ๋„ค์š”. ๊ทธ๋Ÿผ pubnames ๋ฐฐ์—ด์€ 5์˜ ๊ธธ์ด๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

19๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹Œ๊นŒ bookStorage ํ•„๋“œ์— Book ํƒ€์ž… ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ, 10์˜ ๊ธธ์ด๋ฅผ ๊ฐ–๋„ค์š”.

๊ทธ๋Ÿฐ ๋’ค ๋ฐ˜๋ณต๋ฌธ์„ ๋“ค์–ด๊ฐ€๊ณ , 5๋ฒˆ์„ ๋ฐ˜๋ณต์„ ํ•˜๋Š”๋ฐ, 21๋ฒˆ์งธ ์ค„์— Book ํƒ€์ž…์— book ๋ณ€์ˆ˜์— Book์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋„ฃ์Šต๋‹ˆ๋‹ค.

 21๋ฒˆ์งธ ์ค„์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ฆฌ๋ฉด ์œ„์™€ ๊ฐ™์ด ๋‚˜์˜ค๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

22๋ฒˆ์งธ ์ค„์„ ๋ถ„์„ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. book๋ณ€์ˆ˜ ์•ˆ์— setTitle ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜) ๊ฐ’์œผ๋กœ "bitcmap"+[i]๋ฅผ ์ฃผ๊ณ  ์žˆ๋„ค์š”.

Book Class์˜ setTitle Method

setter๋Š” ํ•„๋“œ๊ฐ€ private(๊ฐ™์€ Class์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ)์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์„ ๋•Œ, ๊ทธ ํ•„๋“œ์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์—์š”.

๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ "bitcamp"๋ฅผ ์ฃผ์—ˆ๊ณ , i๊ฐ’์„ ์ฃผ์—ˆ์œผ๋‹ˆ bitcamp0, 1, 2... ์ˆœ์œผ๋กœ ๋“ค์–ด์˜ฌ ๊ฒƒ์ด๊ณ , ๊ทธ๊ฒƒ์ด title์— ๋“ค์–ด์˜จ ๋’ค this.title ์ฆ‰, ํ•„๋“œ์— ์ €์žฅ์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋’ค ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ this๋ฅผ ์ฃผ๋‹ˆ ํ•„๋“œ์— ์ €์žฅ๋œ ๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค๋Š” ๊ฒƒ์ด ๋˜๊ฒ ๋„ค์š”. ๋ฐ˜ํ™˜ ๊ฐ’์— ํƒ€์ž…์€ Book์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋’ค 23๋ฒˆ์งธ ์ค„์—์„œ ๋‹ค์‹œ book ์ฐธ์กฐ ๋ณ€์ˆ˜ ์•ˆ์— setPubName Method๊ฐ€ ์žˆ๋Š”๋ฐ, ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜) ๊ฐ’์œผ๋กœ pubnames [i]๋ฅผ ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

17๋ฒˆ์งธ ์ค„์—์„œ pubnames๋ผ๋Š” ๋ฐฐ์—ดํ˜• ์ฐธ์กฐ ๋ณ€์ˆ˜์— ์ถœํŒ์‚ฌ๋ช…์„ ๋„ฃ์€ ๊ฒƒ ๊ธฐ์–ตํ•˜์‹œ๋‚˜์š”? ๊ทธ๋Ÿผ ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด Index์— ๋“ค์–ด ์žˆ๋Š” ํ”„๋ฆฌ๋ ‰ ๋“ฑ๋“ฑ์˜ ์ถœํŒ์‚ฌ๋ฅผ PubName setter์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ๋˜๊ฒ ๋„ค์š”! ์ด๊ฒƒ์„ ํ™•์ธํ•ด๋ด์•ผ๊ฒ ์–ด์š”!

 

Book Class์— setPubName Method

์ด ๊ณณ์— ์™€์„œ ๋ณด๋‹ˆ ์—ญ์‹œ String ํƒ€์ž…์œผ๋กœ ์ธ์ž ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”. pubnames ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜์—๋Š” ๋ฌธ์ž์—ด์ด ๋“ค์–ด ์žˆ์—ˆ์œผ๋‹ˆ ๊ทธ๊ฒƒ๋“ค์ด for๋ฌธ์„ ๋Œ๋ฉด์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋“ค์–ด์˜ค๊ฒŒ ๋  ๊ฒƒ์ด๊ณ , ๊ทธ ์ž…๋ ฅ๊ฐ’์ด this.pubName ์ฆ‰, pubName์ด๋ผ๋Š” ํ•„๋“œ์— ์ €์žฅ์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ ํƒ€์ž…์ด void๋‹ˆ return์€ ์ƒ๋žต๋˜์–ด ์žˆ๋Š” ๊ฒƒ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค!

์™”๋‹ค ๊ฐ”๋‹ค ์ •๋ง ์ •์‹ ์ด ์—†๋Š” ๊ฒƒ์ด์—์š”! ํ•˜์ง€๋งŒ, ๋‹ค์‹œ ๋Œ์•„๊ฐ€์„œ ๊ณ„์† ๋ถ„์„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

24๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹ˆ book ๋ณ€์ˆ˜ ๊ฐ’์„ bookStorage ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜์— Index ๋ฒˆํ˜ธ๋ฅผ ํ•˜๋‚˜์”ฉ ์˜ฌ๋ ค ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

book์—๋Š” ์–ด๋–ค ๋‚ด์šฉ์ด ์žˆ์—ˆ์ง€์š”? ๋ฐ”๋กœ ์ฑ… ์ œ๋ชฉ (setTitle)๊ณผ ์ถœํŒ์‚ฌ๋ช… (setPubName)์ด ๋“ค์–ด ์žˆ๋˜ ๊ฒƒ์ด์—์š”. ์ด ๋‘˜์„ ๊ฐ™์ด ๋„ฃ๊ณ  ์žˆ๋„ค์š”!

 

27๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹Œ๊นŒ pubStorage ํ•„๋“œ์— Publisher ํƒ€์ž… ๋ฐฐ์—ด 5์˜ ๊ธธ์ด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”. ์•„๋งˆ ์ถœํŒ์‚ฌ ์ •๋ณด๋ฅผ ๋„ฃ์„ ๊ฒƒ ๊ฐ™์ง€์š”?

๋‹ค์‹œ for๋ฌธ์ด ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค~~ ๋น™๊ธ€๋น™๊ธ€ ๋Œ์•„๊ฐ€๋Š” ์„ธ์ƒ~ for๋ฌธ์€ pubStorage ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ ๊ธธ์ด๋งŒํผ ๋„๋Š” ๊ฒƒ์ด์—์š”. 27๋ฒˆ์งธ ์ค„์—์„œ bpuStorage์•ˆ์— Publisher ํƒ€์ž… ๋ฐฐ์—ด 5์˜ ๊ธธ์ด๋ฅผ ๋„ฃ์—ˆ์œผ๋‹ˆ 5์˜ ๊ธธ์ด๋ฅผ ๊ฐ–๊ฒŒ ๋˜๊ฒ ๋„ค์š” ๊ทธ๋Ÿผ 5๋ฒˆ ๋Œ๊ฒŒ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค!

29๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹ˆ Publisher ํƒ€์ž…์— p๋ณ€์ˆ˜ ์•ˆ์— Publisher ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋„ฃ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

29๋ฒˆ์งธ ์ค„ ๋‚ด์šฉ์„ ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ฆฌ๋ฉด ์œ„์™€ ๊ฐ™์ด ๋‚˜์˜ค๊ฒŒ ๋˜๊ฒ ๋„ค์š”!

30๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹ˆ p๋ณ€์ˆ˜ ์•ˆ์— setName Method๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ pubnames [i]๋ฅผ ์ฃผ๊ณ  ์ž‡๋Š” ๊ฒƒ์ด์—์š”. setName์— ๊ฐ€๋ด์•ผ๊ฒ ์–ด์š”!

Publisher Class์˜ setName Method

pubnames [i]์˜ ์ž…๋ ฅ๊ฐ’์€ ๋ฌธ์ž์—ด ํƒ€์ž… name๋ณ€์ˆ˜๊ฐ€ ๋ฐ›์•„ name๋ณ€์ˆ˜์— ์ž…๋ ฅ๋˜๊ณ  ์ด๊ฒƒ์„ ํ•„๋“œ name์— ๋Œ€์ž…ํ•ด ์ค€ ๋’ค return์„ ํ•˜๊ณ  ์žˆ์–ด์š”!

pubnames ๋ฌธ์ž ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜์—๋Š” 5๊ฐœ์˜ ์ถœํŒ์‚ฌ๋ช…์ด ๋“ค์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ๋“ค์ด ํ•˜๋‚˜์”ฉ ๋“ค์–ด๊ฐ€์„œ p๋ณ€์ˆ˜์— ์ €์žฅ์ด ๋˜๊ฒ ๋„ค์š”.

31๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹ˆ p ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๋‚ด์šฉ์„ pubStorage ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜์— ๊ฐ Index์— ์ฐจ๊ณก์ฐจ๊ณก ์ €์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

ConsoleApp Class

์ถœํŒ์‚ฌ๋ช…์„ ์ €์žฅํ•˜๋Š” ๋‚ด์šฉ๋“ค์ด ๋‹ค ๋Œ์•˜์œผ๋‹ˆ ์ด๋ฅผ ํ˜ธ์ถœํ–ˆ์—ˆ๋˜ Main Method์— 31๋ฒˆ์งธ ์ค„๋กœ ๋Œ์•„์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค.

Publisher ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ pubs์™€ service ๋ณ€์ˆ˜ ์•ˆ์— searchPubsName Method์— ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์ด ์ „๋‹ฌ๋œ ์ถœํŒ์‚ฌ ๋ช…์„ ๋น„๊ตํ•ด์„œ ์žˆ๋‹ค๋ฉด ๋Œ€์ž…์„ ํ•ด ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

32๋ฒˆ์งธ ์ค„์— if ๋ฌธ์ด ์‹œ์ž‘๋˜๋Š”๋ฐ, pubs ๋ณ€์ˆ˜๊ฐ€ null์ธ์ง€๋ฅผ ํ™•์ธํ•˜๊ณ  ์žˆ๋„ค์š”. ๋งŒ์•ฝ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ–ˆ๋˜ ์ถœํŒ์‚ฌ๋ช…์ด ์žˆ์—ˆ๋‹ค๋ฉด pubs ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜์— ๋‹ด๊ฒผ์„ ํ…Œ๋‹ˆ if๋ฌธ ์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒ ์ง€์š”?

for ๋ฌธ์ด๋‹ˆ i๋Š” ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๊ณ , :๋ฅผ ์ถœ๋ ฅํ•œ ๋‹ค์Œ pubs๋ฐฐ์—ด์˜ i๋ฒˆ์งธ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ถœํŒ์‚ฌ ๋ช…์ด ๊ฒ€์ƒ‰๋˜์–ด pubs์— ๋“ค์–ด๊ฐ„ ์ถœํŒ์‚ฌ ๋ช…๋“ค์ด ์ถœ๋ ฅ์ด ๋˜๊ฒ ๊ณ , ๊ฒ€์ƒ‰๋˜์ง€ ์•Š์€ ๊ฒƒ๋“ค์€ null๊ฐ’์ž„์œผ๋กœ ๋ช‡ ๋ฒˆ์งธ ์ธ์ง€๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๊ฒ ๋„ค์š”.

๊ทธ๋Ÿฐ ๋‹ค์Œ String (๋ฌธ์ž์—ด) ํƒ€์ž… list ๋ณ€์ˆ˜์— service ์•ˆ์— serchBookBypubName Method๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ž…๋ ฅ๊ฐ’์œผ๋กœ pubname์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ serchBookByPubName์„ ํ™•์ธํ•˜๋Ÿฌ ๊ฐ€๋ด์•ผ๊ฒ ์–ด์š”!

 SearchService Class์— searchBookByPubName Method

pubName์„ ์ „๋‹ฌ๋ฐ›์€ ์ด Method์—์„  ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ ๊นŒ์š”? ๋จผ์ €, 86๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹ˆ Book ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ temp ์•ˆ์— Book ํƒ€์ž… ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ๊ทธ ๊ธธ์ด๋ฅผ bookStorage ๋ฐฐ์—ด๋งŒํผ์œผ๋กœ ํ•˜๋„ค์š”. bookStorage ๋ฐฐ์—ด์€ 10(SearchService Class 19๋ฒˆ์งธ ์ค„)์˜ ๊ธธ์ด๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ ์•Œ๊ณ  ๊ณ„์‹œ์ง€์š”?

67๋ฒˆ์งธ ์ค„์—์„œ ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜ count์— 0์„ ๋„ฃ์–ด ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด ์ฃผ๊ณ , for๋ฌธ์ด pubStorage ๊ธธ์ด(5 - SearchService Class 27๋ฒˆ์งธ ์ค„)๋งŒํผ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค. 

< for each ๋ฌธ ํ˜•์‹ >

for (๋ณ€์ˆ˜ ํƒ€์ž… ๋ณ€์ˆ˜๋ช… : ๋ฐฐ์—ด ์ด๋ฆ„) {
          // ์‹คํ–‰ ๋ถ€๋ถ„;
}

89๋ฒˆ์งธ ์ค„์„ ๋ณด๋ฉด for - each๋ฌธ์ด ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ์ด์—์š”. ๋ฐ˜๋ณต์ด ์ด๋ค„์งˆ ๋•Œ๋งˆ๋‹ค ๋ฐฐ์—ด์˜ ํ•ญ๋ชฉ(bookStorage)์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด Book ํƒ€์ž…์˜ book ๋ณ€์ˆ˜์— ๋„ฃ์–ด ์ค€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , if๋ฌธ์„ ๋งŒ๋‚˜๊ฒŒ ๋˜๋„ค์š”! bookStorage์—์„œ ํ•˜๋‚˜์”ฉ ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›์€ book์ด null์ธ์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. null์ด ์•„๋‹ˆ๋ฉด ๋‹ค์‹œ if๋ฌธ์„ ๋งŒ๋‚˜๊ฒŒ ๋˜๋Š”๋ฐ, book์•ˆ์— getPubName Method๊ฐ€ pubname๊ณผ ๊ฐ™์€์ง€๋ฅผ ํ™•์ธํ•˜๋Š” equals Method๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

๊ทธ๋Ÿฐ ๋’ค temp ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜์— count Index๋ฒˆํ˜ธ์— book ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ๋„ค์š”. ์ฒ˜์Œ์—๋Š” 0์œผ๋กœ ์‹œ์ž‘๋  ๊ฒƒ์ด๊ณ , 93๋ฒˆ์งธ ์ค„์— ++count;๊ฐ€ ์žˆ์œผ๋‹ˆ ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ๊ฒ€์ƒ‰๋œ book ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋’ค return์œผ๋กœ ์กฐ๊ฑด์‹ ์—ฐ์‚ฐ์ž (์‚ผํ•ญ ์—ฐ์‚ฐ์ž)๋ฅผ ํ†ตํ•ด count ๋ณ€์ˆ˜๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด temp๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹ˆ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. count๊ฐ€ 0๋ณด๋‹ค ์ž‘๋‹ค๋Š” ๊ฒƒ์€ boot์•ˆ์— getpubName์ด pubname๊ณผ ๊ฐ™์€ ๊ฒƒ์ด ์—†์–ด count๊ฐ€ ์ฆ๊ฐ€ํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ๊ทธ๊ฑด ๊ฒฐ๊ตญ ๊ฒ€์ƒ‰๋œ ์ฑ…์ด ์—†๋‹ค๋Š” ๊ฒƒ์ด๊ฒ ์ง€์š”?

ํœด! ํž˜๋“œ๋„ค์š”! ๋‹ค์‹œ ConsoleApp Class์— 40๋ฒˆ์งธ ์ค„์„ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. if๋ฌธ์„ ๋งŒ๋‚˜๋Š”๋ฐ, list๊ฐ€ null์ด ์•„๋‹ˆ๋ƒ๊ณ  ๋ฌป๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ list์—๋Š” searchBookByPubName Method์— pubname์„ ์ž…๋ ฅํ•˜์—ฌ ๊ฒ€์ƒ‰๋œ ์ฑ…์ด ์žˆ๋‹ค๋ฉด ์ „๋‹ฌ ๊ฐ’์œผ๋กœ temp๊ฐ€ ์ „๋‹ฌ๋˜์–ด null์ด ์•„๋‹ ๊ฒƒ์ด๊ณ , ๊ฒ€์ƒ‰๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ฉด null์ด ๋ฐ˜ํ™˜๋˜์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž! ๊ทธ๋‹ค์Œ์— for๋ฌธ์„ ๋งŒ๋‚ฉ๋‹ˆ๋‹ค list ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ ๊ธธ์ด (์ฑ… ๊ฒ€์ƒ‰ ๊ธธ์ด)๋งŒํผ ๋„๋Š”๋ฐ, ๊ทธ ์•ˆ์— ๋ณด๋‹ˆ ๋˜ if๋ฌธ์ด list์˜ b Index์— null์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. null์ด ์•„๋‹ˆ๋ฉด ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ b๋กœ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•œ ๋‹ค์Œ list [i]๋กœ ์ถœ๋ ฅ์„ ํ•˜๊ฒŒ ๋˜๊ฒ ๋„ค์š”!

 

ConsoleApp Class์— else๋ฌธ

์ด else๋ฌธ์€ 35๋ฒˆ์งธ if๋ฌธ์— else์ž…๋‹ˆ๋‹ค. ์ฆ‰, pubs [i]์— null๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’ ๋‚ด์šฉ์ด ๊ฒ€์ƒ‰๋˜์ง€ ์•Š์•„ "๋“ฑ๋ก๋œ ๊ฑฐ ์—†๋‹ค"๋ผ๊ณ  ์ถœ๋ ฅํ•˜๊ณ , break;๋ฅผ ๋งŒ๋‚˜ swich๋ฌธ์„ ๋‚˜๊ฐ€ ๋‹ค์‹œ while๋ฌธ์ด ๋™์ž‘ํ•˜๊ฒŒ ๋˜๊ฒ ๋„ค์š”.

 

์ž! ์ด์ œ ๋˜ ๋‹ค๋ฅธ case๋ฅผ ๋ถ„์„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ConsoleApp Class 53 ~ 76๋ฒˆ์งธ ์ค„

์‚ฌ์šฉ์ž๊ฐ€ menu๊ฐ€ ๋ฐ›๋Š” ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋ฌธ์ž์—ด 1์„ ์ฃผ์—ˆ๋‹ค๋ฉด ์ด ๊ณณ์œผ๋กœ ์˜ค๊ฒŒ ๋˜๊ฒ ์ง€์š”?

๋จผ์ € ๊ฒ€์ƒ‰ํ•  ๋„์„œ๋ช…์„ ๋ฌป๊ณ , ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ›์•„ ๋ฌธ์ž์—ด ํƒ€์ž… key ๋ณ€์ˆ˜์— ๋„ฃ์–ด ์ค๋‹ˆ๋‹ค.

57๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹ˆ Book ํƒ€์ž…์— ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ list์— service์•ˆ์— serchBookName ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์žˆ๋Š” key๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ „๋‹ฌํ•ด ์ฃผ๊ณ  ์žˆ๋„ค์š”! serchBookName์„ ํ™•์ธํ•ด ๋ด์•ผ๊ฒ ์–ด์š”!

 

SerchService Class์˜ searchBookName Method

์ด Method๋Š” ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ ๋ฐ›์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— String(๋ฌธ์ž์—ด) ํƒ€์ž…์œผ๋กœ bookTitle์„ ๋ฐ›๊ณ  ์žˆ๋„ค์š”.

38๋ฒˆ์งธ ์ค„์„ ๋ณด๋‹ˆ Book ํƒ€์ž…์— ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜  temp ์•ˆ์— Book ํƒ€์ž… ๋ฐฐ์—ด์„ ๋„ฃ๋Š”๋ฐ, ๊ทธ ๊ธธ์ด๋Š” bookStorage (10์˜ ๊ธธ์ด - SearchService Class 19๋ฒˆ์งธ ์ค„)๋งŒํผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  ์žˆ๋„ค์š”. ๊ทธ๋Ÿฐ ๋’ค ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜ count๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์ด๋ฅผ ๊ฒ€์ƒ‰๋œ ๊ฐœ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

๋‹ค์Œ for๋ฌธ์„ ๋งŒ๋‚ฉ๋‹ˆ๋‹ค. bookStorage ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ์ด๋‹ˆ 10๋ฒˆ ๋Œ๊ฒŒ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค!

๊ทธ๋Ÿฐ ๋’ค if๋ฌธ์„ ๋งŒ๋‚˜๋Š”๋ฐ, bookStorage ๊ฐ Index ์•ˆ์— null์ด ์žˆ๋Š”์ง€ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์‹ค์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. null ์•„๋‹ˆ ์•„๋‹ˆ๋ฉด Book ํƒ€์ž…์— ์ฐธ์กฐํ˜• ๋ณ€์ˆ˜ book์— bookStorage [i] ๋ฒˆ์งธ ์ •๋ณด๋ฅผ ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด ์ฃผ๊ฒŒ ๋˜๊ณ , ๋‹ค์‹œ if๋ฌธ์„ ๋งŒ๋‚˜ book์— ์ž…๋ ฅ๋˜์—ˆ๋˜ ๊ฐ’๋“ค์ด getTitle์„ ๊ฑฐ์ณ Book Class์— private String title ํ•„๋“œ์— ์žˆ๋Š” ๊ฒƒ์„ ์ฝ์–ด๋“œ๋ฆฌ๊ณ , ๊ทธ๊ฒƒ์ด ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’ ๋‚ด์šฉ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ธ€์ž๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

์ฐธ์ด๋ผ๋ฉด temp๋ฐฐ์—ด์— ๊ฒ€์ƒ‰๋œ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” count๋ณ€์ˆ˜๋ฅผ Index๋ฒˆํ˜ธ๋กœ ํ•˜์—ฌ ๊ทธ ์•ˆ์— book์•ˆ์— ๊ฐ’์„ ๋„ฃ์–ด์ค€ ๋’ค count ๊ฐ’์„ ํ•˜๋‚˜ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋’ค 51๋ฒˆ์งธ ์ค„์—์„œ count๊ฐ€ 0์ธ์ง€๋ฅผ ํ™•์ธํ•˜์—ฌ ๊ฒ€์ƒ‰๋œ ๊ฒƒ์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ณ , ์žˆ๋‹ค๋ฉด Book ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ list์— Book ํƒ€์ž… ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ๊ทธ ๊ธธ์ด๋Š” count (๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๊ฐœ์ˆ˜)๋งŒํผ์˜ ๊ธธ์ด๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ๋ฐฐ์—ด์€ ๊ฐ’์„ ์ง์ ‘ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ฐธ์กฐ ์ฆ‰, ์ฃผ์†Œ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์ž„์„ ์žŠ์–ด์„œ๋Š” ์•„๋‹ˆ ๋˜๋Š” ๊ฒƒ์ด์—์š”!

54๋ฒˆ์งธ ์ค„์—์„œ for๋ฌธ์„ ๋งŒ๋‚ฌ์–ด์š”! count๋งŒํผ์ด๋‹ˆ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๊ฐœ์ˆ˜๋งŒํผ ๋Œ๊ฒ ๋„ค์š”! ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ๊ฐœ์ˆ˜๋งŒํผ temp ๋ฐฐ์—ด ๋‚ด์šฉ์„ list์˜ i๋ฒˆ์งธ ๋ฐฐ์—ด์— ๋„ฃ๊ณ , 58๋ฒˆ์งธ ์ค„์—์„œ ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ list์— ์žˆ๋Š” ๊ฐ’์„ ๋„ฃ์–ด ์ค๋‹ˆ๋‹ค. 55๋ฒˆ์งธ ์ค„์—์„œ ์ € ๊ฒƒ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Method๊ฐ€ ์žˆ๋Š”๋ฐ, ์•„๋ž˜ ์ฐธ๊ณ ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ผ๋Š” ๊ฒƒ์ด์—์š”.

๋งŒ์•ฝ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด if๋ฌธ์„ ๋“ค์–ด๊ฐ€์ง€ ๋ชปํ•˜๊ณ , ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ null์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

 

์ž! ๋‹ค์‹œ Main Method๋กœ ๋Œ์•„๊ฐ€ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 ConsoleApp Class์˜ 59 ~ 76๋ฒˆ์งธ ์ค„

serchBookName Method์—์„œ ์—ฐ์‚ฐ๋œ ๊ฒฐ๊ณผ๋Š” Book ํƒ€์ž… ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜ list์— ์ €์žฅ์ด ๋˜์—ˆ์„ ๊ฒƒ์ด๊ณ , ์ด์ œ 59๋ฒˆ์งธ ์ค„์—์„œ if๋ฌธ์„ ๋งŒ๋‚˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. list๊ฐ€ null์ธ์ง€๋ฅผ ๋ฌป๊ณ  ์žˆ๋„ค์š”. ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด list๋Š” null์ด์—ˆ์„ ๊ฒƒ์ด๊ณ , ๊ทธ๋ ‡๋‹ค๋ฉด ์ด if๋ฌธ์— ๋“ค์–ด๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” "๋“ฑ๋ก๋œ ๋„์„œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

else ์•„๋‹ˆ๋ผ๋ฉด? ์ฆ‰, list๊ฐ€ null์ด ์•„๋‹ˆ๋ผ๋ฉด for๋ฌธ์„ ๋งŒ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. list ๊ธธ์ด๋งŒํผ์ด๋‹ˆ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋งŒํผ ๋Œ๊ฒŒ ๋˜๊ฒ ๋„ค์š”!

Book ํƒ€์ž…์— book ์ฐธ์กฐํ˜• ๋ณ€์ˆ˜์— ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ด๊ธด list ๋ฐฐ์—ด์— ๋‚ด์šฉ์„ i๋ฒˆ์”ฉ ํ•˜๋‚˜ํ•˜๋‚˜ ์˜ฎ๊ฒจ ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋’ค ๋‹ค์‹œ if๋ฌธ์„ ๋งŒ๋‚˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋Š”๋ฐ, book์ด null์ด ์•„๋‹ˆ๋ฉด ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ์ฃผ๊ฒ ๋„ค์š”!

์—ฌ๊ธฐ์„œ ํ•œ ๊ฐ€์ง€ ์•Œ์•„์•ผ ํ•  ๊ฒƒ์€ println์— book์„ ์ถœ๋ ฅ์„ ํ•˜๋Š”๋ฐ, toString์ด๋ผ๋Š” Method๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

Book Class์˜ toStirng Method

 

์ดํด๋ฆฝ์Šค์—์„œ ๋งˆ์šฐ์Šค ์šฐํด๋ฆญ

์ดํด๋ฆฝ์Šค์—์„œ ์ฝ”๋“œ ์ž…๋ ฅ ๋ถ€๋ถ„์— ๋งˆ์šฐ์Šค ์šฐํด๋ฆญ์„ ํ•˜๋ฉด Source๊ฐ€ ๋ณด์ด๊ฒŒ ๋  ๊ฒƒ์ด๊ณ , ์ด Source ์•ˆ ๋ฐ‘์— ๋ณด๋ฉด Generate toStrint()... ์ด ๋ณด์ด๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”. ์ด๊ฒƒ์„ ๋ˆ„๋ฅด๋ฉด toString Method๊ฐ€ ๋ณด์ด๊ฒŒ ๋˜๊ณ , Overriding์„ ํ•˜์—ฌ ์ฐธ์กฐ ๋ฐฐ์—ดํ˜• ๋ณ€์ˆ˜๋ฅผ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๋ณด์ด๋Š” ์ฃผ์†Œ ๊ฐ’์ด ์•„๋‹Œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ์ถœ๋ ฅ๋˜๋„๋ก ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

toString Method๋ฅผ ๋ณด๋ฉด ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ Book [title=์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋˜๊ณ , title ํ•„๋“œ ๊ฐ’์ด ๋‚˜์˜ค๊ณ , pubName= ๋ฌธ์ž๊ฐ€ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋œ ๋’ค pubName ํ•„๋“œ์˜ ๊ฐ’์ด ์ถœ๋ ฅ์ด ๋  ๊ฒƒ์ด์—์š”.

 

69๋ฒˆ์งธ ์ค„์— break๋Š” switch๋ฌธ์— ๋Œ€ํ•œ break๋กœ ๊ฒ€์ƒ‰์ด ์™„๋ฃŒ๋œ ๋’ค ๋‹ค์‹œ while๋ฌธ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

73๋ฒˆ์งธ ์ค„์— ๋ณด๋ฉด kb.close();๋ผ๊ณ  ์žˆ๋Š”๋ฐ, ์ด Method์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜ ์ฐธ๊ณ ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธธ ๋ฐ”๋ผ๋Š” ๊ฒƒ์ด์—์š”.

 

 

 

๐Ÿ’ฌ ์ฐธ๊ณ ์‚ฌํ•ญ


contains(); ์ด Method๋Š” ๋น„๊ต ๋Œ€์ƒ๋“ค์— ๋ฌธ์ž๋“ค ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ฐ™์€ ๊ฒŒ ์žˆ์œผ๋ฉด ์ฐธ์ด ๋ฉ๋‹ˆ๋‹ค.

equlas(); ์ด Method๋Š” ๋น„๊ต ๋Œ€์ƒ๋“ค์— ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์ผ์น˜ํ•ด์•ผ ์ฐธ์ด ๋ฉ๋‹ˆ๋‹ค.

System.arraycopy (์ €์žฅ ๋ณ€์ˆ˜๋ช…, Index ์ดˆ๊ธฐ ๋ฒˆํ˜ธ, ๊ฐ’์„ ์ „๋‹ฌํ•  ๋ณ€์ˆ˜๋ช…, Index ์ดˆ๊ธฐ๋ฒˆํ˜ธ, ๊ฐœ์ˆ˜);

  - ์ด๊ฒƒ์€ ๊ฐ’์„ ์ „๋‹ฌํ•  ๋ณ€์ˆ˜์—์„œ ์ €์žฅ ๋ณ€์ˆ˜์—๊ฒŒ Index ์ดˆ๊ธฐ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๊ฐœ์ˆ˜๊นŒ์ง€ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” Method์ธ ๊ฒƒ์ด์—์š”!'

close(); ์ด Method๋Š” ํ‚ค๋ณด๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์„ ๋๋‚œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์‚ฌ์šฉ์ด ๋๋‚ฌ๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค๋ณด๋“œ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ์„ ๋Œ๋ ค์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 


์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค!


728x90
๋ฐ˜์‘ํ˜•