addressbook.java

来自「iCarnegie SSD3 exam3」· Java 代码 · 共 111 行

JAVA
111
字号
import java.util.*;

/**
 * Maintains an address book.
 *
 * @author  author name
 * @version  1.0.0
 * @see AddressBookEntry
 */
public class AddressBook implements Iterable<AddressBookEntry>  {

	/* Collection of {@link AddressBookEntry} objects. */
	private ArrayList<AddressBookEntry>  entries;

	/**
	 * Constructs an empty address book.
	 */
	public AddressBook()  {

		this.entries = new ArrayList<AddressBookEntry>();
	}

	/**
	 * Adds the specified entry to this address book.
	 *
	 * @param entry  the entry to be added.
	 */
	public void  addEntry(AddressBookEntry entry)  {

		this.entries.add(entry);
	}

	/**
	 * Removes the specified entry from this address book.
	 *
	 * @param entry  the entry to be removed.
	 */
	public void  removeEntry(AddressBookEntry entry)  {

		this.entries.remove(entry);
	}

	/**
	 * Removes all the this.entries from this address book.
	 */
	public void  removeAllEntries()  {

		this.entries.clear();
	}

	/**
	 * Returns the first entry with the specified <code>name</code> in this
	 * address book.
	 *
	 * @param name  the name of the entry.
	 * @return  The object in the address book with the specified name.
	 *          Returns <code>null</code> if the object with the
	 *          specified name is not found.
	 */
	public AddressBookEntry  getEntry(String name)  {

		for (AddressBookEntry entry : this.entries) {

			if (entry.getName().equals(name)) {

				return entry;
			}
		}

		return null;
	}

	/**
	 * Returns an array with all the names in this address book.
	 *
	 * @return  an array with all the names in this address book.
	 */
	public String[]  getNames()  {

		String[] result = new String[getNumberOfEntries()];
		int index = 0;

		for (AddressBookEntry entry : this.entries) {
			result[index++] = entry.getName();
		}

		return result;
	}

	/**
	 * Returns an iterator over the this.entries in this address book.
	 *
	 * return  an {@link Iterator<AddressBookEntry>}
	 */
	public Iterator<AddressBookEntry>  iterator() {

		return this.entries.iterator();
	}

	/**
	 * Returns the number of this.entries in this address book.
	 *
	 * @return  the number of this.entries in this address book.
	 */
	public int  getNumberOfEntries()  {

		return this.entries.size();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?