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());
}
}
}