RGAlex

MobilePhone

Apr 22nd, 2021 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. package academy.learnprograming;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class MobilePhone {
  6.     private final ArrayList<Contact> myContacts;
  7.  
  8.     public MobilePhone(String myNumber) {
  9.         this.myContacts = new ArrayList<Contact>();
  10.     }
  11.  
  12.     public boolean addNewContact(Contact contact) {
  13.         if (findContact(contact.getName()) >= 0) {
  14.             System.out.println("Contact is already on file");
  15.             return false;
  16.         }
  17.         myContacts.add(contact);
  18.         return true;
  19.     }
  20.  
  21.     public boolean updateContact(Contact oldContact, Contact newContact) {
  22.         int foundPosition = findContact(oldContact);
  23.         if (foundPosition < 0) {
  24.             System.out.println(oldContact.getName() + ", was not found.");
  25.             return false;
  26.         } else if (findContact(newContact.getName()) != -1) {
  27.             System.out.println("Contact with name " + newContact.getName() +
  28.                     " already exists. Update was not successful.");
  29.             return false;
  30.         }
  31.         this.myContacts.set(foundPosition, newContact);
  32.         System.out.println(oldContact.getName() + ", was replaced whit " + newContact.getName());
  33.         return true;
  34.     }
  35.  
  36.     public boolean removeContact(Contact contact) {
  37.         int foundPosition = findContact(contact);
  38.         if (foundPosition < 0) {
  39.             System.out.println(contact.getName() + ", was not found.");
  40.             return false;
  41.         }
  42.         this.myContacts.remove(foundPosition);
  43.         System.out.println(contact.getName() + ", was deleted.");
  44.         return true;
  45.     }
  46.  
  47.     private int findContact(Contact contact) {
  48.         return this.myContacts.indexOf(contact);
  49.     }
  50.  
  51.     private int findContact(String contactName) {
  52.         for (int i = 0; i < this.myContacts.size(); i++) {
  53.             Contact contact = this.myContacts.get(i);
  54.             if (contact.getName().equals(contactName)) {
  55.                 return i;
  56.             }
  57.         }
  58.         return -1;
  59.     }
  60.  
  61.     public String queryContact(Contact contact) {
  62.         if (findContact(contact) >= 0) {
  63.             return contact.getName();
  64.         }
  65.         return null;
  66.     }
  67.  
  68.     public Contact queryContact(String name) {
  69.         int position = findContact(name);
  70.         if (position >= 0) {
  71.             return this.myContacts.get(position);
  72.         }
  73.         return null;
  74.     }
  75.  
  76.     public void printContacts() {
  77.         System.out.println("Contact List");
  78.         for (int i = 0; i < this.myContacts.size(); i++) {
  79.             System.out.println((i + 1) + "." +
  80.                     this.myContacts.get(i).getName() + " -> " +
  81.                     this.myContacts.get(i).getPhoneNumber());
  82.         }
  83.     }
  84. }
  85.  
Add Comment
Please, Sign In to add comment