/src/ChatApp/ContactList.java

https://bitbucket.org/blitz2145/cop4331-chat-application · Java · 146 lines · 78 code · 12 blank · 56 comment · 9 complexity · f92fe80527f9aaa65064e769a6f1517a MD5 · raw file

  1. package ChatApp;
  2. import java.io.*;
  3. import java.net.InetAddress;
  4. import java.util.*;
  5. /**
  6. * This class manages a list of contacts
  7. */
  8. public class ContactList {
  9. /**
  10. * Constructs a new contact list by reading data from "ContactList.dat".
  11. * If the file is not found, an empty list will be created
  12. */
  13. //suppress unchecked cast exception when deserializing contact list
  14. @SuppressWarnings("unchecked")
  15. public ContactList() {
  16. try {
  17. ObjectInputStream in = new ObjectInputStream(new FileInputStream("ContactList.dat"));
  18. list = (ArrayList<Contact>)in.readObject();
  19. in.close();
  20. Contact tmp = new Contact("test2");
  21. tmp.setIpAddress(InetAddress.getByName("192.168.1.136"));
  22. //list.add(tmp);
  23. } catch (FileNotFoundException e) {
  24. // TODO Auto-generated catch block
  25. list = new ArrayList<Contact>();
  26. System.out.print("Contact List file not found...");
  27. //e.printStackTrace();
  28. } catch (IOException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. } catch (ClassNotFoundException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. }
  36. /**
  37. * Selects a contact from the list
  38. * @param index the index of the contact to be selected
  39. * If (index < 0 || index > list.size() -1) the contact selected will be null
  40. */
  41. public void setSelected(int index) {
  42. if(index < 0 || index > list.size() -1) {
  43. selectedContact = null;
  44. }
  45. selectedContact = list.get(index);
  46. }
  47. /**
  48. * Accessor for the selected Contact
  49. * @return the selected Contact
  50. */
  51. public Contact getSelected() {
  52. return selectedContact;
  53. }
  54. /**
  55. * Adds a contact to the list
  56. * @param contact the contact to be added
  57. * @return true
  58. * @precondition contact != null
  59. */
  60. public boolean add(Contact contact) {
  61. assert contact != null : "violated precondition contact != null";
  62. return list.add(contact);
  63. }
  64. /**
  65. * Removes a contact from the list
  66. * @param contact the contact to be removed
  67. * @precondition contact != null
  68. */
  69. public void remove(Contact contact) {
  70. assert contact != null : "violated precondition contact != null";
  71. list.remove(contact);
  72. }
  73. /**
  74. * Given a username, this method searches the list of contacts for this
  75. * username and returns the index of the contact in the list if found.
  76. * It returns -1 if the username is not found.
  77. *
  78. * @param
  79. * username the username to find in the contacts list
  80. * @returns (index >= 0) if the username is found in the contact list
  81. * -1 if the username is not found in the contact list
  82. */
  83. public int find(String username) {
  84. int i = 0;
  85. for(Contact contact : list) {
  86. if(username.equals(contact.getUsername())){
  87. return i;
  88. }
  89. i++;
  90. }
  91. return -1;
  92. }
  93. /**
  94. * Creates a string array of the contacts' usernames
  95. * @return the usernames
  96. */
  97. public String[] getNames() {
  98. if(list == null) {
  99. return null;
  100. }
  101. String[] names = new String[list.size()];
  102. int i = 0;
  103. for(Contact contact : list) {
  104. names[i] = contact.getUsername();
  105. i++;
  106. }
  107. return names;
  108. }
  109. /**
  110. * Saves the contact list to file
  111. */
  112. public void saveContacts() {
  113. try {
  114. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("ContactList.dat"));
  115. out.writeObject(list);
  116. out.close();
  117. } catch (FileNotFoundException e) {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. } catch (IOException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. }
  125. /**
  126. *
  127. * @return the number of contacts in the list
  128. */
  129. public int size() {
  130. return list.size();
  131. }
  132. private ArrayList<Contact> list;
  133. private Contact selectedContact;
  134. }