Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public class Main {
  2.  
  3. class Contact{
  4. String name;
  5. String phoneNumber;
  6. String email;
  7.  
  8. }
  9.  
  10. class ContactsManager {
  11. // Fields:
  12. Contact [] myFriends;
  13. int friendsCount;
  14.  
  15. // Constructor:
  16. ContactsManager(){
  17. friendsCount = 0;
  18. myFriends = new Contact[500];
  19. }
  20.  
  21. // Methods:
  22. void addContact(Contact contact){
  23. myFriends[friendsCount] = contact;
  24. friendsCount++;
  25. }
  26.  
  27. Contact searchContact(String searchName){
  28. for(int i=0; i<friendsCount; i++){
  29. if(myFriends[i].name.equals(searchName)){
  30. return myFriends[i];
  31. }
  32. }
  33. return null;
  34. }
  35. }
  36.  
  37. public static void main(String [] args){
  38. // Create the ContactsManager object
  39. ContactsManager myContactsManager = new ContactsManager();
  40. // Create a new Contact object for James
  41. Contact friendJames = new Contact();
  42. // Set the fields
  43. friendJames.name = "James";
  44. friendJames.phoneNumber = "0012223333";
  45. // Add James Contact to the ContactsManager
  46. myContactsManager.addContact(friendJames);
  47. // Create a new Contact object for Cezanne
  48. Contact friendCezanne = new Contact();
  49. // Set the fields
  50. friendCezanne.name = "Cezanne";
  51. friendCezanne.phoneNumber = "987654321";
  52. // Add Cezanne Contact to the ContactsManager
  53. myContactsManager.addContact(friendCezanne);
  54. // Create a new Contact object for Jessica
  55. Contact friendJessica = new Contact();
  56. // Set the fields
  57. friendJessica.name = "Jessica";
  58. friendJessica.phoneNumber = "5554440001";
  59. // Add Jessica Contact to the ContactsManager
  60. myContactsManager.addContact(friendJessica);
  61.  
  62. // Now let's try to search of a contact and display their phone number
  63. Contact result = myContactsManager.searchContact("Jessica");
  64. System.out.println(result.phoneNumber);
  65.  
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement