Progamming/Java

[Java]Java 학교 프로젝트 - 가상 항공권 예매 프로그램(gui)

SteffenLee 2018. 7. 15. 17:24

학교에서 자바 프로젝트를 해서 항공권 예매 프로그램을 제작 해봤습니다.


개발 동기는 프로젝트에서 상속과 인터페이스 추상클래스를 사용하라는 조건이 주어졌고, 비행기에 관한 걸 추상클래스나 인터페이스 등을 사용하면 각 항공사마다 사용하기 쉽다고 판단해서 개발하게 되었습니다.



프로그램 구동

시작 페이지




메인 페이지

항공사 칸은 버튼을 활용해서 분리했습니다.



예메 페이지

좌석 페이지는 버튼을 각각 따로 만들었습니다.



마우스 커서는 안보이지만 커서를 올리면 노란색으로 변하고, 클릭을 해도 노란색이 됩니다.



이름과 주민번호를 입력하는데 주민번호에 숫자가 아닌 문자를 넣으면 오류가 발생합니다.


소스코드


시작화면


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
    package ticketing;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    interface Airplane {
        public void buyflight();
    }
    
    public class StartPage extends JFrame {
    
        public static JPanel contentPane;
    
        public StartPage() {
            List li = new List();
            
            setTitle("린청공항 항공권 예매");
    
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setBounds(100100, Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
            contentPane = new JPanel();// 버튼이나 라벨이 들어갈 화면을 만듬
            contentPane.setBorder(new EmptyBorder(5555));
            setContentPane(contentPane);
            contentPane.setLayout(null);
    
            JButton startbtn = new JButton("예매");//예매버튼을 만듬
            startbtn.setFont(new Font("굴림", Font.PLAIN, 36));
            startbtn.setBounds(55148113844);
            contentPane.add(startbtn);
    
            JLabel label = new JLabel("린청공항 항공편 예매");//제목을 만듬
            label.setForeground(Color.WHITE);
            label.setFont(new Font("굴림", Font.PLAIN, 50));
            label.setBounds(39914249180);
            contentPane.add(label);
    
            JLabel background = new JLabel("");//배경화면 설정
            background.setIcon(new ImageIcon("src\\images\\airport.jpg"));
            background.setBounds(001262673);
            contentPane.add(background);
    
            setVisible(true);
    
            startbtn.setCursor(new Cursor(Cursor.HAND_CURSOR));// 버튼 위에 마우스 포인터를 올리면 커서 모양을 손으로 바꿈
            startbtn.addMouseListener(new MouseAdapter() {// 마우스 클릭 이벤트
                @Override
                public void mousePressed(MouseEvent e) {
                    contentPane.removeAll();//모든 컴포넌트를 지움
                    contentPane.revalidate();
                    contentPane.repaint();//다시 그려줌
                    //항공사 리스트 
                    li.List_Asiana();
                    li.List_KoreanAir();
                    li.List_Jinair();
                }
            });
        }
    }
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
package ticketing;
 
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;
 
abstract class function extends JFrame{ 
    public abstract void List_Asiana();
    public abstract void List_KoreanAir();
    public abstract void List_Jinair();
}
 
class GetUserInfo{
    String[] name = new String[100];
    int[] idnum = new int[100];
}
 
 
class List extends function{
    //항공사 
    @Override 
    public void List_Asiana() {
 
        JLabel AsianaIcon = new JLabel(""); //아시아나 로고 이미지를 띄움
        AsianaIcon.setIcon(new ImageIcon("src\\images\\asiana.jpg"));
        AsianaIcon.setBounds(3455219119); 
        StartPage.contentPane.add(AsianaIcon);
        StartPage.contentPane.setBackground(new Color(255250250));
 
        
        JLabel flight = new JLabel("항공편 명 : OZ541"); 
        flight.setFont(new Font("굴림", Font.PLAIN, 20)); 
        flight.setBounds(2915640018);                
        StartPage.contentPane.add(flight);
 
        //항공사를 분리하는 상자/버튼을 활용함
        JButton box = new JButton(); 
        box.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { } 
        });
 
        JButton buybutton1 = new JButton("구매하기"+ " " + "가격 : 1,300,000won~"); 
        buybutton1.setBounds(88955345119); 
        StartPage.contentPane.add(buybutton1);
 
        JLabel Start = new JLabel("출발지 : 인천(ICN)"); 
        Start.setFont(new Font("굴림", Font.PLAIN, 20)); 
        Start.setBounds(2918617518);
        StartPage.contentPane.add(Start);
 
        JLabel arrive = new JLabel("도착지 : 프랑크프루트(FRA)"); 
        arrive.setFont(new Font("굴림", Font.PLAIN, 20)); 
        arrive.setBounds(29111627118);
        StartPage.contentPane.add(arrive);
 
        JLabel price = new JLabel("출발 시각 : 13 : 00"); 
        price.setFont(new Font("굴림", Font.PLAIN, 20));
        price.setBounds(29114617518);
        StartPage.contentPane.add(price);
 
        box.setForeground(Color.WHITE); 
        box.setEnabled(false); 
        box.setBackground(new Color(220220220));
        box.setBorderPainted(false); 
        box.setBounds(14421234147); 
        StartPage.contentPane.add(box);
 
        //구매하기 버튼을 눌렀을 때 이벤트
        buybutton1.setCursor(new Cursor(Cursor.HAND_CURSOR));
        buybutton1.addMouseListener(new MouseAdapter() {
            @Override 
            public void mousePressed(MouseEvent e) { 
                StartPage.contentPane.removeAll();
                StartPage.contentPane.revalidate();
                StartPage.contentPane.repaint();
                AirplaneStruct Buy = new AirplaneStruct();
                Buy.printflight("OZ541");//콘솔창에 띄울 항공편명을 printflight에 넘겨줌
                Buy.buyflight();
            }
        });
        //////////////////////코드 중략/////////////////////////////////////////////////////
    }     
    
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class AirplaneStruct extends GetUserInfo implements Airplane{
 
    public void buyflight() {
        //좌석 버튼
        JLabel lblNewLabel = new JLabel("\uC88C\uC11D\uD45C\r\n");
        lblNewLabel.setBounds(596011756);
        StartPage.contentPane.add(lblNewLabel);
        lblNewLabel.setFont(new Font("굴림", Font.BOLD, 37));
 
        JButton button_1 = new JButton("좌석");
        button_1.setBackground(Color.LIGHT_GRAY);
        button_1.setBounds(14140127103);
        StartPage.contentPane.add(button_1);
 
        JButton button_2 = new JButton("좌석");
        button_2.setBackground(Color.LIGHT_GRAY);
        button_1.setBounds(14140127103);
        StartPage.contentPane.add(button_1);
 
        button_2.setBounds(200140127103);
        StartPage.contentPane.add(button_2);
 
        JButton button_3 = new JButton("좌석");
        button_3.setBackground(Color.LIGHT_GRAY);
        button_3.setBounds(383140127103);
        StartPage.contentPane.add(button_3);
 
        JButton button_4 = new JButton("좌석");
        button_4.setBackground(Color.LIGHT_GRAY);
        button_4.setBounds(575140127103);
        StartPage.contentPane.add(button_4);
 
        JButton button_5 = new JButton("좌석");
        button_5.setBackground(Color.LIGHT_GRAY);
        button_5.setBounds(764140127103);
        StartPage.contentPane.add(button_5);
/////////////////////중략////////////////////////////////
 
        //좌석 버튼 클릭시 이벤트
        button_1.addMouseListener(new MouseAdapter() {// 마우스 클릭 이벤트
            @Override
            public void mousePressed(MouseEvent e) {
                getinfo();
            }
            @Override
            public void mouseEntered(MouseEvent e){
                button_1.setBackground(Color.yellow);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                button_1.setBackground(Color.LIGHT_GRAY);
            }
        });
 
        button_2.addMouseListener(new MouseAdapter() {// 마우스 클릭 이벤트
            @Override
            public void mousePressed(MouseEvent e) {
                getinfo();
            }
            @Override
            public void mouseEntered(MouseEvent e){
                button_2.setBackground(Color.yellow);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                button_2.setBackground(Color.LIGHT_GRAY);
            }
        });
 
        button_3.addMouseListener(new MouseAdapter() {// 마우스 클릭 이벤트
            @Override
            public void mousePressed(MouseEvent e) {
                getinfo();
            }
            @Override
            public void mouseEntered(MouseEvent e){
                button_3.setBackground(Color.yellow);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                button_3.setBackground(Color.LIGHT_GRAY);
            }
        });
 
        button_4.addMouseListener(new MouseAdapter() {// 마우스 클릭 이벤트
            @Override
            public void mousePressed(MouseEvent e) {
                getinfo();
            }
            @Override
            public void mouseEntered(MouseEvent e){
                button_4.setBackground(Color.yellow);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                button_4.setBackground(Color.LIGHT_GRAY);
            }
        });
 
/////////////////////생략////////////////////////////////////
 
            ////좌석 소스 끝////
 
    public void printflight(String FlightNum) {
        System.out.println("항공편 :" + FlightNum);
    }
    
    public void getinfo(){
        GetUserInfo flight = new GetUserInfo();
        int i =0;
        System.out.print("이름 : ");
        Scanner name = new Scanner(System.in);
        flight.name[i]= name.next();
        System.out.print("주민번호앞자리 : ");
        Scanner id = new Scanner(System.in);
        while (!id.hasNextInt()) {//숫자 입력 판별
            id.next();//버퍼를 지움
            System.err.print("에러! 숫자가 아닙니다. \n재 입력 : ");
        }
        flight.idnum[i] = id.nextInt();
        ++i;
        System.out.println("예매 완료");
        
    }
 
 
cs



프로그램 개선 사항 

1. 코드 반복 없애기 : 버튼을 반복문을 이용하여 만들기

2. 예매창 : 예매창을 새로운 화면을 띄워서 만들기

3. 예매 : 파일 입출력을 이용해 정보 저장/주민번호 검증하기


느낀점 : 많이 힘들었다. 거기다 윈도우 빌더를 사용하다보니 원하는 대로 되지 않은 부분이 있어 아쉬웠다. 그래도 GUI 프로그래밍을 해봤는데 재미있었다.


비밀 댓글로 이메일 주소를 남겨주시면 프로젝트 파일을 보내드리겠습니다.