
具体方法如下:
1、定义封装一条记录的实体类
2、根据实际系统容量,定义一个数组
3、完成系统中显示全部记录的逻辑
4、完成系统中添加一条记录的逻辑
5、完成系统中删除一条记录的逻辑
6、完成孙乱系统中修改一条记录的逻辑
7、全部代码:
import java.util.Scanner
class Contact {
String cellPhone
String name
}
public class Main {
private static void menu () {
System.out.println("************** 菜单 ******"
+ "************")
System.out.println(" 1.显示全部通讯录")
System.out.println(" 2.增加一条记录")
System.out.println(" 3.删除一条记录")
System.out.println(" 4.修改一条记录")
System.out.println(" 0.退出")
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in)
Contact[] contacts = new Contact[200]
int size = 0
String cmd = ""
do {
menu()
System.out.print("请输入芦弊你得选择:(0-4)")
cmd = scn.nextLine()
if (cmd.equals("1")) {
if (size == 0)
System.out.println("系统当前无记录!")
else
for (int i = 0i <sizei++) {
System.out.println(contacts[i].name + ":"
+ contacts[i].cellPhone)
}
} else if (cmd.equals("2")) {
System.out.print("请输入手机号:")
String cellphone = scn.nextLine()
System.out.print("请输入姓名:")
String name = scn.nextLine()
Contact contact = new Contact()
contact.cellPhone = cellphone
contact.name = name
if (size <contacts.length) {
contacts[size++] = contact
System.out.println("添加成功!")
} else {
System.out.println("你最多只能添加" +
contacts.length + "条则哗档记录")
}
} else if (cmd.equals("3")) {
System.out.print("请输入要删除的手机号:")
String cellphone = scn.nextLine()
int index = -1
for (int i = 0i <size &&i <contacts.length
i++) {
if (contacts[i].cellPhone.equals(cellphone)) {
index = i
break
}
}
if (index == -1) {
System.out.println("该记录不存在!")
} else {
for (int i = indexi <sizei++) {
contacts[index] = contacts[index + 1]
}
contacts[size - 1] = null
size--
System.out.println("删除成功!")
}
} else if (cmd.equals("4")) {
System.out.print("请输入要修改的手机号:")
String cellphone = scn.nextLine()
int index = -1
for (int i = 0i <size &&i <contacts.length
i++) {
if (contacts[i].cellPhone.equals(cellphone)) {
index = i
break
}
}
if (index == -1) {
System.out.println("该记录不存在!")
} else {
System.out.print("请输入姓名:")
String name = scn.nextLine()
contacts[index].name = name
}
}
} while (!cmd.equals("0"))
System.out.println("退出成功!")
scn.close()
System.exit(0)
}
}
附件:源程序清单 import java.awt.*import java.awt.event.*class Info { public static String name,address,mail,telephoneInfo(){} } public class addressBook extends Frame implements ActionListener { private MenuItem noteAdd=new MenuItem("Add")private MenuItem noteDel=new MenuItem("Del")private MenuItem noteEdit=new MenuItem("Modify")private MenuItem noteExit=new MenuItem("Exit")private List noteList=new List()private String[][] message=new String [1000][4]public Info notes=new Info() public addressBook(){ super("拿段通讯仿败录 向阳消大誉")Menu note=new Menu("Edit")note.add(noteAdd)note.add(noteDel)note.add(noteEdit)note.addSeparator()note.add(noteExit) MenuBar bar=new MenuBar()bar.add(note)setMenuBar(bar)add(noteList) noteAdd.addActionListener(this)noteDel.addActionListener(this)noteEdit.addActionListener(this)noteExit.addActionListener(this)noteList.addActionListener(this)addWindowListener(new WindowCloser())setSize(300,400)show()} public void actionPerformed(ActionEvent e){ if(e.getSource()==noteAdd){ notes.mail=nullnotes.name=nullnotes.address=nullnotes.telephone=nullint index=noteList.getItemCount()EditDialog dlg=new EditDialog(this,"添加通讯录")if(dlg.isOk){ message[index][0]=dlg.textName.getText()message[index][1]=dlg.textAddress.getText()message[index][2]=dlg.textEmail.getText()message[index][3]=dlg.textTelephone.getText()if (message[index][0]!=null){ String s="姓名:"+message[index][0]+";\t地址"+message[index][1]+";\tE-mail"+message[index][2]+";\t电话"+message[index][3]noteList.add(s) } } dlg.dispose()}else if(e.getSource()==noteDel){ ConfirmDialog dlg=new ConfirmDialog(this,"确认删除","确定要删除这一条吗?")if (dlg.close){ noteList.remove(noteList.getSelectedIndex())} dlg.dispose()}else if(e.getSource()==noteEdit||e.getSource()==noteList){ int i=noteList.getSelectedIndex()if(i!=-1){ notes.name=message[i][0]notes.address=message[i][1]notes.mail=message[i][2]notes.telephone=message[i][3]EditDialog dlg=new EditDialog(this,"修改通讯录")if(dlg.isOk){ message[i][0]=dlg.textName.getText()message[i][1]=dlg.textAddress.getText()message[i][2]=dlg.textEmail.getText()message[i][3]=dlg.textTelephone.getText() String s="姓名:"+message[i][0]+";\t地址"+message[i][1]+";\tE-mail"+message[i][2]+";\t电话"+message[i][3]noteList.replaceItem(s,i)} dlg.dispose()} }else if(e.getSource()==noteExit){ System.exit(0)} } private class WindowCloser extends WindowAdapter{ public void WindowClosing(WindowEvent we){ System.exit(0)} } public static void main(String[]args){ addressBook a=new addressBook()} } class EditDialog extends Dialog implements ActionListener { private Button ok=new Button("确定")private Button cancel=new Button("取消") private Label labelName=new Label("姓名",Label.LEFT)private Label labelAddress=new Label("地址",Label.LEFT)private Label labelEmail=new Label("E-mail",Label.LEFT)private Label labelTelephone=new Label("电话",Label.LEFT) TextField textName=new TextField(30)TextField textAddress=new TextField(30)TextField textEmail=new TextField(30)TextField textTelephone=new TextField(30) public boolean isOk=falseprivate Info notes=new Info()public EditDialog(Frame parent,String title){ super(parent,title,true)Panel pName=new Panel()pName.setLayout(new FlowLayout())pName.add(labelName)pName.add(textName) Panel pAddress=new Panel()pName.setLayout(new FlowLayout())pName.add(labelAddress)pName.add(textAddress) Panel pEmail=new Panel()pName.add(labelEmail)pName.add(textEmail) Panel pTelephone=new Panel()pName.add(labelTelephone)pName.add(textTelephone) Panel pInfo=new Panel()pInfo.setLayout(new GridLayout(4,1))pInfo.add(pName)pInfo.add(pAddress)pInfo.add(pEmail)pInfo.add(pTelephone) Panel pButton=new Panel()pButton.setLayout(new FlowLayout())pButton.add(ok)pButton.add(cancel) setLayout(new BorderLayout())add("Center",pInfo)add("South",pButton) textName.setText(notes.name)textAddress.setText(notes.address)textEmail.setText(notes.mail)textTelephone.setText(notes.telephone) ok.addActionListener(this)cancel.addActionListener(this)addWindowListener(new WindowCloser())setResizable(false)pack()show()} public void actionPerformed(ActionEvent ae) { if(ae.getSource()==ok){ isOk=truethis.hide() } if(ae.getSource()==cancel){ isOk=falsethis.hide()} } private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent we) { isOk=falseEditDialog.this.hide()} } } class ConfirmDialog extends Dialog implements ActionListener { private Button okay=new Button("确定")private Button cancel=new Button("取消")private Label label=new Label()public boolean close=false public ConfirmDialog (Frame parent,String title,String question) { super(parent,title,true)label.setText(question)Panel buttons=new Panel()buttons.setLayout(new FlowLayout())buttons.add(okay)buttons.add(cancel)setLayout(new BorderLayout())add("Center",label)add("South",buttons)okay.addActionListener(this)cancel.addActionListener(this)addWindowListener(new WindowCloser())setResizable(false)pack()show()} private class WindowCloser extends WindowAdapter{ public void WindowClosing(WindowEvent we){ ConfirmDialog.this.close=falseConfirmDialog.this.hide()} public void actionPerformed(ActionEvent ae){ close=(ae.getSource()==okay)hide()} }}java编写这个通讯录管理系统java编写这个通讯录管理系统_Java如何实现通讯录管理系统租族裤
咕噜噜在芬兰
原创
关穗握注
3点赞·2305人阅读
Java如何实现通讯录管理系统
发布时间:2020-07-28 09:39:42
来源:亿速云
阅读:65
作者:Leah
这篇文章将为大家详细讲解有关Java如何实现通讯录管理系统,文章内容质量较高,因此小编分享给大家做弊简个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
本文实例为大家分享了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 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= 0a
// 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 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 = 0a <lena++){
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= 0a
if(s1.equals(list.get(a).name)){
System.out.println(list.get(a).toString())
}
}
}
/**
* 显示功能
**/
public static void showFunction(){
for(int i = 0 i
System.out.println(list.get(i).toString())
}
}
/**
* 保存功能
**/
public static void writeFunction() throws IOException{
FileWriter writer = new FileWriter("通讯录管理.txt")
for(int i = 0 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("", "", "", "", "", "")
}
}
关于Java如何实现通讯录管理系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)