一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - java實(shí)現(xiàn)通訊錄管理系統(tǒng)

java實(shí)現(xiàn)通訊錄管理系統(tǒng)

2020-07-25 00:14PayneWoo JAVA教程

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

java實(shí)現(xiàn)通訊錄管理系統(tǒng)

完成項(xiàng)目的流程:

1.根據(jù)需求,確定大體方向
2.功能模塊分析
3.界面實(shí)現(xiàn)
4.功能模塊設(shè)計(jì)
5.coding
6.代碼測試

下面是源代碼:

?
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.text.html.HTMLDocument.Iterator;
 
 
class Infro{
 public String id;
 public String name;
 public String sex;
 public String address;
 public String e_mail;
 public String phoneNumber;
 static int index = 0;
 static ArrayList<Infro> list = new ArrayList();
 static int len = list.size();
 //構(gòu)造函數(shù)
 public Infro(String id,String name,String sex,String address,String e_mail,String phoneNumber){
  this.id = id;
  this.name = name;
  this.sex = sex;
  this.address = address;
  this.e_mail = e_mail;
  this.phoneNumber = phoneNumber;
 }
 public String toString(){
  return "編號:"+id+" 姓名:"+name+" 性別:"+sex+" 通訊地址:"+address+" 郵箱地址:"+e_mail+" 電話:"+phoneNumber;
 }
 
 /*
       * 添加功能
  * */
 public static void addFunction(){//添加功能
  Infro infro = new Infro("","","","","","");
  System.out.println("請輸入添加的數(shù)據(jù):");
  Scanner in = new Scanner(System.in);
  System.out.println("輸入編號:");
  infro.id = in.next();
  System.out.println("輸入姓名:");
  infro.name = in.next();
  System.out.println("輸入性別:");
  infro.sex = in.next();
  System.out.println("輸入通訊地址:");
  infro.address = in.next();
  System.
  out.println("輸入郵箱地址:");
  infro.e_mail = in.next();
  System.out.println("輸入電話:");
  infro.phoneNumber = in.next();
  list.add(index,infro);
  index++;
  if(list.isEmpty()){
   System.out.println("數(shù)據(jù)添加失敗啦");
  }else{
   System.out.println("數(shù)據(jù)添加成功啦");
   len++;//list集合長度加一
//   System.out.println(list.get(0).toString());
  }
 
 }
// public static void deleteFunction(){//刪除功能
//  System.out.println("輸入要刪除的聯(lián)系人的編號");
//  Scanner in_2 = new Scanner(System.in);
//  String d1 = in_2.nextLine();
//  for(int a= 0; a<len;a++){
//   if(d1.equals(list.get(a).id)){
//    list.remove(list.get(a));
//    len --; 
//   }
//  }
// }
 /*
       * 刪除功能
  * */
  public static void deleteFunction(){
   System.out.println("輸入要刪除的聯(lián)系人的編號");
   Scanner in_2 = new Scanner(System.in);
   String d1 = in_2.nextLine();
   java.util.Iterator<Infro> it = list.iterator();
   while (it.hasNext()){
    Infro infro = it.next();
    if (infro.id.equals(d1)){
     it.remove();
     --index;//一定要加這個,否則當(dāng)做了刪除操作再做添加操作的時候會出現(xiàn)異常(類似于指針,棧)
     System.out.println("刪除完畢"+"此時通訊錄記錄條數(shù)為:" + --len);
    }
   }
  }
  /*
  *      修改功能
  * */
  public static void reditFunction(){
   System.out.println("輸入要修改的通訊錄的Id");
   Scanner in_r = new Scanner(System.in);
   String r1 = in_r.nextLine();
   for(int a = 0; a < len;a++){
    if(r1.equals(list.get(a).id)){
     System.out.println("輸入修改后的姓名:");
     String name_1 = in_r.next();
     list.get(a).name = name_1;
     System.out.println("輸入修改后的性別:");
     String sex_1 = in_r.next();
     list.get(a).sex = sex_1;
     System.out.println("輸入修改后的通訊地址:");
     String address_1 = in_r.next();
     list.get(a).address = address_1;
     System.out.println("輸入修改后的郵箱地址:");
     String e_mail_1 = in_r.next();
     list.get(a).e_mail = e_mail_1;
     System.out.println("輸入修改后的電話:");
     String phoneNumber_1 = in_r.next();
     list.get(a).phoneNumber = phoneNumber_1;
     System.out.println("數(shù)據(jù)修改完畢");
    }
   }
  }
  /*
        * 查詢功能
  * */
 public static void searchFunction() throws Exception{//查詢功能
  System.out.println("請輸入要查詢的姓名:");
  Scanner in_1 = new Scanner(System.in);
  String s1=in_1.nextLine();
  for(int a= 0; a<len;a++){//切記,,這里不能用a<=list.seze(),否則會數(shù)組越界異常
   if(s1.equals(list.get(a).name)){
    System.out.println(list.get(a).toString());
   }
  }
 }
 
 /*
       * 顯示功能
  * */
 public static void showFunction(){
  for(int i = 0 ;i<len;i++){
   System.out.println(list.get(i).toString());
  }
 }
 /*
  *      保存功能
  * */
 public static void writeFunction() throws IOException{
  FileWriter writer = new FileWriter("通訊錄管理.txt");
  for(int i = 0 ;i<len;i++){
   String []strwriter = new String[len];
   strwriter[i]=list.get(i).toString();
   writer.write(strwriter[i]);
   writer.write("\r\n");
   System.out.println("成功寫入一行數(shù)據(jù)到 通訊錄管理.txt 中");
  }
  writer.close();//關(guān)閉寫入流,釋放資源
 }
 /*
  *      讀取功能
  * */
 public static void readFunction() throws IOException{
  FileReader reader = new FileReader("通訊錄管理.txt");
  BufferedReader br = new BufferedReader(reader);
  String str;
  while((str = br.readLine()) != null){//每次讀取一行文本,判斷是否到達(dá)文件尾
   System.out.println(str);
  }
  br.close();
 }
}
 
 
public class Demo extends JFrame {
 
 /*
  * 界面設(shè)計(jì)
  * */
 public Demo(){
  Container c = getContentPane(); //定義一個頂級容器c
  JPanel jp = new JPanel(); //新建JPanel面板--jp
  JButton button1 = new JButton("新建聯(lián)系人");
  JButton button2 = new JButton("刪除聯(lián)系人");
  JButton button3 = new JButton("編輯聯(lián)系人");
  JButton button4 = new JButton("查找聯(lián)系人");
  JButton button5 = new JButton("顯示所有聯(lián)系人");
  JButton button6 = new JButton("保存聯(lián)系人到本地");
  JButton button7 = new JButton("讀取本地聯(lián)系人");
  jp.setLayout(new GridLayout(2,4,5,5));//新建網(wǎng)格布局管理器(行數(shù),列數(shù),組件間的水平垂直間距)
  jp.add(button1);
  jp.add(button2);
  jp.add(button3);
  jp.add(button4);
  jp.add(button5);
  jp.add(button6);
  jp.add(button7);
  c.add(jp);//將JPanel面板jp添加到頂級容器c中
  setSize(600,500);
  setTitle("*通 訊 錄 管 理 系 統(tǒng)*");
  setVisible(true);
  setResizable(false);//窗體大小由程序員決定,用戶不能自由改變大小
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
 
  /*
   *按鍵響應(yīng)
   *
   * */
  button1.addActionListener(new ActionListener(){//添加功能實(shí)現(xiàn)
   public void actionPerformed(ActionEvent arg0){
    Infro.addFunction();
   }
  });
  button2.addActionListener(new ActionListener(){//刪除功能實(shí)現(xiàn)
   public void actionPerformed(ActionEvent arg0){
    Infro.deleteFunction();
   }
  });
  button3.addActionListener(new ActionListener(){//修改功能實(shí)現(xiàn)
   public void actionPerformed(ActionEvent arg0){
    Infro.reditFunction();
   }
  });
  button4.addActionListener(new ActionListener(){//查詢功能實(shí)現(xiàn)
   public void actionPerformed(ActionEvent arg0){
    try {
     Infro.searchFunction();
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  });
  button5.addActionListener(new ActionListener(){//顯示功能實(shí)現(xiàn)
   public void actionPerformed(ActionEvent arg0){
    Infro.showFunction();
   }
  });
  button6.addActionListener(new ActionListener(){//保存功能實(shí)現(xiàn)
   public void actionPerformed(ActionEvent arg0){
    try {
     Infro.writeFunction();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  });
  button7.addActionListener(new ActionListener(){//讀取功能實(shí)現(xiàn)
   public void actionPerformed(ActionEvent arg0){
    try {
     Infro.readFunction();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  });
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new Demo();
  Infro a = new Infro("", "", "", "", "", "");
 }
 
}

ps:在用List集合的時候,遇到了一些小問題,這些小問題我在代碼里有注釋,希望以后不要再犯這種問題。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/wu2304211/article/details/51768436

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91制片厂免费观看 | 97在线资源站 | 非洲黑人女bbwxxxx | 男人的视频网站 | h片在线看 | 亚洲久草在线 | 香港日本三级亚洲三级 | 顶级欧美做受xxx000 | 亚洲天堂日韩在线 | 边摸边吃奶边做爽gif动态图 | 男女男精品视频网站 | 操穴片| 午夜国产视频 | 亚洲精品www久久久久久 | 大胆国模一区二区三区伊人 | 国产精品最新资源网 | 欧美一区二区视频 | 欧美精品国产第一区二区 | 久久偷拍免费2017 | 国产在线视频福利 | 性欧美xxxxxxx另类 | 国产日韩免费视频 | 香蕉免费一区二区三区在线观看 | 十大看黄网站 | 亚洲电影成人 成人影院 | 色综合伊人色综合网站中国 | 天堂在线中文无弹窗全文阅读 | 午夜国产在线 | 久久精品国产久精国产果冻传媒 | 国产日韩一区二区三区 | 日本成人黄色片 | 免费看全黄特黄毛片 | 91肥熟国产老肥熟在线 | 国产原创精品 | 午夜亚洲国产 | 日本嫩交 | 东京干福利 | 国内视频一区二区 | 国内精品免费一区二区三区 | 日日网 | 男女激情网 |