전화번호부 관리 프로그램(HashTable사용)
프로젝트 생성 : File -> New -> Java Project Class 생성 : scr 폴더에서 마우스 오른쪽 버튼 클릭 -> New -> Class 생성된 클래스들 PhoneDTO.java 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package kr.ac.jj.ai_phone; public class PhoneDTO { int _id; String name = ""; String hp = ""; int group = 0; public void myprint() { System.out.printf("%d %s %s %d", this._id, this.name, this.hp, this.group); } } |
PhoneUI.java 수정 : UI 생성
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 |
package kr.ac.jj.ai_phone; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class PhoneUI extends JFrame{ JTextField tfId; JTextField tfName; JTextField tfHp; JTextField tfGroup; public PhoneUI() { this.setSize(800, 600); this.setLayout(null); this.tfName = new JTextField(); this.tfName.setBounds(10, 10, 100, 25); this.add(tfName); this.tfHp = new JTextField(); this.tfHp.setBounds(10, 50, 100, 25); this.add(tfHp); this.tfGroup = new JTextField(); this.tfGroup.setBounds(10, 90, 100, 25); this.add(tfGroup); JButton btnSave = new JButton("저장"); btnSave.setBounds(10, 300, 100, 25); this.add(btnSave); JButton btnDelete = new JButton("삭제"); btnDelete.setBounds(10, 350, 100, 25); this.add(btnDelete); JButton btnUpdate = new JButton("수정"); btnUpdate.setBounds(10, 400, 100, 25); this.add(btnUpdate); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new PhoneUI(); } } |
PhoneUI.java 수정 : ActionListener 추가
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 |
package kr.ac.jj.ai_phone; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class PhoneUI extends JFrame implements ActionListener{ JTextField tfId; JTextField tfName; JTextField tfHp; JTextField tfGroup; public PhoneUI() { this.setSize(800, 600); this.setLayout(null); this.tfName = new JTextField(); this.tfName.setBounds(10, 10, 100, 25); this.add(tfName); this.tfHp = new JTextField(); this.tfHp.setBounds(10, 50, 100, 25); this.add(tfHp); this.tfGroup = new JTextField(); this.tfGroup.setBounds(10, 90, 100, 25); this.add(tfGroup); JButton btnSave = new JButton("저장"); btnSave.setBounds(10, 300, 100, 25); this.add(btnSave); btnSave.addActionListener(this); JButton btnDelete = new JButton("삭제"); btnDelete.setBounds(10, 350, 100, 25); this.add(btnDelete); btnDelete.addActionListener(this); JButton btnUpdate = new JButton("수정"); btnUpdate.setBounds(10, 400, 100, 25); this.add(btnUpdate); btnUpdate.addActionListener(this); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new PhoneUI(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub PhoneDTO dto; PhoneDAO dao; switch(e.getActionCommand()) { case "저장": dto = new PhoneDTO(); //dto._id = Integer.parseInt( tfId.getText() ); dto.name = tfName.getText(); dto.hp = tfHp.getText(); dto.group = Integer.parseInt( tfGroup.getText()); dto.myprint(); break; case "삭제": System.out.println("삭제 클릭"); break; case "수정": System.out.println("수정 클릭"); break; } } } |
삽입 기능 추가 : PhoneDAO 수정
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 |
package kr.ac.jj.ai_phone; import java.util.Enumeration; import java.util.Hashtable; public class PhoneDAO { Hashtable<Integer, PhoneDTO> hash; public PhoneDAO() {} public PhoneDAO(Hashtable<Integer, PhoneDTO> hash) { this.hash = hash; } public void insert(PhoneDTO dto) { int newid = 10001; Enumeration<Integer> enumer = hash.keys(); while (enumer.hasMoreElements()) { int no = hash.get(enumer.nextElement())._id; if(no >= newid) { newid = no + 1; } } dto._id = newid; hash.put(newid, dto); } public void update(PhoneDTO dto) { PhoneDTO d = this.hash.get(dto._id); d.name = dto.name; d.hp = dto.hp; d.group = dto.group; //.... } public void delete(int _id) { this.hash.remove(_id); } public PhoneDTO getOne(int _id) { return this.hash.get(_id); } public Object[] getTableHeader() { Object [] cols = {"_id", "이름", "전화번호", "그룹"}; return cols; } public Object[][] getListVector(){ Object[][] data = new Object[hash.size()][]; Enumeration<Integer> enumer = hash.keys(); int i=0; while (enumer.hasMoreElements()) { int key = enumer.nextElement(); data[i] = new Object[4]; data[i][0] = hash.get(key)._id + ""; data[i][1] = hash.get(key).name; data[i][2] = hash.get(key).hp; data[i][3] = hash.get(key).group + ""; i++; } return data; } } |
삽입 기능 추가 :… Continue Reading 전화번호부 관리 프로그램(HashTable사용)