Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package phonebook;
- public class PhoneBook
- {
- private Contact[] list;
- private int count;
- private static final int MAX_CONTACT = 40;
- public PhoneBook()
- {
- count = 0;
- list = new Contact[MAX_CONTACT];
- }
- public void addContact(String name, String phone)
- {
- list[count] = new Contact(name, phone);
- count++;
- }
- public void delContact(String name)
- {
- for(int i = 0; i < count; i++)
- {
- if(list[i].getN().equals(name))
- {
- for(int j = i; j < count - 1; j++)
- {
- Contact temp = list[j];
- list[j] = list[j + 1];
- list[j + 1] = temp;
- }
- list[count - 1] = null;
- count--;
- }
- }
- }
- public String getPhone(String name)
- {
- for(int i = 0; i < count; i++)
- {
- if(list[i].equals(name))
- {
- return list[i].getP();
- }
- }
- return null;
- }
- public String[] getAllContactsNames()
- {
- String[] names = new String[count];
- for(int i = 0; i < count; i++)
- {
- names[i] = list[i].getN();
- }
- return names;
- }
- @Override
- public String toString()
- {
- Contact[] a = new Contact[count];
- for(int i = 0; i < a.length; i++){
- a[i] = list[i];
- }
- boolean isFinished=true;
- while(isFinished){
- isFinished = false;
- for(int i = 0; i < count-1; i++)
- {
- if(a[i].getN().compareTo(a[i+1].getN())>0)
- {
- Contact tmp = a[i];
- a[i] = a[i+1];
- a[i+1] = tmp;
- isFinished = true;
- }
- }
- }
- String x = "";
- for(int i = 0; i < count; i++)
- {
- x += a[i].getN()+"\n";
- }
- return x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement