Bluezz旅遊筆記本

讚一個! Bluezz粉絲團
  • 計數器
  • 寄給朋友
  • facebook分享
  •  建立:2007-05-18  更新:2007-05-18
 

身分證字號產生器與檢查-java


相關標籤 身分證  

字體:
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class IDMaker extends JFrame{
   private String[] location = {
      "台北市", "台中市", "基隆市", "台南市", "高雄市", "台北縣", "宜蘭縣", "桃園縣",
      "嘉義市", "新竹縣", "苗栗縣", "台中縣", "南投縣", "彰化縣", "新竹市", "雲林縣",
      "嘉義縣", "台南縣", "高雄縣", "屏東縣", "花蓮縣", "台東縣", "金門縣", "澎湖縣",
      "陽明山", "連江縣"
   };
   private int[] locationNumber = {
      10, 11, 12, 13, 14, 15, 16, 17,
      34, 18, 19, 20, 21, 22, 35, 23,
      24, 25, 26, 27, 28, 29, 32, 30,
      31, 33
   };
   private char[] locationChar = {
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
      'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
      'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
      'Y', 'Z'
   };
   private JComboBox c = new JComboBox(location);
   private JComboBox s = new JComboBox(new String[]{"男", "女"});
   private JButton make = new JButton("製造");
   private JButton checkID = new JButton("確認");
   private JTextField tf = new JTextField(10);

   private ActionListener makeAction = new ActionListener(){
      public void actionPerformed(ActionEvent e){
         StringBuffer output = new StringBuffer();
         int[] number = new int[9];
         int locationIndex = c.getSelectedIndex();
         output.append(locationChar[locationIndex]);
         number[0] = (locationNumber[locationIndex] % 10) * 9 +
            (int)(locationNumber[locationIndex] / 10);

         output.append(number[1] = s.getSelectedIndex() + 1);

         Random r = new Random();
         for(int i = 2; i < number.length; i++){
            output.append(number[i] = r.nextInt(10));
         }

         int sum = number[0];
         for(int i = number.length - 1, j = 1; i > 0 ; i--, j++){
            sum += number[i] * j;
         }
         int check = (sum % 10) == 0 ? 0 : 10 - (sum % 10);
         output.append(check);
         tf.setText(output.toString());
      }
   };

   private ActionListener checkAction = new ActionListener(){
      public void actionPerformed(ActionEvent e){
         String s = tf.getText().trim();
         if(s.equals("")) return;

         if(checkID(s)){
            JOptionPane.showMessageDialog(null, "正確", "身分證字號確認",
               JOptionPane.INFORMATION_MESSAGE);
         }
         else{
            JOptionPane.showMessageDialog(null, "錯誤", "身分證字號確認",
               JOptionPane.ERROR_MESSAGE);
         }
      }
   };

   public IDMaker(){
      JPanel p1 = new JPanel();
      p1.add(c);
      p1.add(s);
      getContentPane().add(p1, BorderLayout.NORTH);

      JPanel p2 = new JPanel();
      make.addActionListener(makeAction);
      p2.add(make);
      checkID.addActionListener(checkAction);
      p2.add(checkID);
      getContentPane().add(p2, BorderLayout.CENTER);
      getContentPane().add(tf, BorderLayout.SOUTH);
   }

   public boolean checkID(String Chk2){
      String v[][] = {{"A", "台北市"}, {"B", "台中市"}, {"C", "基隆市"},
         {"D", "台南市"}, {"E", "高雄市"}, {"F", "台北縣"}, {"G", "宜蘭縣"},
         {"H", "桃園縣"}, {"J", "新竹縣"}, {"K", "苗栗縣"}, {"L", "台中縣"},
         {"M", "南投縣"}, {"N", "彰化縣"}, {"P", "雲林縣"}, {"Q", "嘉義縣"},
         {"R", "台南縣"}, {"S", "高雄縣"}, {"T", "屏東縣"}, {"U", "花蓮縣"},
         {"V", "台東縣"}, {"X", "澎湖縣"}, {"Y", "陽明山"}, {"W", "金門縣"},
         {"Z", "連江縣"}, {"I", "嘉義市"}, {"O", "新竹市"}
      };

      int inte = -1;
      String s1 = String.valueOf(Character.toUpperCase(Chk2.charAt(0)));
      for(int i = 0; i < 26; i++){
         if(s1.compareTo(v[i][0]) == 0){
            inte = i;
         }
      }
      int total = 0;
      int all[] = new int[11];
      String E = String.valueOf(inte + 10);
      int E1 = Integer.parseInt(String.valueOf(E.charAt(0)));
      int E2 = Integer.parseInt(String.valueOf(E.charAt(1)));
      all[0] = E1;
      all[1] = E2;
      try{
         for(int j = 2; j <= 10; j++)
            all[j] = Integer.parseInt(String.valueOf(Chk2.charAt(j - 1)));
         for(int k = 1; k <= 9; k++)
            total += all[k] * (10 - k);
         total += all[0] + all[10];
         if(total % 10 == 0)
            return true;
      }
      catch(Exception ee){;
      }
      return false;
   }

   public static void main(String[] args){
      IDMaker app = new IDMaker();
      app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      app.pack();
      app.setVisible(true);
   }
}
參考資料:
轉載網友swanky文章 http://www.javaworld.com.tw/jute/post/view?bid=35&id=33999&sty=3
本頁網址:https://bluezz.tw/c.php?id=802



讚一個! Bluezz粉絲團

留言討論區


↑4516樓
 標 題  Re:身分證字號產生器與檢查-java
 暱 稱
 大大
時間:2009-03-07 20:57:30 來自:125.224.188.xxx
此則為私密留言

↑3166樓
 標 題  Re:身分證字號產生器與檢查-java
 暱 稱
 畫畫
時間:2008-11-27 17:27:46 來自:118.167.224.xxx

↑1907樓
 標 題  Re:身分證字號產生器與檢查-java
 暱 稱
 小宇
時間:2008-08-14 17:30:02 來自:61.225.15.xxx
我想知道身分證產生器在哪?

↑1456樓
 標 題  Re:身分證字號產生器與檢查-java
 暱 稱
 囉邋闢溝
時間:2008-07-15 21:52:29 來自:125.232.102.xxx
此則為私密留言

↑1388樓
 標 題  Re:身分證字號產生器與檢查-java
 暱 稱
 阿克
時間:2008-07-08 11:42:06 來自:123.195.79.xxx
此則為私密留言

觀看更多留言

標 題   ※必填
暱 稱  ※必填
E-Mail  
評 價  非常好普通非常差
內 容  
 確認碼  
 更換一組認證碼 ※必填不分大小寫