본문 바로가기

Learning/JAVA

private, static, 자바 클래스 분리해서 입력메뉴 만들고 데이터출력

  • 과일의 가격, 할인율, 할인율에 따른 과일가격 변동 출력 (Good, GoodMain)
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
package day05_object;
public class Good {
     String num;
     String name;
     String company;
     int price;
     double discountRate;
     
     public Good(String num, String name, String company, int price) {
          this.num=num;
          this.name=name;
          this.company=company;
          this.price=price;
     }
     
     public int getSellingPrice() {
          return price-(int)(price*discountRate);
     }
     
     public void updateDiscountRate(double rate) {
          discountRate=rate;
     }
     
     public double discountRate() {
          return discountRate;
     }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ackage day05_object;
public class GoodMain {
     public static void main(String[] args) {
          Good g1=new Good("001","레몬","레몬사",1000);
          System.out.println(g1.name+":"+g1.getSellingPrice());
          
          Good g2=new Good("002","사과","애플사",1500);
          System.out.println(g2.name+":"+g2.getSellingPrice());
     
          g2.updateDiscountRate(0.2);
          
          System.out.println("사과:"+g2.getSellingPrice());
          System.out.println(g2.name+"할인율: "+g2.discountRate());
     }
}

 


  • 배열에 책제목과 저자 입력하고 출력하기 (BookBean, BookMain)
  • public은 누구나 접근.
  • default는 접근제어자 생략. 같은 패키지 안에서는 접근가능.
  • private은 자신만 접근.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package day06_objectArray;
public class BookBean {
     private String title;
     private String writer;
     
     public BookBean(String title, String writer) {
          this.title=title;
          this.writer=writer;
     }
     //getter. title과 writer가 private이기 때문에
     //BookMain에서 접근하지 못함.
     //따라서 public으로 메소드를 만들어서 값을 돌려주기.
     public String getTitle() {
          return title;
     }
     
     public String getWriter() {
          return writer;
     }
     
}

 

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
package day06_objectArray;
 
import java.util.Scanner;
 
public class BookMain {
    static Scanner sc =new Scanner(System.in);
    BookBean[]arr=new BookBean[50];
    int cnt; //멤버변수의 초기값은 0이라서 굳이 0 안줘도 됨.
    
    public void showMenu() {
        System.out.println("선택하세요");
        System.out.println("1.데이터입력");
        System.out.println("2.전체보기");
        System.out.println("3.종료");
        System.out.println("선택>>");
    }
    //데이터 입력
    public void bookInsert() {
        System.out.println("데이터 입력하세요...");
        System.out.print("책제목: ");
        String title=sc.nextLine(); //한줄 입력받는 것. 한줄은 엔터 입력 전까지 인식.
        System.out.print("책저자: ");
        String writer=sc.nextLine();
        arr[cnt++= new BookBean(title, writer); //입력받으면 배열에 저장하기.
    }
    
    public void bookList() {
        System.out.println("제목\t작가");
        /*
        for(int i=0;i<cnt;i++) {
            System.out.print(arr[i].title+"\t");
            System.out.print(arr[i].writer+"\n");
        }
        */
        
        for(BookBean book : arr) { //향상된 for문. for-each
            if(book==nullbreak;
            System.out.print(book.getTitle()+"\t");
            System.out.print(book.getWriter()+"\n");
        }
        
    }
    
    public static void main(String[] args) {
        BookMain bm=new BookMain();
        while(true) {
            bm.showMenu(); //메뉴
            int choice=sc.nextInt();
            BookMain.sc.nextLine(); //그 전의 엔터를 버림
            switch(choice) {
            case 1 : bm.bookInsert(); break;
            case 2 : bm.bookList(); break;
            case 3 : System.out.println("종료");
                     System.exit(0);
            default : System.out.println("입력오류");
            }
        }
    }
}

 


  • 배열에 선수 데이터입력하고 선수 이름과 나이 출력하기 (Player, PlayerManager, PlayerMain)
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
package day06_objectArray;
 
public class Player {
    private String name, address;
    private int age;
    private double height, weight;
 
    public Player(String name, String address, int age, double height, double weight) {
        this.name=name;
        this.address=address;
        this.age=age;
        this.height=height;
        this.weight=weight;
    }
 
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void getInfo() {
        System.out.println("이름: " +name);
        System.out.println("주소: " +address);
        System.out.println("나이: " +age);
        System.out.println("키: " +height);
        System.out.println("몸무게: " +weight);
    }
 
}
 
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 day06_objectArray;
 
public class PlayerManager {
    private final int MAX_INT=50;
    Player[]arr=new Player[MAX_INT];
    private int cnt;
    
    void insertPlayer() {
        System.out.println("선수 정보 등록...");
        System.out.print("이름: ");
        String name=PlayerMain.sc.nextLine();
        System.out.print("주소: ");
        String address=PlayerMain.sc.nextLine();
        System.out.print("나이: ");
        int age=PlayerMain.sc.nextInt();
        System.out.print("키: ");
        double height=PlayerMain.sc.nextDouble();
        System.out.print("몸무게: ");
        double weight=PlayerMain.sc.nextDouble();
        arr[cnt++]=new Player(name, address, age, height, weight);
    }
    
    public void viewPlayer() {
        System.out.println("선수이름\t나이");
        for(Player player : arr) {
            if(player==nullbreak;
            System.out.print(player.getName()+"\t");
            System.out.print(player.getAge()+"\n");
        }
    }
    
    public void searchPlayer() {
        System.out.println("찾을 선수 이름>>");
        String searchName=PlayerMain.sc.next();
        Player p = search(searchName);
        if(p==null) {
            System.out.println("찾는 선수 없음");
            return;
        }
        p.getInfo();
    }
    
    public Player search(String searchName) { //리턴형이 객체일 수도 있다.
        for(int i=0;i<cnt;i++) {
            if(searchName.equals(arr[i].getName())) {
                return arr[i]; //객체를 리턴시켜줌.
            }
        }
        return null;
    }
        
    /*
    public void searchPlayer() {
        System.out.println("찾을 선수 이름>>");
        //이름이 같으면 찾는 선수
        //찾는 선수의 모든 정보를 출력
        //없으면 찾는 선수가 없습니다. 라고 출력
        
        String search=PlayerMain.sc.next();
        Player p=null;
 
        for(int i=0;i<cnt;i++) {
            if(search.equals(arr[i].getName())) {
                p=arr[i];
                break;
            }
        }
        if(p!=null) {
            //이름, 나이, 주소, 키, 몸무게
            p.getInfo();
        }else{
            System.out.println("찾는 선수가 없습니다.");
        }
        */
}
 
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
package day06_objectArray;
 
import java.util.Scanner;
 
public class PlayerMain {
    static Scanner sc=new Scanner(System.in);
 
    public static void showMenu() {
        System.out.println("선택하세요");
        System.out.println("1.선수 데이터입력");
        System.out.println("2.전체보기");
        System.out.println("3.선수찾기");
        System.out.println("4.종료");
        System.out.println("선택>>");
    }
    
    public static void main(String[] args) {
        PlayerManager pm=new PlayerManager();
        while(true){
            PlayerMain.showMenu();
            int choice=PlayerMain.sc.nextInt();
            PlayerMain.sc.nextLine();
            switch(choice) {
            case 1 : pm.insertPlayer(); break
            //PlayerManager에다 pm을 만들었으니
            //PlayerManager에서 insertPlayer, viewPlayer메소드 만들어줌.
            case 2 : pm.viewPlayer(); break//이름, 나이 출력
            case 3 : pm.searchPlayer(); break;
            case 4 : System.out.println("종료");
                     System.exit(0);
            default : System.out.println("입력오류");
            }
        }
    }
}