A simple demo for java about Adress Book

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值