java通讯录存储源代码 java编写通讯录代码
JAVA通讯录 求一个JAVA编写的通讯录,基本的就可以。
具体方法如下:
创新互联公司是一家专业提供侯马企业网站建设,专注与网站设计、成都网站制作、成都h5网站建设、小程序制作等业务。10年已为侯马众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
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 = 0; i size; i++) {
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 = 0; i size i contacts.length;
i++) {
if (contacts[i].cellPhone.equals(cellphone)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该记录不存在!");
} else {
for (int i = index; i size; i++) {
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 = 0; i 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);
}
}
java 电子通讯录的载入和保存代码
思路如下:
确定电子通讯录的文字格式,比如纯文本文件,每条通讯记录一行,每项内容之间使用tab键间隔
使用最基础的IO读写,操作此文件。
载入时从文件一行行读入,使用split拆分各项内容,存入一个对应的通讯录对象中,所有的通讯录对象保存在一个List中;
保存时,以覆盖的方式写入文件,以相同的格式写入即可。
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 = content + string;
}
} 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; i files.length - 1; i++) {
path = path + files[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 = content + name + "" + 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; i imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
java小型通讯录源代码
package src;
public class TelBook {
// 姓名
String name;
// 家庭电话
Integer homePhone;
// 个人移动电话
Integer personalMobilePhone;
// 办公电话
Integer officePhone;
// 家庭地址
String homeAddress;
// 办公地址
String officeAddress;
// QQ号码
Integer qqNumber;
// MSN号码
String msn;
// 邮件
String email;
// 备注
String notes;
String getEmail() {
return email;
}
void setEmail(String email) {
this.email = email;
}
String getHomeAddress() {
return homeAddress;
}
void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
Integer getHomePhone() {
return homePhone;
}
void setHomePhone(Integer homePhone) {
this.homePhone = homePhone;
}
String getMsn() {
return msn;
}
void setMsn(String msn) {
this.msn = msn;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
String getNotes() {
return notes;
}
void setNotes(String notes) {
this.notes = notes;
}
String getOfficeAddress() {
return officeAddress;
}
void setOfficeAddress(String officeAddress) {
this.officeAddress = officeAddress;
}
Integer getOfficePhone() {
return officePhone;
}
void setOfficePhone(Integer officePhone) {
this.officePhone = officePhone;
}
Integer getPersonalMobilePhone() {
return personalMobilePhone;
}
void setPersonalMobilePhone(Integer personalMobilePhone) {
this.personalMobilePhone = personalMobilePhone;
}
Integer getQqNumber() {
return qqNumber;
}
void setQqNumber(Integer qqNumber) {
this.qqNumber = qqNumber;
}
public TelBook() {
}
public TelBook(String name, Integer personalMobilePhone) {
this.setName(name);
this.setPersonalMobilePhone(personalMobilePhone);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TelBook myfriend = new TelBook("张三", new Integer("13800138000"));
}
}
网站题目:java通讯录存储源代码 java编写通讯录代码
地址分享:http://scjbc.cn/article/doooheh.html