Introducing the sample Java 8 application

We will be using a sample Java 8 code base to try migrating to Java 9. It's a command-line shopping bag utility. When you run the application, it prompts you to add items to your shopping bag. Once you've added all the items and you are done, you type end. The application then displays a consolidated shopping list of items that you've added. The application is intentionally simple, but it gives us a good starting point to work through the migration.

Here's a screenshot of the application in action:

The application consists of three classes in three different packages:

  • The ShoppingBag class: It contains a method to add items to a shopping bag, and one to pretty print the contents of the bag. The class uses the Bag data structure from the Apache Commons Collections library. Think of this data structure as something similar to a Set, but with duplicates allowed:
        public class ShoppingBag  { 
     
          public static String END_TOKEN = "end"; 
          private Bag<String> bag = new HashBag<>(); 
     
          public boolean addToBag(String itemName) { 
            return (END_TOKEN.equals(itemName)) ||
this.bag.add(itemName); } public void prettyPrintBag() { ... } }
  • The UserInputUtil class: It contains a method to prompt the user for an input. It also contains a public close method to close the input stream when done:
        public class UserInputUtil { 
          Scanner scanner = new Scanner(System.in); 
    
          public String getUserInput(String prompt) { 
            System.out.print(prompt); 
            return scanner.nextLine(); 
          } 
    
          public void close() { 
            scanner.close(); 
          } 
 
        } 
  • The App class: Putting it all together. This class has the main method. It uses the UserInputUtil to prompt the user to enter items into the shopping bag. It adds each item to a ShoppingBag instance, and it then prints the bag when done:
        public class App { 
 
          private static final Logger logger = 
            Logger.getLogger(App.class.getName()); 
 
          public static void main(String[] args) { 
 
            logger.info("Shopping Bag application: Started"); 
 
            ShoppingBag shoppingBag = new ShoppingBag(); 
            UserInputUtil userInputUtil = new UserInputUtil(); 
            String itemName; 
            do { 
             
              itemName = userInputUtil.getUserInput("Enter item (
Type '" + ShoppingBag.END_TOKEN + "' when done): "); shoppingBag.addToBag(itemName); } while (!ShoppingBag.END_TOKEN.equals(itemName)); userInputUtil.close(); shoppingBag.prettyPrintBag(); logger.info("Shopping Bag application: Completed"); } }

In addition to the application code, there's a lib folder with the Apache Commons Collection library JAR file--commons-collections4-4.1.jar. The code depends on this library JAR file. We'll need to add this JAR file to the classpath when compiling and running the code.

I recommend looking at the included source code at the location -10-migrating-application/01-legacy-appand getting familiar with it. We'll be using this application as we work through the migration process.
..................Content has been hidden....................

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