Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Contact.java
- =============
- public class Contact {
- private String firstName;
- private String lastName;
- private String phone;
- public Contact(String firstName, String lastName, String phone) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.phone = phone;
- }
- public Contact(Contact c) {
- this.firstName = c.firstName;
- this.lastName = c.lastName;
- this.phone = c.phone;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- @Override
- public String toString() {
- return "Contact [firstName=" + firstName + ", lastName=" + lastName + ", phone=" + phone + "]";
- }
- }
- PhoneBook.java
- ==============
- public class PhoneBook {
- private Contact[] myContacts;
- private int current;
- public PhoneBook(int size) {
- myContacts = new Contact[size];
- current = 0;
- }
- public boolean addContact(Contact c) {
- if (current < myContacts.length) {
- myContacts[current] = new Contact(c);
- current += 1;
- return true;
- }
- return false;
- }
- public boolean addContact(String fName, String lName, String Phone) {
- if (current < myContacts.length) {
- myContacts[current] = new Contact(fName, lName, Phone);
- current += 1;
- return true;
- }
- return false;
- }
- public void removeContact(String fName, String lName) {
- for (int i = 0; i < current; i += 1) {
- if (myContacts[i].getFirstName().compareTo(fName) == 0
- && myContacts[i].getLastName().compareTo(lName) == 0) {
- for (int j = i + 1; j < current; j += 1) {
- myContacts[j - 1] = myContacts[j];
- }
- // myContacts[current]=null;
- current -= 1;
- }
- }
- }
- public void removeContact(String phone) {
- for (int i = 0; i < current; i += 1) {
- if (myContacts[i].getPhone().compareTo(phone) == 0) {
- for (int j = i + 1; j < current; j += 1) {
- myContacts[j - 1] = myContacts[j];
- }
- // myContacts[current]=null;
- current -= 1;
- }
- }
- }
- public void removeAll() {
- current = 0;
- }
- public Contact findFirstByName(String name) {
- return findNextByName(name, 0);
- }
- public Contact findNextByName(String name, int from) {
- for (int i = from; i < current; i += 1) {
- if (myContacts[i].getFirstName().compareTo(name) == 0 || myContacts[i].getLastName().compareTo(name) == 0) {
- return myContacts[i];
- }
- }
- return null;
- }
- public Contact findByNumber(String phone) {
- for (int i = 0; i < current; i += 1) {
- if (myContacts[i].getPhone().compareTo(phone) == 0) {
- return myContacts[i];
- }
- }
- return null;
- }
- @Override
- public String toString() {
- String res = "Empty Phone Book";
- for (int i = 0; i < current; i += 1) {
- res += myContacts[i] + "\n";
- }
- return res;
- }
- }
- Tester.java
- ===========
- public class Tester {
- public static void main(String[] args) {
- PhoneBook myBook = new PhoneBook(5);
- myBook.addContact("Anan", "Samnia", "123123123");
- myBook.addContact("Siraj", "Sawaed", "123456789");
- myBook.addContact("Lolo", "jojo", "951623847");
- System.out.println("My phone Book\n" + myBook);
- if (!myBook.addContact("Anas", "Sawaed", "111111111"))
- System.out.println("Error adding");
- else
- System.out.println("My phone Book\n" + myBook);
- if (myBook.findFirstByName("yamir") == null)
- System.out.println("Yamir not found");
- else
- System.out.println(myBook.findFirstByName("yamir"));
- if (myBook.findFirstByName("Lolo") == null)
- System.out.println("Lolo not found");
- else
- System.out.println(myBook.findFirstByName("Lolo"));
- myBook.removeContact("951623847");
- System.out.println("My phone Book\n" + myBook);
- myBook.removeAll();
- System.out.println("My phone Book\n" + myBook);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement