Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10.  
  11. class Contact implements Serializable {
  12.  
  13. private static final long serialVersionUID = 1L;
  14. private String name;
  15. private String surname;
  16. private String phoneNumber;
  17. private String email;
  18.  
  19.  
  20. public Contact(String name, String surname, String phoneNumber, String email) {
  21. this.name = name;
  22. this.surname = surname;
  23. this.phoneNumber = phoneNumber;
  24. this.email = email;
  25. }
  26. public String getFullName() {
  27. return name + " " + surname;
  28. }
  29. public String getName() {
  30. return name;
  31. }
  32.  
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36.  
  37. public String getSurname() {
  38. return surname;
  39. }
  40.  
  41. public void setSurname(String surname) {
  42. this.surname = surname;
  43. }
  44.  
  45. public String getPhoneNumber() {
  46. return phoneNumber;
  47. }
  48.  
  49. public void setPhonenumber(String phoneNumber) {
  50. this.phoneNumber = phoneNumber;
  51. }
  52.  
  53. public String getEmail() {
  54. return email;
  55. }
  56.  
  57. public void setEmail(String email) {
  58. this.email = email;
  59. }
  60.  
  61. @Override
  62. public String toString() {
  63. return "Contact [name=" + name + ", surname=" + surname + ", phoneNumber=" + phoneNumber + ", email=" + email
  64. + "]";
  65. }
  66.  
  67. }
  68.  
  69. public class AdressBook {
  70. public static AdressBook instance;
  71. private ArrayList<Contact> contacts;
  72. private Scanner scanner;
  73.  
  74. public static void main(String[] args) throws ClassNotFoundException {
  75. instance = new AdressBook();
  76. instance.init();
  77. instance.run();
  78. }
  79.  
  80. private void init() throws ClassNotFoundException {
  81.  
  82. contacts = new ArrayList<Contact>();
  83.  
  84. try
  85. {
  86. // load from file
  87. FileInputStream fileInput = new FileInputStream("src/adressbook.txt");
  88. ObjectInputStream objectInput = new ObjectInputStream(fileInput);
  89. objectInput.readObject();
  90. objectInput.close();
  91. fileInput.close();
  92. }
  93. catch (IOException e)
  94. {
  95. // no file
  96. e.printStackTrace();
  97. }
  98.  
  99. scanner = new Scanner(System.in);
  100. }
  101.  
  102. private void run() {
  103. System.out.println("What do you want to do?");
  104. System.out.println("1-add contact");
  105. System.out.println("2-search contacts");
  106. System.out.println("3-edit contacts");
  107. System.out.println("4-list contacts");
  108. System.out.println("5-delete contact");
  109. System.out.println("6-quit");
  110.  
  111. int option = scanner.nextInt();
  112. switch (option) {
  113. case 1:
  114. addContact();
  115. break;
  116. case 2:
  117. searchContacts();
  118. break;
  119. case 3:
  120. editContacts();
  121. break;
  122. case 4:
  123. listContacts();
  124. break;
  125. case 5:
  126. deleteContact();
  127. break;
  128. case 6:
  129. System.exit(0);
  130. default:
  131. System.out.println("Error");
  132. break;
  133. }
  134.  
  135. // save to file
  136. try
  137. {
  138. FileOutputStream fileOutput = new FileOutputStream("src/adressbook.txt");
  139. ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
  140. objectOutput.writeObject(contacts);
  141. objectOutput.close();
  142. fileOutput.close();
  143. }
  144. catch (IOException e)
  145. {
  146. e.printStackTrace();
  147. }
  148.  
  149. }
  150.  
  151. private void addContact() {
  152. System.out.println("Name:");
  153. String name = scanner.next();
  154. System.out.println("Surname:");
  155. String surname = scanner.next();
  156. System.out.println("phonenumber:");
  157. String phoneNumber = scanner.next();
  158. System.out.println("email:");
  159. String email = scanner.next();
  160. contacts.add(new Contact(name,surname,phoneNumber,email));
  161.  
  162. instance.run();
  163. }
  164.  
  165. private void searchContacts() {
  166. System.out.println("Search by (1)name (2)surname (3)phone number :");
  167. int option2 = scanner.nextInt();
  168.  
  169. switch (option2) {
  170. case 1:
  171. if (option2 == 1)
  172. for(int i=0; i != contacts.size(); i++) {
  173. if (contacts.get(i).getName().equals(scanner.next()))
  174. System.out.println(contacts.get(i));
  175. instance.run();
  176. }
  177. case 2:
  178. if (option2 == 2)
  179. for(int i=0; i != contacts.size(); i++) {
  180. if (contacts.get(i).getSurname().equals(scanner.next()))
  181. System.out.println(contacts.get(i));
  182. instance.run();
  183. }
  184. case 3:
  185. if (option2 == 3)
  186. for(int i=0; i != contacts.size(); i++) {
  187. if (contacts.get(i).getPhoneNumber().equals(scanner.next()))
  188. System.out.println(contacts.get(i));
  189. instance.run();
  190. }
  191. }
  192. }
  193.  
  194. private void editContacts() {
  195. //todo
  196. }
  197.  
  198. private void listContacts() {
  199. for (Contact current : contacts) {
  200. System.out.println(current);
  201. }
  202. System.out.println();
  203. instance.run();
  204. }
  205.  
  206. private void deleteContact() {
  207. System.out.println("REMOVE BY FULL NAME ONLY: ");
  208. scanner.nextLine();
  209. for (int i=0; i<contacts.size(); i++)
  210. if(contacts.get(i).getFullName().equals(scanner.nextLine()))
  211. contacts.remove(i);
  212. instance.run();
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement