(13) 문제

JAVA/JAVA 이론 2018. 11. 1. 22:46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package hello;
 
// 데이터 저장을 위한 클래스 : Value Object
public class Hello
{
    // 속성 - 데이터를 저장하기 위함
    private String Eng;
    private String Kor;
    private int Level;
    private String Wdate;
    
    // 인스턴스 메소드
    // 예) cat : 고양이 (★), 181030
    
    public String print()
    {
        String temp = "";
        
        for(int i=0;i<Level;i++)
        {
            temp +="★";
        }
        return Eng + " : " + Kor + "(" + temp + ")" + " , " + Wdate;
    }
    
    // 생성자 이름은 클래스 이름하고 똑같다
    public Hello(String Eng, String Kor, int Level , String Wdate)
    {
        // new Word(); 생성 못함
        // 별도로 기본생성자 정의를 해야 할 수 있음
        this.Eng = Eng;
        this.Kor = Kor;
        this.Level = Level;
        this.Wdate = Wdate;
    }
    
    // getter & setter 사용
    // 파랑글씨 = 속성 값
    
    public String getEng()
    {
        return Eng;
    }
    public void setEng(String Eng)
    {
        this.Eng = Eng;
    }
    public String getKor()
    {
        return Kor;
    }
    public void setKor(String Kor)
    {
        this.Kor = Kor;
    }
    public int getLevel()        
    {
        return Level;
    }
    
    public void setLevel(int Level)    
    {
        this.Level = Level;                                
    }
    
    public String getWdate()        
    {
        return Wdate;
    }
    
    public void setName(String Wdate)    
    {
        this.Wdate = Wdate;                                
    }
}
 
cs





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package hello;
 
import java.util.Scanner;
 
public class HelloTest 
{
 
    public static void main(String[] args) 
    {
        Hello[] wordbook = new Hello[10];
        
        Scanner sc = new Scanner(System.in);
        
        int i =0;
        
        String Eng , Kor , Wdate;
        int Level;
        
        System.out.println("[[My 단어장]]");
        System.out.println("단어장 종료 : quit , 모두 출력 : print , 단어 검색 : find ");
        
        while(true)
        {
            System.out.print("***영어 단어 >>> ");
            Eng = sc.nextLine();
            
            if(Eng.equals("quit"))
            {
                break;


            }
            
            if(Eng.equals("print"))
            {
                // 참조하는 객체가 null이면 print() 메소드 실행오류
                print_list(wordbook,i);        // 배열의 참조 값 , 반복횟수를 메소드에게 전달 


       
            }
            else if(Eng.equals("find")) // 검색
            {
                find(wordbook,i);


            }
            else
            {
                System.out.print("*** 한글 뜻 >>> ");
                Kor = sc.nextLine();
                
                System.out.print("*** 레벨 (1~3) >>> ");
                Level = Integer.parseInt(sc.nextLine());
                
                System.out.print("*** 날짜 >>> ");
                Wdate = sc.nextLine();
                
                // 입력받은 값으로 객체 생성하고 배열에서 참조하기
                
                wordbook[i] = new Hello(Eng,Kor,Level,Wdate);
                
                i++;
                sc.nextLine();
            }            
        }
        System.out.println("끝");
    }
    // 메소드로 전달되는 값 : 배열의 참조 값 , 반복횟수
    public static void print_list(Hello[]w,int cnt)    // 모두 출력
    {
        for(int k=0; k < cnt; k++ )        // 객체가 생성된 개수만큼 반복
        {
            System.out.println(w[k].print());
        }
    }
    
    public static void find(Hello[]w,int cnt)    // 단어 추가
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("[[단어 검색]] 검색단어 입력 >>> ");
        String find = sc.nextLine();
        for(int k = 0; k < cnt; k++)
        {
            //비교
            if(w[k].getEng().equals(find))
            {
                System.out.println(w[k].print());
            }
           
        }
    }
 
}
 
cs


'JAVA > JAVA 이론' 카테고리의 다른 글

(12) 메소드 & 객체  (0) 2018.11.01
(11) Method & Class  (0) 2018.10.30
(10) 2중 for 문 & 2차원 배열  (0) 2018.10.29
(9) String Method  (0) 2018.10.28
(8) Array  (0) 2018.10.25

설정

트랙백

댓글