java通讯录程序代码 java编写简单的通讯录

Java编写一个班级通讯录import java.util.ArrayList;
import java.util.List;
【java通讯录程序代码 java编写简单的通讯录】import java.util.Scanner;
public class AddressList {
static Scanner scanner = new Scanner(System.in);
static ListEntity enlist = new ArrayListEntity();
public static void main(String[] args) {
int input;
AddressList addre = new AddressList();
System.out.println("===============欢迎使用**通讯录===============");
do{
System.out.println("1、插入联系人");
System.out.println("2、删除练习人");
System.out.println("3、修改联系人");
System.out.println("4、查询练习人");
System.out.println("5、查询全部联系人");
System.out.println("0、退出系统");
System.out.println("请选择:");
input = scanner.nextInt();
switch(input){
case 1:
Entity e = new Entity();
e.id = enlist.size();
System.out.println("请输入联系人姓名:");
e.name = scanner.next();
System.out.println("请输入联系方式:");
e.number = scanner.nextInt();
addre.add(e);
System.out.println("插入成功!");
break;
case 2:
System.out.println("请输入要删除联系人编号:");
int num = scanner.nextInt();
if(addre.delete(num))
System.out.println("删除成功!");
else
System.out.println("删除失败 , 请确认信息是否正确!");
break;
case 3:
System.out.println("请输入要修改人编号:");
int unum = scanner.nextInt();
addre.update(unum);
System.out.println("修改完成!");
break;
case 4:
System.out.println("请输入要查询人姓名:");
String name = scanner.next();
Entity ent = addre.select(name);
if(ent!=null){
System.out.println(name "的联系方式为:" ent.number);
}else{
System.out.println("查无此人!");
}
break;
case 5:
for(Entity entit:enlist){
System.out.println(entit.name "的联系方式为:" entit.number);
}
break;
}
}while(input!=0);
System.out.println("谢谢使用!");
}
/*
* 添加联系人
*/
public boolean add(Entity e){
enlist.add(e);
return true;
}
/*
* 删除联系人
*/
public boolean delete(int num){
if(numenlist.size())
return false;
else
enlist.remove(num);
return true;
}
/*
* 修改联系人
*/
public void update(int num){
if(numenlist.size())
System.out.println("查无此人!");
else{
Entity e = new Entity();
e.id = num;
System.out.println("请输入联系人姓名:");
e.name = scanner.next();
System.out.println("请输入联系方式:");
e.number = scanner.nextInt();
enlist.set(num, e);
}
}
/*
* 查询指定联系人电话
*/
public Entity select(String name){
Entity en = null;
for(Entity e : enlist){
if(e.name.equals(name))
en = e;
}
return en;
}
}
class Entity{
public int id;//编号
public String name;//姓名
public int number;//联系电话
}
java通讯录全部代码!import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class AddList {
private String filePath = "";
private String bakPath = "";
private String content = "";
Scanner sc = new Scanner(System.in);
public String readFile(){
content = "";
if (isNull(filePath)) {
System.out.println("文件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileReader fr = null;
try {
if (file.exists()) {
fr = new FileReader(file);
char[] chars = new char[1024];
int n = 0;
while((n = fr.read(chars)) != -1){
String string = new String(chars, 0, n);
content = contentstring;
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
}
public void writeFile(String path){
File file = new File(path);
FileOutputStream fos = null;
mkDirs(path);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void writeFile(){
if (isNull(filePath)) {
System.out.println("文件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileOutputStream fos = null;
mkDirs(filePath);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void mkDirs(String filepath){
if (filepath.indexOf("\\") != -1) {
filepath = filepath.replaceAll("\\", "/");
}
int n = filepath.indexOf("//");
String path = filepath.substring(0, n)"//";
filepath = filepath.substring(filepath.indexOf("//")1, filepath.length());
String[] files = filepath.split("/");
for (int i = 0; ifiles.length - 1; i) {
path = pathfiles[i];
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}
}
public void addImfor(){
System.out.println("--------增加记录---------");
String name = "";
String tel = "";
String email = "";
content = readFile();
while(true){
System.out.println("姓名:");
name = sc.next();
System.out.println("电话:");
tel = sc.next();
System.out.println("Email:");
email = sc.next();
content = contentname""tel""email"==";
System.out.println("0、Exit 1、继续");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
writeFile();
}
public void deleteImfor(){
System.out.println("---------删除记录---------");
String name = "";
String[] imfors = null;
content = readFile();
while(true){
System.out.println("你要删除的姓名是:");
name = sc.next();
if (content.indexOf(name) != -1) {
imfors = content.split("==");
for (int i = 0; iimfors.length; i) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
java通讯录管理系统设计代码怎么编译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= 0; 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 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; alen;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
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如何实现通讯录管理系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识 。如果觉得文章不错,可以把它分享出去让更多的人看到 。
java通讯录程序代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java编写简单的通讯录、java通讯录程序代码的信息别忘了在本站进行查找喔 。

    推荐阅读