java实现通讯录管理系统
程序员文章站
2022-07-09 13:30:10
本文实例为大家分享了java实现通讯录管理系统的具体代码,供大家参考,具体内容如下完成项目的流程:1.根据需求,确定大体方向 2.功能模块分析 3.界面实现 4.功能模块设计 5.coding 6.代...
本文实例为大家分享了java实现通讯录管理系统的具体代码,供大家参考,具体内容如下
完成项目的流程:
1.根据需求,确定大体方向
2.功能模块分析
3.界面实现
4.功能模块设计
5.coding
6.代码测试
下面是源代码:
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(); //构造函数 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("请输入添加的数据:"); 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("数据添加失败啦"); }else{ system.out.println("数据添加成功啦"); len++;//list集合长度加一 // system.out.println(list.get(0).tostring()); } } // public static void deletefunction(){//删除功能 // system.out.println("输入要删除的联系人的编号"); // 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("输入要删除的联系人的编号"); 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;//一定要加这个,否则当做了删除操作再做添加操作的时候会出现异常(类似于指针,栈) system.out.println("删除完毕"+"此时通讯录记录条数为:" + --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("数据修改完毕"); } } } /* * 查询功能 * */ 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(),否则会数组越界异常 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("成功写入一行数据到 通讯录管理.txt 中"); } writer.close();//关闭写入流,释放资源 } /* * 读取功能 * */ public static void readfunction() throws ioexception{ filereader reader = new filereader("通讯录管理.txt"); bufferedreader br = new bufferedreader(reader); string str; while((str = br.readline()) != null){//每次读取一行文本,判断是否到达文件尾 system.out.println(str); } br.close(); } } public class demo extends jframe { /* * 界面设计 * */ public demo(){ container c = getcontentpane(); //定义一个*容器c jpanel jp = new jpanel(); //新建jpanel面板--jp jbutton button1 = new jbutton("新建联系人"); jbutton button2 = new jbutton("删除联系人"); jbutton button3 = new jbutton("编辑联系人"); jbutton button4 = new jbutton("查找联系人"); jbutton button5 = new jbutton("显示所有联系人"); jbutton button6 = new jbutton("保存联系人到本地"); jbutton button7 = new jbutton("读取本地联系人"); jp.setlayout(new gridlayout(2,4,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距) 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("*通 讯 录 管 理 系 统*"); setvisible(true); setresizable(false);//窗体大小由程序员决定,用户不能*改变大小 setdefaultcloseoperation(windowconstants.exit_on_close); /* *按键响应 * * */ button1.addactionlistener(new actionlistener(){//添加功能实现 public void actionperformed(actionevent arg0){ infro.addfunction(); } }); button2.addactionlistener(new actionlistener(){//删除功能实现 public void actionperformed(actionevent arg0){ infro.deletefunction(); } }); button3.addactionlistener(new actionlistener(){//修改功能实现 public void actionperformed(actionevent arg0){ infro.reditfunction(); } }); button4.addactionlistener(new actionlistener(){//查询功能实现 public void actionperformed(actionevent arg0){ try { infro.searchfunction(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }); button5.addactionlistener(new actionlistener(){//显示功能实现 public void actionperformed(actionevent arg0){ infro.showfunction(); } }); button6.addactionlistener(new actionlistener(){//保存功能实现 public void actionperformed(actionevent arg0){ try { infro.writefunction(); } catch (ioexception e) { e.printstacktrace(); } } }); button7.addactionlistener(new actionlistener(){//读取功能实现 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集合的时候,遇到了一些小问题,这些小问题我在代码里有注释,希望以后不要再犯这种问题。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。