java通讯录
㈠ java:19.使用集合类工具实现一个名片通信录。完成一个具有图形界面的、功能比较完善的名片通信录。
因为Vector已经实现了序列化,所以可以直接用对象流读写,写了一个小例子 供参考
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.util.Vector;
publicclassoutStream{
staticVector<String>v=newVector<String>();
staticFilefdata=newFile("data");
publicstaticvoidmain(String[]args){
booleanflag=false;
if(flag){
for(inti=0;i<10;i++){
v.add("name"+i);
}
saveVector();
}
if(!flag){
readVector();
}
}
privatestaticvoidsaveVector(){
try{
ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream(fdata));
oos.writeObject(v);
oos.close();
}catch(Exceptione){
e.printStackTrace();
}
}
privatestaticvoidreadVector(){
try{
ObjectInputStreamois=newObjectInputStream(newFileInputStream(fdata));
v=(Vector<String>)ois.readObject();
ois.close();
for(Stringe:v){
System.out.println(e);
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
㈡ 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
Friend类:public class Friend {
/*
* 姓名
*/
private String name;
/*
* 电耐稿话
*/
private String telephone;
/*
* 邮箱
*/
private String email;
/*
* 公司
*/
private String company; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String toString() {
StringBuffer str = new StringBuffer(); str.append("姓名:" + name).append("\n");
str.append("电话:" + telephone).append("\n");
str.append("邮箱昌仿孝:" + email).append("\n");
str.append("公司:" + company).append("\n");
str.append("-----------------------------------------\n");
return str.toString();
}
}AddFriend类:public class AddFriend { /**
* 主方法 程序的入口
*/
public static void main(String[] args) {
List<Friend> friendList = new ArrayList<Friend>();
char isGo = 'Y';
int i = 0;
do {
Friend friend = new Friend();
System.out.println("请输入第" + (i + 1) + "位朋友的姓名:");
InputStreamReader reader = new InputStreamReader(System.in);
String str = "";
try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setName(str); System.out.println("请输入第" + (i + 1) + "位朋友的电话:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str.matches("\\d*") && str.length() == 11) {// 判断用户输入的电话是否符合标准
friend.setTelephone(str);
} else {
System.out.println("电话号码输入有误,请重新输入!");
continue;
} System.out.println("请输入第" + (i + 1) + "位大孙朋友的邮箱:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setEmail(str); System.out.println("请输入第" + (i + 1) + "位朋友的公司:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setCompany(str); friendList.add(friend); i++; System.out.println("是否继续添加?(Y/N):");
String go = "";
try {
go = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
isGo = go.charAt(0);
} while (isGo == 'Y' || isGo == 'y'); for (int j = 0; j < friendList.size(); j++) {
System.out.println(friendList.get(j).toString());
}
}
}