package CustomProject;
import java.util.Arrays;
// Simple principle about Adress Book;
public class AdressBook {
public static void main(String[] args) {
AdressBookManager abm = new AdressBookManager(5);
// 添加
abm.add(new PersonInfo("Oliver", 12));
abm.add(new PersonInfo("James", 13));
abm.add(new PersonInfo("John", 12));
abm.add(new PersonInfo("BiBox", 13));
abm.add(new PersonInfo("Bobir", 12));
abm.add(new PersonInfo("ABaby", 116));
int len = abm.arrayLength();
System.out.println("通讯录的长度为: " + len);
System.out.println("********************打印所有**************************");
// 打印所有
abm.printAll();
System.out.println("**********************查找****************************");
// 查找
PersonInfo p = abm.find("BiBox");
p.printInfo();
System.out.println("**********************更新****************************");
// 更新
abm.update("Bobir", "Irez", 56);
abm.printAll();
System.out.println("**********************删除****************************");
// 删除
abm.delete("James");
abm.printAll();
}
}
// 管理类
class AdressBookManager{
private PersonInfo[] person = null;
int count = 0;
// 显式保留构造函数
public AdressBookManager() {
}
// 构造函数
public AdressBookManager(int size) {
if (size > 0) {
this.person = new PersonInfo[size];
}else {
this.person = new PersonInfo[10];
}
}
// 判断数组长度
public int arrayLength() {
return person.length;
}
// 通讯录添加人员
public void add(PersonInfo per) {
if (count >= this.person.length) {
int newLength = this.person.length * 3 / 2 + 1;
this.person = Arrays.copyOf(this.person, newLength);
}
this.person[count] = per;
count++;
}
// 删除
public void delete(String name) {
for (int i = 0; i < this.person.length; i++) {
if (this.person[i].getName().equals(name)) {
for (int j = i; j < this.person.length - 1; j++) {
this.person[i] = this.person[i + 1];
}
this.person[this.person.length - 1] = null;
count --;
break;
}
}
}
// 通讯了查找人员
public PersonInfo find(String name) {
for (int i = 0; i < count; i++) {
if (this.person[i].getName().equals(name)) {
return this.person[i];
}
}
return null;
}
// 更新通讯了信息
public void update(String name, String reName, long phone) {
for (int i = 0; i < count; i++) {
if (this.person[i].getName().equals(name)) {
this.person[i].setName(reName);
this.person[i].setPhone(phone);
}
}
}
// 打印所有人的信息
public void printAll() {
for (int i = 0; i < this.count; i++) {
this.person[i].printInfo();
}
}
}
// 个人信息类
class PersonInfo{
private String name;
private long phone;
public PersonInfo() {
}
public PersonInfo(String name, long phone) {
this.name = name;
this.phone = phone;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setPhone(long phone) {
this.phone = phone;
}
public long getPhone() {
return this.phone;
}
public void printInfo() {
System.out.println("姓名: " + this.name + "\t" + "电话: " + this.phone);
}
}