Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package contact;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class InputContactInfo
  6. {
  7. public static void main (String [] args)
  8. {
  9. Scanner input = new Scanner (System.in);
  10. TreeMap <Integer, Contacts> contact = new TreeMap <Integer, Contacts>();
  11. String firstname = new String ();
  12. String lastname = new String();
  13. String phone = new String();
  14. String email = new String();
  15. int i = 1;
  16. while (i != 0)
  17. {
  18. System.out.println("Press 1 to add a contact. Press 2 to delete a contact. Press 3 to write the contact data to the file. Press 4 to display all contacts. Press 5 to quit.");
  19. int x = input.nextInt();
  20. switch (x)
  21. {
  22. case 1:
  23. System.out.println("Enter the first name of the contact.");
  24. firstname = input.next();
  25. System.out.println("Enter the last name of the contact.");
  26. lastname = input.next();
  27. System.out.println("Enter the phone number of the contact. Please use the format xxx-xxx-xxxx.");
  28. phone = input.next();
  29. System.out.println("Enter the email address of the contact.");
  30. email = input.next();
  31. Contacts contactinfo = new Contacts (firstname, lastname, phone, email);
  32. contact.put(i++, contactinfo);
  33. break;
  34. case 2:
  35. System.out.println("Enter the contact number you wish to delete.");
  36. int deletenum = input.nextInt();
  37. contact.remove(deletenum);
  38. System.out.println("Contact deleted.");
  39. break;
  40. case 3:
  41. try
  42. {
  43. ObjectOutputStream out = new ObjectOutputStream (new BufferedOutputStream (new FileOutputStream ("Contacts.dat")));
  44. out.writeObject(contact);
  45. out.close();
  46. System.out.println("Data Written to file Contacts.dat");
  47. }
  48. catch (IOException e)
  49. {
  50. System.out.println("Error writing contact data to file.");
  51. }
  52. break;
  53. case 4:
  54. System.out.println();
  55. TreeMap<Integer, Contacts> info = null;
  56. ObjectInputStream in = null;
  57. try
  58. {
  59. in = new ObjectInputStream (new BufferedInputStream (new FileInputStream("Students.dat")));
  60. info = (TreeMap)in.readObject();
  61. for (Map.Entry Contacts : info.entrySet())
  62. {
  63. Contacts c = contact.get(Contacts.getKey());
  64. System.out.println(c.getFirstName() + " " + c.getLastName() + ", " + c.getPhoneNumber() + ", " + c.getEmail() );
  65. System.out.println();
  66. }
  67. in.close();
  68. }
  69. catch (Exception e)
  70. {
  71. System.out.println("Error reading file.");
  72. }
  73. break;
  74. case 5:
  75. i = 0;
  76. break;
  77. }
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement