본문 바로가기

Study/Programming

AWT 주민등록번호 검사

반응형
import java.awt.Frame;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.GridBagConstraints;
import java.awt.Label;
import java.awt.FlowLayout;
import java.awt.Button;

public class JuminCheck extends Frame {

    private static final long serialVersionUID = 1L;
    private Panel panel = null;
    private TextArea textArea = null;
    private TextField jumin1 = null;
    private Label label = null;
    private TextField jumin2 = null;
    private Button button = null;
  
    private Panel getPanel() {
        if (panel == null) {
            FlowLayout flowLayout = new FlowLayout();
            flowLayout.setAlignment(FlowLayout.LEFT);
            label = new Label();
            label.setText("~");
            label.setAlignment(Label.CENTER);
            panel = new Panel();
            panel.setLayout(flowLayout);
            panel.add(getJumin1(), null);
            panel.add(label, null);
            panel.add(getJumin2(), null);
            panel.add(getButton(), null);
        }
        return panel;
    }

    private TextArea getTextArea() {
        if (textArea == null) {
            textArea = new TextArea();
        }
        return textArea;
    }

    private TextField getJumin1() {
        if (jumin1 == null) {
            jumin1 = new TextField();
            jumin1.setColumns(6);
        }
        return jumin1;
    }

    private TextField getJumin2() {
        if (jumin2 == null) {
            jumin2 = new TextField();
            jumin2.setColumns(7);
            jumin2.setSelectionEnd(0);
            jumin2.setEchoChar('*');
        }
        return jumin2;
    }

    private Button getButton() {
        if (button == null) {
            button = new Button();
            button.setLabel("검 사 하 기");
            button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent e) {                  
                    //System.out.println("첫번째 자리 : " + jumin1.getText());
                    //System.out.println("두번째 자리 : " + jumin2.getText());
                    if(jumin1.getText().length() != 6 || jumin2.getText().length() !=7){
                        textArea.append("주민등록번호를 정확히 입력해 주세요" +"\r\n");
                        return;
                    }
                    String juminSum = jumin1.getText() + jumin2.getText();
                    //char[] ch = new char[juminSum.length()];
                    char[] ch = new char[13];
                    int sum=0;
                    for(int j=0; j<juminSum.length()-1; j++){
                        ch[j] = juminSum.charAt(j);                                            
                        if(j<=7)
                            sum += ((int)ch[j]-48) * (j+2);
                        else if(j>=8 && j<=11)
                            sum += ((int)ch[j]-48) * (j-6);     
                    }
                        int cal = (11 - (sum % 11)) % 10;
                        int checkbit = Integer.parseInt(jumin2.getText().substring(6,7));                   
                        if(cal == checkbit)
                            textArea.append("맞는 주민번호 입니다." +"\n");
                        else
                            textArea.append("틀린 주민번호 입니다." +"\n");
                  
                    textArea.append("주민등록번호 : " + juminSum +"\n");
                    textArea.append("Checkbit " +checkbit +" Cal : " + cal +" Sum : "+sum +"\n");
                }
            });
            button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent e) {
                    System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
                }
            });
        }
        return button;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JuminCheck jc = new JuminCheck();
        jc.setVisible(true);
    }

    public JuminCheck() {
        super();
        initialize();
    }

    private void initialize() {
        this.setSize(350, 200);
        this.setTitle("Frame");

        this.add(getPanel(), BorderLayout.NORTH);
        this.add(getTextArea(), BorderLayout.CENTER);
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
                e.getWindow().setVisible(false);
                e.getWindow().dispose();
                System.exit(0);
            }
        });
    }

}

import java.awt.Frame;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.GridBagConstraints;
import java.awt.Label;
import java.awt.FlowLayout;
import java.awt.Button;

public class JuminCheck2 extends Frame {

    private static final long serialVersionUID = 1L;
    private Panel panel = null;
    private TextArea textArea = null;
    private TextField jumin1 = null;
    private Label label = null;
    private TextField jumin2 = null;
    private Button button = null;
    /**
     * This method initializes panel   
     *    
     * @return java.awt.Panel   
     */
    private Panel getPanel() {
        if (panel == null) {
            FlowLayout flowLayout = new FlowLayout();
            flowLayout.setAlignment(FlowLayout.LEFT);
            label = new Label();
            label.setText("~");
            label.setAlignment(Label.CENTER);
            panel = new Panel();
            panel.setLayout(flowLayout);
            panel.add(getJumin1(), null);
            panel.add(label, null);
            panel.add(getJumin2(), null);
            panel.add(getButton(), null);
        }
        return panel;
    }

    /**
     * This method initializes textArea   
     *    
     * @return java.awt.TextArea   
     */
    private TextArea getTextArea() {
        if (textArea == null) {
            textArea = new TextArea();
        }
        return textArea;
    }

    /**
     * This method initializes jumin1   
     *    
     * @return java.awt.TextField   
     */
    private TextField getJumin1() {
        if (jumin1 == null) {
            jumin1 = new TextField();
            jumin1.setColumns(10);
        }
        return jumin1;
    }

    /**
     * This method initializes jumin2   
     *    
     * @return java.awt.TextField   
     */
    private TextField getJumin2() {
        if (jumin2 == null) {
            jumin2 = new TextField();
            jumin2.setColumns(10);
            jumin2.setEchoChar('*');
        }
        return jumin2;
    }

    /**
     * This method initializes button   
     *    
     * @return java.awt.Button   
     */
    private Button getButton() {
        if (button == null) {
            button = new Button();
            button.setLabel("검 사 하 기");
            button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent e) {
                    //System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
                   
                    //System.out.println("첫번째 자리 : " + jumin1.getText());
                    //System.out.println("두번째 자리 : " + jumin2.getText());
                   
                    //textArea.append("첫번째 자리 : " + jumin1.getText() + "\n");
                    //textArea.append("두번째 자리 : " + jumin2.getText());
                   
                    textArea.setText("");
                   
                    String jumin = jumin1.getText() + jumin2.getText();
                    int[] checkbit = {2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4};
                    int total = 0;
                    for(int i=0 ; i<checkbit.length ; i++) {
                        total += Integer.parseInt(jumin.substring(i, i+1)) * checkbit[i];
                    }
                    int calbit = (11-(total%11)) % 10;
                    int lastbit = Integer.parseInt(jumin.substring(12, 13));
                   
                    if(calbit == lastbit) {
                        textArea.append("주민등록번호가 정확합니다.\n");
                        textArea.append("출생년도 : " + jumin.substring(0, 2) + "\n");
                        textArea.append("출생월 : " + jumin.substring(2, 4) + "\n");
                        textArea.append("출생일 : " + jumin.substring(4, 6) + "\n");
                        textArea.append("성별 : " + (jumin.substring(6, 7).equals("1")||jumin.substring(6, 7).equals("3") ? "남" : "여") + "\n");
                    } else {
                        textArea.append("주민등록번호가 틀립니다.\n");
                    }
                }
            });
        }
        return button;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JuminCheck2 jc = new JuminCheck2();
        jc.setVisible(true);
    }

    /**
     * This is the default constructor
     */
    public JuminCheck2() {
        super();
        initialize();
    }

    private void initialize() {
        this.setSize(492, 369);
        this.setTitle("Frame");

        this.add(getPanel(), BorderLayout.NORTH);
        this.add(getTextArea(), BorderLayout.CENTER);
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
                e.getWindow().setVisible(false);
                e.getWindow().dispose();
                System.exit(0);
            }
        });
    }

}

반응형

'Study > Programming' 카테고리의 다른 글

자바 Swing  (0) 2009.05.21
AWT choice, list  (0) 2009.05.20
AWT  (0) 2009.05.18
자바 달력  (0) 2009.05.18
자바 컬렉션 프레임워크  (0) 2009.05.18