The address book viewer application

Now that you are comfortable creating, compiling, and executing a simple Java 9 module, let's update it and start adding address book viewer functionality.

The following informal class diagram shows how we'll design the application classes to begin with:

The main class has the main() method that displays the list of contacts in ascending order, sorted by the lastName property. It gets the list of contacts by calling the ContactUtil.getContacts() method and it sorts it using SortUtil.sortList(). It then displays the list of contacts to the console.

We'll start with a new model class Contact, which represents a single piece of contact information. Apart from the obvious contact-related private member variables and getters and setters, this class also has a couple of additions that'll come in handy later:

  • The constructor with arguments: This makes it easy for us to create contact objects. This is useful since we'll be hardcoding our contact list to begin with.
  • The toString() method: This provides readable output when we print Contact objects to the console.
  • The compareTo() method: Since we'll need to sort Contact objects, we'll have the Contact class, which implements Comparable, and the compareTo() method, which compares Contact instances by their lastName property.

Here's what the Contact class looks like:

    package packt.addressbook.model; 
    public class Contact implements Comparable { 
      private String id; 
      private String firstName; 
      private String lastName; 
      private String phoneNumber; 
      public Contact(String id, String firstName,
String lastName, String phoneNumber) { this.id = id; this.firstName = firstName; this.lastName = lastName; this. phoneNumber = phoneNumber; } // Getters and setters omitted for brevity public String toString() { return this.firstName + " " + this.lastName; } public int compareTo(Object other) { Contact otherContact = (Contact)other; return this.lastName.compareTo(otherContact.lastName); } }

Let's move on to the ContactUtil class. We need a source of some contact data to start coding with, so for now, we'll hardcode a few Contact objects and add them to a list. The ContactUtil.getContacts() method just prepares this hard coded list for now:

    package packt.addressbook.util; 
public class ContactUtil {
public List<Contact> getContacts() { List<Contact> contacts = Arrays.asList( new Contact("Edsger", "Dijkstra", "345-678-9012"), new Contact("Alan", "Turing", "456-789-0123"), new Contact("Ada", "Lovelace", "234-567-8901"), new Contact("Charles", "Babbage", "123-456-7890"), new Contact("Tim", "Berners-Lee", "456-789-0123") ); return contacts; } }

Next is the SortUtil class. You don't typically write sort methods, thanks to some good collection libraries that are available out of the box with Java. In this case, however, we will implement our own sorting algorithm for the purposes of learning about modules because it'll help us illustrate some important use cases throughout this book. Instead of creating a method specifically designed to sort Contact instances, we'll instead write a generic Bubble Sort method to sort any type that implements Comparable. Thanks to Contact implementing the Comparable interface, we should be able to use the SortUtil to sort its instances too:

    public class SortUtil { 
      public <T extends Comparable> List<T> sortList(List<T> list) { 
        for (int outer = 0; outer < list.size() - 1; outer++) {         
          for (int inner = 0; inner < list.size()-outer-1; inner++) {
if (list.get(inner).compareTo(list.get(inner + 1)) > 0) { swap(list, inner); } } } return list; } private <T> void swap(List<T>list, int inner) { T temp = list.get(inner); list.set(inner, list.get(inner + 1)); list.set(inner + 1, temp); } }

Now let's bring it all together in Main.java. We'll first call the getContacts() method on an instance of ContactUtil to get the hardcoded Contact list. Then we'll pass it to the sortList() method on an instance of SortList. We will then print the sorted list on the console using System.out.println():

    package packt.addressbook; 
 
    public class Main {  
      public static void main(String args) { 
        ContactUtil contactUtil = new ContactUtil(); 
        SortUtil sortUtil = new SortUtil(); 
        List<Contact> contacts = contactUtil.getContacts(); 
        sortUtil.sortList(contacts); 
        System.out.println(contacts); 
      } 
    } 

With this, we are done and ready to compile our code. We've seen that the command to compile your code looks like this:

$ javac --module-source-path src -d out <all-java-classes-here>

The part where we need to specify all Java classes can get tedious. We have a handful of classes at this time, and I already don't want to be bothered with typing all the class names (with paths!) in the command. This can get worse when we have multiple modules in the module source path and we want to compile them. Thankfully, there's a shortcut. The compiler also has the --module option that lets you specify the names of the modules that you need to compile. You can specify multiple module names here, separated by commas. The compiler looks for those modules in the module source path and compiles all classes in those modules. And as you can imagine, using this command is much easier!

Since we are compiling just one module here, that's what we'll specify as the value for the --module argument:

$ javac -d out --module-source-path src --module packt.addressbook  

The compilation should complete without any errors. Now, let's move on to execute your code. The command remains unchanged:

$ java --module-path out --module packt.addressbook/packt.addressbook.Main  

Here's the sorted output from the hardcoded contacts list:

[Babbage 123-456-7890, Lovelace 234-567-8901, Dijkstra 345-678-9012,
Turing 456-789-0123, Berners-Lee 456-789-0123]

If you expand the out directory, you'll again notice the one-to-one mapping between the .java files and the .class files:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.70.38