Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package contact;
- import java.io.*;
- import java.util.*;
- public class InputContactInfo
- {
- public static void main (String [] args)
- {
- Scanner input = new Scanner (System.in);
- TreeMap <Integer, Contacts> contact = new TreeMap <Integer, Contacts>();
- String firstname = new String ();
- String lastname = new String();
- String phone = new String();
- String email = new String();
- int i = 1;
- while (i != 0)
- {
- 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.");
- int x = input.nextInt();
- switch (x)
- {
- case 1:
- System.out.println("Enter the first name of the contact.");
- firstname = input.next();
- System.out.println("Enter the last name of the contact.");
- lastname = input.next();
- System.out.println("Enter the phone number of the contact. Please use the format xxx-xxx-xxxx.");
- phone = input.next();
- System.out.println("Enter the email address of the contact.");
- email = input.next();
- Contacts contactinfo = new Contacts (firstname, lastname, phone, email);
- contact.put(i++, contactinfo);
- break;
- case 2:
- System.out.println("Enter the contact number you wish to delete.");
- int deletenum = input.nextInt();
- contact.remove(deletenum);
- System.out.println("Contact deleted.");
- break;
- case 3:
- try
- {
- ObjectOutputStream out = new ObjectOutputStream (new BufferedOutputStream (new FileOutputStream ("Contacts.dat")));
- out.writeObject(contact);
- out.close();
- System.out.println("Data Written to file Contacts.dat");
- }
- catch (IOException e)
- {
- System.out.println("Error writing contact data to file.");
- }
- break;
- case 4:
- System.out.println();
- TreeMap<Integer, Contacts> info = null;
- ObjectInputStream in = null;
- try
- {
- in = new ObjectInputStream (new BufferedInputStream (new FileInputStream("Students.dat")));
- info = (TreeMap)in.readObject();
- for (Map.Entry Contacts : info.entrySet())
- {
- Contacts c = contact.get(Contacts.getKey());
- System.out.println(c.getFirstName() + " " + c.getLastName() + ", " + c.getPhoneNumber() + ", " + c.getEmail() );
- System.out.println();
- }
- in.close();
- }
- catch (Exception e)
- {
- System.out.println("Error reading file.");
- }
- break;
- case 5:
- i = 0;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement