Meber.java
package test;
public class Member {
private String name;
private int height;
private int weight;
private String nation;
public Member(String name, int height, int weight, String nation){
this.name = name;
this.height = height;
this.weight = weight;
this.nation = nation;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getHeight(){
return this.height;
}
public void setHeight(int height){
this.height = height;
}
public int getWeight(){
return this.weight;
}
public void setWeight(int weight){
this.weight = weight;
}
public String getNation(){
return this.nation;
}
public void setNation(String nation){
this.nation = nation;
}
}
SearchService.java
package test;
public class SearchService {
Member[] members = new Member[5];
public SearchService(){
members[0] = new Member("오정원", 167, 77, "대한민국");
members[1] = new Member("James", 187, 67, "미국");
members[2] = new Member("가루상", 166, 57, "일본");
members[3] = new Member("홍금보", 177, 80, "중국");
members[4] = new Member("인도인" , 199, 77, "인도");
}
boolean searchMember(String name){
boolean searchResult = false;
for(int i = 0; i < members.length; i++){
if(members[i].getName().equals(name)){
printInfo(members[i]);
searchResult = true;
}
}
return searchResult;
}
private void printInfo(Member member){
System.out.println(member.getName() + "으로 찾은 결과");
System.out.println("이름 : " + member.getName());
System.out.println("신장 : " + member.getHeight());
System.out.println("몸무게 : " + member.getWeight());
System.out.println("국가 : " + member.getNation());
}
}
test.java
package test;
import java.util.Scanner;
public class test {
public static void main(String[] args){
SearchService sv = new SearchService();
Scanner sc = new Scanner(System.in);
do{
System.out.println("검색할 회원을 입력하세요.");
String name = sc.next();
boolean searchResult = sv.searchMember(name);
if(searchResult) break;
System.out.println("검색된 회원이 없습니다.");
}while(true);
}
}
물론 프로젝트 명은 test이며 다양한 클래스를 만들어서 하는 프로젝트인 것 같은데...........
책에 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ설명이 안나오고 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ예제만 떡하니 나온건 조금 그랬다. ㅎㅎ
'웹 프로그래밍 > Spring Framework' 카테고리의 다른 글
[JAVA] final 예약어 (0) | 2014.07.15 |
---|---|
[JAVA] 상속(Extends) (0) | 2014.06.09 |
[JAVA] this() 생성자(코드의 간결함을 위한!) (0) | 2014.06.08 |
[JAVA] new 생성자 (0) | 2014.06.08 |
[JAVA] 정적 메소드와 인스턴스 메소드(Static Method and Instance Method) (0) | 2014.06.08 |