Contacts – Part 6
In this workbook, you will add interactivity to the application.
Task 1
Inside Main
, make the ContactManager
object a class
variable.
Task 2
Define a function named loadContacts
.
/**
* Name: loadContacts
* @param fileName (String)
* @throws FileNotFoundException
*
* Inside the function:
* - 1. loads contacts from <fileName>.txt;
* - 2. manager adds all contacts to the contacts list.
* Hint: use scan.next to grab the next String separated by white space.
*/
The function receives the fileName
from which you will load contacts.
/**
* Name: loadContacts
* @param fileName (String) <--------
* @throws FileNotFoundException
*
* Inside the function:
* - 1. loads contacts from <fileName>.txt;
* - 2. manager adds all contacts to the contacts list.
* Hint: use scan.next to grab the next String separated by white space.
*/
The function throws a FileNotFoundException
.
/**
* Name: loadContacts
* @param fileName (String)
* @throws FileNotFoundException <--------
<
*
* Inside the function:
* - 1. loads contacts from <fileName>.txt;
* - 2. From the manager object, it adds all contacts to the contacts list.
* Hint: use scan.next to grab the next String separated by white space.
*/
Follow the steps to complete the function.
/**
* Name: loadContacts
* @param fileName (String)
* @throws FileNotFoundException
*
* Inside the function:
* - 1. loads contacts from <fileName>.txt;
* - 2. From the manager object, it adds all contacts to the contacts list.
* Hint: use scan.next to grab the next String separated by white space.
*/
From main()
, try
to call loadContacts
. After calling loadContacts
, print:
- print:
"CONTACTS LOADED\n\n"
- print the manager object.
In case of a failure, catch the FileNotFoundException
and print its message
.
Expected Output:
The second contact doesn't load because its date is not parseable.
Task 3 – manageContacts
Inside Main
, add a method called manageContacts
.
/**
* Name: manageContacts
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. In an infinite loop, the user can choose to a) add or b) remove a contact c) exit.
* - case a: ask for the name, phone number and birthDate.
* - case b: ask who they'd like to remove.
* - case c: break the loop.
* - 3. call close() from the Scanner object.
*/
Create a Scanner
object.
/**
* Name: manageContacts
* Inside the function:
* - 1. Starts a new instance of Scanner; <------
* - 2. In an infinite loop, the user can choose to a) add or b) remove a contact c) exit.
* - case a: ask for the name, phone number and birthDate.
* - case b: ask who they'd like to remove.
* - case c: break the loop.
* - 3. call close() from the Scanner object.
*/
-
Make a
while
loop that runs infinitely. -
println
:Would you like to \n\ta) add another contact\n\tb) remove a contact \n\tc) exit
/**
* Name: manageContacts
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. In an infinite loop, the user can choose to a) add or b) remove a contact c) exit. <------
* - case a: ask for the name, phone number and birthDate.
* - case b: ask who they'd like to remove.
* - case c: break the loop.
* - 3. call close() from the Scanner object.
*/
case a:
-
Add a contact from the user's data:
-
System.out.print("\tName: ");
-
//pick up name.
-
System.out.print("\tPhone Number: ");
-
//pick up number.
-
System.out.print("\tBirth Date: ");
-
//pick up birth date.
-
-
Inside a
finally
block,println
"\n\nUPDATED CONTACTS\n\n"
followed by the updated list of contacts.finally
gets called no matter what (even if an exception is caught). In other words, we will be printing the updated contacts regardless of the outcome.
/**
* Name: manageContacts
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. In an infinite loop, the user can choose to a) add or b) remove a contact c) exit.
* - case a: ask for the name, phone number and birthDate. <----------
* - case b: ask who they'd like to remove.
* - case c: break the loop.
* - 3. call close() from the Scanner object.
*/
case b:
-
System.out.println("\nWho would you like to remove?");
-
After removing the contact,
println
"\n\nUPDATED CONTACTS\n\n"
followed by the updated list of contacts.
/**
* Name: manageContacts
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. In an infinite loop, the user can choose to a) add or b) remove a contact c) exit.
* - case a: ask for the name, phone number and birthDate.
* - case b: ask who they'd like to remove. <----
* - case c: break the loop.
* - 3. call close() from the Scanner object.
*/
case c:
/**
* Name: manageContacts
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. In an infinite loop, the user can choose to a) add or b) remove a contact c) exit.
* - case a: ask for the name, phone number and birthDate.
* - case b: ask who they'd like to remove.
* - case c: break the loop. <------
* - 3. call close() from the Scanner object.
*/
Finally, call manageContacts
inside the try
block.
Task 4: The finally block
finally
gets called after thetry - catch
block no matter.
finally {
//gets called last (after try - catch)
}
This is a good place for cleanup code. In this case, you may feel free to just print: "Process Complete".