Java:Assignment - Address book - Introduction
Address book application - Introduction
- Address book - 01 - Introduction - an introduction to the Assignment
In this assignment, you will create an application for an address book. The application will have a text interface presenting the user with a menu. The features of the address book application will be:
- List entries
- Add entry
- Adding a contact to the address book should be done interactively
- Adding a contact to the address book should automatically save the contact book to file
The assignment will be a mix of using an API provided by the teachers and writing classes needed for the components of the address book. Using the API provided by the teachers, usually means completing some half-ready code, or in some cases, writing new classes from scratch.
This assignment consists of four main tasks, leading up to a functional application (in small increments). The first two steps/tasks come with test code to help you verify that you have completed one step and can move on to the next.
This application will also be used in the next assignment, assignment 3, where the students will refactor parts of the application and add features.
Expand using link to the right to see a list of topics covered in this assignment.
Topics covered in this assignment:
- error handling (exceptions) - exceptions are thrown and we have a global exception handler in the main method
- inheritance - Contact extends Object and overrides equals, hashCode and toString
- access modifiers - generally keeping instance variables private and only some of the instance methods public
- implementing an interface
- anonymous inner class for MenuActions
- instance variables and methods - saving constructor parameters in instance variables, calling instance methods
- static variables and methods (including constants) textutils class offers a static method
- constructors - completing the constructor of the Contact class, writing a constructor in the SimpleApplication class from specification
- test cases are provided with the source code the students get
- packages/imports - There are a few packages in the application, which means both package declarations and import statements - also, understanding how to compile the application and running it requires understanding of class path and packages
- implementing a class from a design - both completing existing code and (optionally) writing the SimpleApplication class from scratch
- collections (using) - The SimpleAddressBook maintains an internal List<Contact>
- generics (using) - The SimpleMutableList interface is generic and the SimpleAddressBook uses <Contact> as type parameter
Description of the application
This is a sample run of the finished application:
INFO: There is no address book file. 0 items loaded from file. ====Address book==== 0 List 1 Add 2 quit Please enter a number from the menu: 0 (user entered "0") You selected List ====Address book==== 0 List 1 Add 2 quit Please enter a number from the menu: 1 (user entered "1") You selected Add Name: Ben Benson (user entered "Ben Benson") Email: email@example.com (user entered "firstname.lastname@example.org") Phone: 123456 (user entered "123456") Saving in /home/username/.address_book... ====Address book==== 0 List 1 Add 2 quit Please enter a number from the menu: 1 (user entered "1") You selected Add Name: Cyrus Cyber (user entered "Cyrus Cyber") Email: email@example.com (user entered "firstname.lastname@example.org") Phone: 654321 (user entered "654321") Saving in /home/username/.address_book... ====Address book==== 0 List 1 Add 2 quit Please enter a number from the menu: 0 (user entered "0") You selected List Ben Benson email@example.com 123456 Cyrus Cyber firstname.lastname@example.org 654321 ====Address book==== 0 List 1 Add 2 quit Please enter a number from the menu: 2 (user entered "2") Bye!
Understanding the layout of the code provided
The zip file you will download contains a directory structure as show below:
. |-- bin |-- doc | |-- allclasses-frame.html | |-- allclasses-noframe.html | |-- constant-values.html | |-- deprecated-list.html | |-- help-doc.html | |-- index-all.html | |-- index.html | |-- org | | `-- addressbook | | |-- storage | | | |-- Contact.html | | | |-- package-frame.html | | | |-- package-summary.html | | | |-- package-tree.html | | | |-- SimpleAddressBook.html | | | `-- SimpleMutableList.html | | `-- ui | | `-- cli | | `-- menu | | |-- MenuAction.html | | |-- MenuExample.html | | |-- Menu.html | | |-- package-frame.html | | |-- package-summary.html | | `-- package-tree.html | |-- overview-frame.html | |-- overview-summary.html | |-- overview-tree.html | |-- package-list | |-- resources | | |-- background.gif | | |-- tab.gif | | |-- titlebar_end.gif | | `-- titlebar.gif | |-- serialized-form.html | `-- stylesheet.css `-- src `-- org `-- addressbook |-- main | |-- SimpleApplication.java | `-- SimpleMain.java |-- storage | |-- Contact.java | |-- SimpleAddressBook.java | `-- SimpleMutableList.java |-- tests | |-- TestContact.java | `-- TestSimpleAddressBook.java |-- textutils | |-- TextUtilsExample.java | `-- TextUtils.java `-- ui `-- cli `-- menu |-- MenuAction.java |-- MenuExample.java `-- Menu.java
Wow, that’s a lot of files!
Let’s break this information down a little bit. The zip file will have two basic directories, bin and src. The src directory contains all source code for the application, distributed in packages (directories and sub directories). The bin directory will contain the class files (maintaining the same directory structure and packages as the src directory).
This means that in order to compile the source code, you need to tell javac to put the resulting class files in the bin directory structure. This is how you do that (on one single command line):
javac -cp src/ -d bin/ src/org/addressbook/tests/*.java src/org/addressbook/storage/*.java src/org/addressbook/main/*.java src/org/addressbook/ui/cli/menu/*.java src/org/addressbook/textutils/*.java
You are giving javac two options. First -cp src/ which tells javac that the packages are relative to the src directory. Second, -d bin/ which tells javac that the resulting class files should go to the bin/ directory structure.
This lets you compile the source code from the directory where both src and bin is located.
To compile only e.g. the org.addressbook.storage.Contact class, you can use the following command line:
$ javac -cp src/ -d bin/ src/org/addressbook/storage/Contact.java
To compile the test programs (which you should do right away!), you can use this command line:
$ javac -cp src/ -d bin/ src/org/addressbook/tests/*.java
To run the test file for testing the Contact class, you may use the following command line:
$ java -ea -cp bin/ org.addressbook.tests.TestContact
The flag -ea is very important, since it enables assertions! Assertions are the things which make tests fail when a condition is not fulfilled. Don’t forget the -ea flag! The flag -cp bin/ lets you run the program from the directory where both bin and src are located. It tells java that the package used is relative to the bin/ directory.
If you think that using class path like this is too complicated, we have provided a build.sh script for you, which you can use to compile all files:
If you think running the tests (and remembering the -ea flag) is difficult, you may use the test scripts:
$ ./run_test_contact.sh $ ./run_test_simple_address_book.sh
Class diagram of the finished application
Where to go next
The next page lists the tasks of the address book assignment: Java:Assignment_-_Address_book_-_Tasks