Objectives

The objectives of this lab are to use a package structure for the Shop app and also to write re-usable utilies. We will also experiment with Wrapper classes and parsing.

ShopV6.0 (using packages)

  • In this step, you will download ShopV5.0 and use it to create a models-controllers-utils package structure for ShopV6.0.

Downloading ShopV5.0 Project

  • Download ShopV5.0. The structure of this project is like so:

ShopV5.0 project structure

Creating ShopV6.0 Project

  • In Eclipse, create a new project called ShopV6.0.

  • Right-click on the src folder and select New, followed by Package. Enter “models” as the package name:

Creating models package

  • Create two more packages: “controllers” and “utils”.

  • Copy the ShopV5.0 files into the ShopV6.0 project to the locations specified in the screen shot below.

ShopV6.0 project structure

  • When we have copied all the existing code to this new format, you can see above that we have errors! The problem is that the Product class cannot be found in the MenuController and the Store classes (because they are stored in a different package).

  • In both MenuController and Store, import models.product;

  • The errors should now be gone

ShopV6.0 project structure

Testing the app

  • Run the app to make sure that all works ok.

ShopV6.0 (using utilities)

  • In this step, you will continue working on ShopV6.0 and add a utilities class for re-usable validation methods.

ShopV6.0 - ScannerInput class

  • In Eclipse, open ShopV6.0 (if it isn't open already).

  • In the utils package, create a new class called ScannerInput.

  • Cut the validNextDouble and validNextInt methods from MenuController and paste them into ScannerInput.

  • Change the accessor modifier for these methods from private to public. Make each method static.

  • Add a local Scanner object for each method and import the Scanner class.

  • Your ScannerInput class should look something like this:

ScannerInput class

MenuController changes

  • Now that we have the ScannerInput class created, our MenuController class no longer compiles; it doesn't recognise our validNextInt and validNextDouble methods anymore.

  • To fix this, add this line of code to the import section of MenuController:

import static utils.ScannerInput.*;

Testing the app

  • Run the app to make sure that all works ok.

  • When testing the app, you might notice that our dummy reads for emptying the buffer are now causing a problem!

  • We can get rid of these now and, as we are creating a new Scanner object for each int and double read, we don’t have to worry about emptying our buffers anymore!

ShopV6.0 (wrappers and parsing)

  • In this step, you will continue working on ShopV6.0 and use wrappers and parsing in your new ScannerInput class.

ShopV6.0 - updating ScannerInput class

  • In Eclipse, open ShopV6.0 (if it isn't open already).

  • In the utils package, open the class called ScannerInput.

  • Rewrite the validNextInt and validNextDouble methods to use Wrapper classes and parsers, as shown in the screen shot below:

Updated ScannerInput class

Testing the app

  • Run the app to make sure that all works ok.

Solution