Each ordered linked list is generic, that is, stores objects of type E, where E is a parameter to the class. However, these linked lists also store, for each object, a string identifying the object. We refer to this string as the object's key.
Each linked list node must therefore have three fields (instead of the two in a normal linked list node): the object being stored, its key, and of course the next reference.
The nodes in the linked list are kept in order of increasing key value. For example, the object with key "abc" must be stored in a node that is before any node with key "xyz". Keys will be unique. Keys are compared using the String.compareToIgnoreCase method of the class String.
You must implement the keyed-node class, the orderered linked list class, and a main method in another class (perhaps called AddressList) that will implement an address book similar to what might be found in your cellphone.
Your ordered linked list class must provide at least the following methods:
Your address book class provides a user interface that allows a user to:
The address book class should have a private inner class that defines the data stored in the address book. That data must have names and telephone numbers, but may have more information, such as email addresses.
The details of the user interface are up to you. At the very least, you could print a prompt and have a user enter the command (one of the strings "add", "find", or "print"), then ask for the appropriate parameters for each. Or if you prefer, you can open a window to get the input from the user and report on the results. Whatever you do, make sure your code is clear enough (and easy enough to use) that it will be easy to use and grade, otherwise your score may be affected.
Starting program, address book is empty.
enter one of: add, find, print, quit. add
enter name to add. Hello World
enter telephone number for 'Hello World'. 1-800-555-0101
'Hello World' added to telephone book, with number 1-800-555-0101The address book has 1 entry.
'Hello World' was found, number is 1-800-555-0101The address book has 1 entry.
'foo' was not foundThe address book has 1 entry.
'hello world' added to telephone book, with number 1-800-555-0105 previous phone number for 'Hello World' was 1-800-555-0101.The address book has 1 entry.
'foo' added to telephone book, with number 1-800-555-0101The address book has 2 entries.
'foo' 1-800-555-0102 'Hello World' 1-800-555-0101The address book has 2 entries.
quit, discarding contents of address book.
Note that "Hello World", with uppercase letters, is replaced by "hello world" written with lowercase letters. Using the String.compareToIgnoreCase method, the two appear to be the same, that is, the comparison returns 0, but displaying the values shows that they are not identical.
Also note that when printing, the names are returned in alphabetical order, since the linked list is ordered.
If you want to make your address book code more useful, you may extend it as follows. This is for your own personal enjoyment and use, and no extra credit will be given.
Add the following method to the Ordered Linked List class:
Also add operations to your address book class. In particular, you can allow the user to:
The private inner class that defines the data stored in the address book can have a constructor that creates an address book entry given a string (say, a line read from a file), and a toString() method that produces a string that can be stored in a file. This allows you to make your address book persistent.
Both saving the address book, and printing it, are accomplished by calling get repeatedly, once for each entry in the address book, and calling toString() for each object. You may have different toString() methods (or two methods with different names), so that one is used for saving to the file, and the other for showing the address book to the user. Or you may use the same for both, as long as your constructor can accept the output of the toString() method.
The data is required to have names and telephone numbers, but may, at your choice, also hold additional fields, such as email addresses, postal addresses, perhaps birthdays, and other information.