[20-06-04][Self-test 46]Java Linker Manage

Posted mirai3usi9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[20-06-04][Self-test 46]Java Linker Manage相关的知识,希望对你有一定的参考价值。

 1 package leason_0603;
 2 
 3 public class Linker {
 4     
 5     private String name;
 6     private String phone;
 7     private int id;
 8     private static int idCode = 100;
 9 
10     public Linker() {
11         this.id = idCode;
12         idCode++;
13     }
14 
15     public Linker(String name, String phone) {
16         this();
17         this.name = name;
18         this.phone = phone;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public String getPhone() {
30         return phone;
31     }
32 
33     public void setPhone(String phone) {
34         this.phone = phone;
35     }
36 
37     public int getId() {
38         return id;
39     }
40 
41     public void setId(int id) {
42         this.id = id;
43     }
44 
45 }

 

 1 package leason_0603;
 2 
 3 public class LinkService {
 4     
 5     private static Linker[] array = new Linker[10];
 6     private static int num;
 7     private int count;
 8     
 9     static {
10         addLinker(new Linker("Joker", "110"));
11         addLinker(new Linker("Navi", "114"));
12         addLinker(new Linker("Skull", "119"));
13         addLinker(new Linker("Violet", "120"));
14         addLinker(new Linker("Violet", "120"));
15     }
16 
17     private static void addLinker(Linker linker) {
18 
19         if (num == array.length) {
20             addArray();
21         }
22 
23         array[num] = linker;
24         num++;
25     }
26 
27     private static void addArray() {
28 
29         Linker[] newArray = new Linker[array.length + 10];
30         System.arraycopy(array, 0, newArray, 0, num);
31         array = newArray;
32     }
33 
34     public Linker[] getArray() {
35         return array;
36     }
37 
38     public int getNum() {
39         return num;
40     }
41 
42     public void add(Linker linker) {
43         addLinker(linker);
44     }
45 
46     public void del(int id) {
47         int index = findById(id);
48         
49         for (int i = index; i < num - 1; i++) {
50             array[i] = array[i + 1];
51         }
52         
53         num--;
54     }
55 
56     public void update(int id, String phone) {
57         int index = findById(id);
58         array[index].setPhone(phone);
59     }
60 
61     public Linker get(int id) {
62         int index = findById(id);
63         return array[index];
64     }
65 
66     public Linker[] findByName(String name) {
67         
68         Linker[] newArray = new Linker[num];
69         count = 0;
70         
71         for (int i = 0 ; i < num; i++) {
72             String n = array[i].getName();
73             
74             if (n.indexOf(name) != -1) {
75                 newArray[count] = array[i];
76                 count++;
77             }
78         }
79         
80         return newArray;
81     }
82 
83     private int findById(int id) {
84         for (int i = 0; i < num; i++) {
85             if (id == array[i].getId()) {
86                 return i;
87             }
88         }
89             
90         return -1;
91     }
92 
93     public int getCount() {
94         return count;
95     }
96 
97 }

 

  1 package leason_0603;
  2 
  3 import java.awt.event.ActionEvent;
  4 import java.awt.event.ActionListener;
  5 
  6 import javax.swing.JFrame;
  7 import javax.swing.JOptionPane;
  8 
  9 import com.lovo.netCRM.component.LovoButton;
 10 import com.lovo.netCRM.component.LovoTable;
 11 import com.lovo.netCRM.component.LovoTitlePanel;
 12 import com.lovo.netCRM.component.LovoTxt;
 13 
 14 public class MainFrame extends JFrame {
 15     
 16     private LovoTable linkerTable = new LovoTable(this, new String[] {"姓名", "电话"}, 
 17             new String[] {"name", "phone"}, "id");
 18     private LinkService service = new LinkService();
 19     private LovoTxt nameTxt;
 20 
 21     public MainFrame() {
 22         setLayout(null);
 23         
 24         init();
 25         
 26         setSize(800, 600);
 27         setVisible(true);
 28         setDefaultCloseOperation(3);
 29         setLocationRelativeTo(null);
 30     }
 31 
 32     private void init() {
 33         
 34         linkerTable.setSizeAndLocation(0, 0, 700, 200);
 35         
 36         initTable();
 37         
 38         LovoButton addButton = new LovoButton("添加", 20, 300, this);
 39         LovoButton delButton = new LovoButton("删除", 140, 300, this);
 40         LovoButton updateButton = new LovoButton("修改", 260, 300, this);
 41         
 42         LovoTitlePanel titlePanel = new LovoTitlePanel("查找联系人", 380, 230, 300, 150, this);
 43         nameTxt = new LovoTxt("姓名", 50, 50, titlePanel);
 44         LovoButton findButton = new LovoButton("查找", 50, 100, titlePanel);
 45         LovoButton refresheButton = new LovoButton("刷新", 180, 100, titlePanel);
 46         
 47         addButton.addActionListener(new ActionListener() {
 48             
 49             @Override
 50             public void actionPerformed(ActionEvent e) {
 51                 add();
 52             }
 53         });
 54         
 55         delButton.addActionListener(new ActionListener() {
 56             
 57             @Override
 58             public void actionPerformed(ActionEvent e) {
 59                 del();
 60             }
 61         });
 62         
 63         updateButton.addActionListener(new ActionListener() {
 64             
 65             @Override
 66             public void actionPerformed(ActionEvent e) {
 67                 update();
 68             }
 69         });
 70         
 71         findButton.addActionListener(new ActionListener() {
 72             
 73             @Override
 74             public void actionPerformed(ActionEvent e) {
 75                 find();
 76             }
 77         });
 78         
 79         refresheButton.addActionListener(new ActionListener() {
 80             
 81             @Override
 82             public void actionPerformed(ActionEvent e) {
 83                 initTable();
 84             }
 85         });
 86         
 87     }
 88 
 89     private void add() {
 90         
 91         new AddFrame(this);
 92         initTable();
 93     }
 94 
 95     private void del() {
 96         
 97         int id = linkerTable.getKeyByInt();
 98         
 99         if (id == -1) {
100             JOptionPane.showMessageDialog(null, "请选择行");
101             return;
102         }
103         
104         service.del(id);
105         initTable();
106     }
107 
108     private void update() {
109         
110         int id = linkerTable.getKeyByInt();
111         
112         if (id == -1) {
113             JOptionPane.showMessageDialog(null, "请选择行");
114             return;
115         }
116         
117         new UpdateFrame(this, id);
118         
119     }
120 
121     private void find() {
122         
123         String name = nameTxt.getText();
124         Linker[] newArray = service.findByName(name);
125         linkerTable.updateLovoTable(newArray, service.getCount());
126         
127     }
128 
129     public void initTable() {
130         
131         linkerTable.updateLovoTable(service.getArray(), service.getNum());
132         
133     }
134 
135     public static void main(String[] args) {
136         new MainFrame();
137     }
138 
139 }

 

 1 package leason_0603;
 2 
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 
 6 import javax.swing.JFrame;
 7 
 8 import com.lovo.netCRM.component.LovoButton;
 9 import com.lovo.netCRM.component.LovoTxt;
10 
11 public class AddFrame extends JFrame {
12     
13     private LovoTxt nameTxt = new LovoTxt("姓名", 50, 50, this);
14     private LovoTxt phoneTxt = new LovoTxt("电话", 50, 100, this);
15     private LinkService service = new LinkService();
16 
17     public AddFrame() {
18         
19     }
20 
21     public AddFrame(MainFrame m) {
22 
23         setLayout(null);
24 
25         init(m);
26 
27         setSize(300, 300);
28         setVisible(true);
29         setDefaultCloseOperation(3);
30         setLocationRelativeTo(null);
31     }
32 
33     private void init(MainFrame m) {
34         
35         LovoButton addButton = new LovoButton("添加", 50, 150, this);
36         LovoButton cancelButton = new LovoButton("取消", 150, 150, this);
37         
38         addButton.addActionListener(new ActionListener() {
39             
40             @Override
41             public void actionPerformed(ActionEvent e) {
42 
43                 service.add(new Linker(nameTxt.getText(), phoneTxt.getText()));
44                 m.initTable();
45                 dispose();
46             }
47         });
48         
49         cancelButton.addActionListener(new ActionListener() {
50             
51             @Override
52             public void actionPerformed(ActionEvent e) {
53                 
54                 dispose();
55             }
56         });
57     }
58 }

 

 1 package leason_0603;
 2 
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 
 6 import javax.swing.JFrame;
 7 import javax.swing.JOptionPane;
 8 
 9 import com.lovo.netCRM.component.LovoButton;
10 import com.lovo.netCRM.component.LovoLabel;
11 import com.lovo.netCRM.component.LovoTxt;
12 
13 public class UpdateFrame extends JFrame {
14     
15     private LovoLabel nameLabel = new LovoLabel("姓名", 50, 50, this);
16     private LovoTxt phoneTxt = new LovoTxt("电话", 50, 100, this);
17     private LinkService service = new LinkService();
18     private int id;
19 
20     public UpdateFrame() {
21         
22     }
23 
24     public UpdateFrame(MainFrame m, int id) {
25         
26         setLayout(null);
27         
28         this.id = id;
29         init(m);
30         
31         setSize(300, 300);
32         setVisible(true);
33         setDefaultCloseOperation(3);
34         setLocationRelativeTo(null);
35     }
36 
37     private void init(MainFrame m) {
38 
39         LovoButton updateButton = new LovoButton("修改", 50, 150, this);
40         LovoButton cancelButton = new LovoButton("取消", 150, 150, this);
41         
42         Linker linker = service.get(id);
43         nameLabel.setText(linker.getName());
44         phoneTxt.setText(linker.getPhone());
45         
46         updateButton.addActionListener(new ActionListener() {
47             
48             @Override
49             public void actionPerformed(ActionEvent e) {
50                 service.update(id, phoneTxt.getText());
51                 JOptionPane.showMessageDialog(null, "修改成功");
52                 dispose();
53                 m.initTable();
54             }
55         });
56         
57         cancelButton.addActionListener(new ActionListener() {
58             
59             @Override
60             public void actionPerformed(ActionEvent e) {
61                 
62                 dispose();
63             }
64         });
65     }
66     
67 }

 

以上是关于[20-06-04][Self-test 46]Java Linker Manage的主要内容,如果未能解决你的问题,请参考以下文章

[20-04-26][Self-test 6]Java CharType

[20-05-09][Self-test 40]Java BankSystem 1

[20-05-09][Self-test 40]Java BankSystem 1

[20-05-02][Self-test 31]Java Dictionary

[20-05-02][Self-test 33]Java Car

[20-05-01][Self-test 27]Java BankAccount