个人通讯录(二)
需求:
通讯录(phone)用来保存若干个联系人的信息,且可以按照联系人姓名的拼音升序排序。要求通讯录提供一下功能:
1.添加联系人信息
2.删除指定联系人信息
3.修改指定联系人信息
4.查找联系人信息
6.清空联系人信息
一、重构类Contract
1.添加方法实现按照联系人姓名进行比较
Collartor类用来执行区分语言环境的字符串比较。通过Collator类提供静态方法getInstance()即可获得“中国”语言环境对应的Collator对象。
public int compareTo(Contract o) {
Collator instance = Collator.getInstance(Locale.CHINA);
return instance.compare(this.getName(),o.getName());
}
2.合并统一联系人的不同信息
对同一联系人的电话号码进行合并是需要进行去重处理。
// 2.合并统一联系人的不同信息
public void mergeContract(Contract o) {
if (this.getName().equals(o.getName())) {
if (this.getGender().equals("") )
this.setGender(o.getGender());
if (this.getEmail().equals(""))
this.setEmail(o.getEmail());
//复制并去重
boolean flag;
String[] newPhones = new String[o.phones.length];
int count = 0;
for (int i = 0; i < o.getPhones().length; i++) {
flag = true;
for (int j = 0; j < o.getPhones()[i].length(); j++) {
if (o.getPhones()[i].equals(this.getPhones()[j])) {
flag = false;
break;
}
}
if (flag)
newPhones[count++] = o.getPhones()[i];
}
int position = phones.length;
phones = Arrays.copyOf(phones,phones.length+ count);//数组扩容
System.arraycopy(newPhones,0,phones,position,count);//追加元素
}
}
3.定义方法实现联系人信息修改
// 3.定义方法实现联系人信息修改
public boolean update(Contract c) {
if (getName().equals(c.getName())) {
if (c.getEmail() != null && c.getEmail() != "")
setEmail(c.getEmail());
if (c.getGender() != null && c.getGender() != "")
setGender(c.getGender());
if (c.getPhones() != null && c.getPhones().length != "")
setPhones(c.getPhones());
return true;
}else
return false;
}
二、重构类Family
重载类父类的update方法,实现Family实例对象的修改
@Override
public boolean update(Contract c) {
if (getName().equals(c.getName())){
if (c instanceof Family) {
super.update(c);
Family f = (Family) c; //向上转型
if (f.getBirthday() != null)
setBirthday(((Family) c).getBirthday());
if (f.getAdress() != null)
setAdress(((Family) c).getAdress());
return true;
}else if (c instanceof Partner)
return false;
else {
super.update(c);
return true;
}
} else
return false;
}
三、重构类Partner
重载类父类的update方法,实现Family实例对象的修改
@Override
public boolean update(Contract c) {
if (getName().equals(c.getName())) {
if (c instanceof Family) {
return false;
} else if (c instanceof Partner) {
super.update(c);
Partner p = (Partner) c;
if (p.getTitle() != null && p.getTitle() != "")
setTitle(p.getTitle());
if (p.getCompany() != null)
getCompany().update(p.getCompany());
return true;
}
else {
super.update(c);
return true;
}
}
else {
return false;
}
}
####四、重构类Company
定义方法update(Company c)方法,实现对公司信息的修改
// 定义方法update(Company c)方法,实现对公司信息的修改
public boolean update(Company c) {
if(c == null)
return false;
if(c.getName() != null && c.getName() != "")
setName(c.getName());
if (c.getAdress() != null && c.getAdress() != "")
setAdress(c.getAdress());
if (c.getPhone() != null && c.getPhone() != "")
setPhone(c.getPhone());
if (c.getFax() != null && c.getFax() != "")
setFax(c.getFax());
return true;
}
五、定义通讯录 PhoneBook类
通讯录 PhoneBook中保存了若干联系人信息,需要提供通讯录的增、删、查、改等操作
1.封装联系人信息
定义Contract数组
private Contract[] contracts;
2.定义setter方法
在setContract()方法中需要对联系人数组进行排序操作,以保证PhoneBook类按照联系人姓名的拼音升序排序
package address_book01;
public class PhoneBook {
// 定义Contract数组
private Contract[] contracts;
// 在setContract()方法中需要对联系人数组进行排序操作,以保证PhoneBook类按照联系人姓名的拼音升序排序
public void setContracts(Contract[] contracts) {
this.contracts = contracts;
//对联系人进行排序
for (int i = 0; i < contracts.length -1 ; i++) {
Contract temp;
for (int j = i + 1; j < contracts.length- 1; j++) {
//冒泡排序
if (contracts[i].compareTo(contracts[j]) > 0){
temp = contracts[i];
contracts[i] = contracts[j];
contracts[j] = temp;
}
}
}
}
}
3.添加联系人
3种情况
空通讯录:分配数组空间,添加新的联系人
添加通讯录中不存在的联系人,对数组进行扩容,添加新的联系人
添加通讯录中存在的联系人,对同一人进行合并操作
public void add(Contract c) {
if (contracts == null) { //空通讯录
contracts = new Contract[1];
Contract[0] = c;
return;
}
int index = findContrat(c);
if (index < 0) {
Contract[] contractsAdded = Arrays.copyOf(contracts,contracts.length + 1);
contractsAdded[contractsAdded.length -1] = c;
setContracts(contractsAdded);
return;
}else
contracts[index].mergeContract(c);//合并操作
}
4.删除联系人
查询联系人,通过移动数组实现元素的删除
// 查询联系人,通过移动数组实现元素的删除
public boolean delete(Contract c){
int index = findContrat(c);
if (index < 0)
return false;
Contract[] contractsDeleted = new Contract[contracts.length-1];
System.arraycopy(contracts,0,contractsDeleted,0,index);
System.arraycopy(contracts,index+1,contractsDeleted,index,contracts.length-1-index);
contracts = contractsDeleted;
return true;
}
5.根据姓名进行模糊查找
利用字符串的contains()方法对字符串进行模糊查询,获得满足条件的所有联系人。
//5.根据姓名进行模糊查找
// 利用字符串的contains()方法对字符串进行模糊查询,获得满足条件的所有联系人。
public Contract[] findContractByName(String name) {
Contract[] result = new Contract[contracts.length];
int num = 0;
for (int i = 0; i < contracts.length; i++) {
if (contracts[i].getName().contains(name)){
result[num++] = contracts[i];
}
}
return Arrays.copyOf(result,num);
}
}
6.修改联系人信息
首先查询待修改的联系人,人后利用多态修改联系人信息
// 6.修改联系人信息
// 首先查询待修改的联系人,人后利用多态修改联系人信息
public boolean updateContract(Contract c) {
int index = findContrat(c);
if (index < 0){
return false;
}
contracts[index].update(c);
return true;
}
7.显示所有联系人
遍历联系人数组,利用多态技术调用display()方法,输出联系人或者子类对象的信息。
public void display() {
for (int i = 0; i < contracts.length; i++) {
contracts[i].dispaly();
}
}
}
8.清空所有联系人
//8.清空所有联系人
public void clearContracts() {
contracts = null;
}