Dave Drohan (SETU)

BACK NEXT

ShopV2.0 - Primitive Arrays

This console based app will ask the user how many products they would like to enter (this will be the capacity of the Primitive Array). When prompted, they will input the details for each of the products. When finished the data entry, the product details will be printed on the console.

During this step, you may need to view this weeks lecture notes to remind you how to use Arrays.

In IntelliJ, create a new Java Project called ShopV2.0.

Copy the Product and Driver classes from ShopV1.0 and paste them into ShopV2.0. Note: solution to ShopV1.0 is here.

Create a new class in the ShopV2.0 project called Store.

Your folder structure should look like this:

ShopV2.0 folder structure

Product Class

There are no changes required in this class.

Store Class

In the Store class, create two private instance fields:

Store Constructor

Write a constructor for this class that takes one parameter (numberItems). This parameter will be used to set the size of the array of Products.

Store - isFull

Write a private method called isFull that takes no parameters. This method should return true if the value of the total field is equal to the length of the products array, false otherwise.

Store - isEmpty

Write a private method called isEmpty that takes no parameters. This method should return true if the value of the total field is zero, false otherwise.

Store - add

Write an add method that takes one parameter (an Object of type Product) and returns a boolean value.

This method tests the boolean value returned from the isFull() method. If isFull() returns:

Store - listProducts

Write a listProducts method that takes no parameter and returns a String value. This method tests the boolean value returned from the isEmpty() method. If isEmpty() returns :

Driver Class

We will refactor this class to remove the Product instance field and replace it with an object of type Store. We will also cater for entering and printing multiple products. To do this:

    private void processOrder(){
        //find out from the user how many products they would like to order
        System.out.print("How many Products would you like to have in your Store?  ");
        int numberProducts = input.nextInt();

        store = new Store(numberProducts);

        //ask the user for the details of the products and add them to the order
        for (int i = 0; i < numberProducts; i++){
            addProduct();
        }
    }
    private void addProduct(){
        input.nextLine();  //dummy read of String to clear the buffer - bug in Scanner class.

        System.out.print("Enter the Product Name:  ");
        String productName = input.nextLine();
        System.out.print("Enter the Product Code:  ");
        int productCode = input.nextInt();
        System.out.print("Enter the Unit Cost:  ");
        double unitCost = input.nextDouble();

        //Ask the user to type in either a Y or an N.  This is then
        //converted to either a True or a False (i.e. a boolean value).
        System.out.print("Is this product in your current line (y/n): ");
        char currentProduct = input.next().charAt(0);
        boolean inCurrentProductLine = false;
        if ((currentProduct == 'y') || (currentProduct == 'Y'))
            inCurrentProductLine = true;

        boolean isAdded = store.add(new Product(productName, productCode, unitCost, inCurrentProductLine));
        if (isAdded){
            System.out.println("Product Added Successfully");
        }
        else{
            System.out.println("No Product Added");
        }
    }
    System.out.println(store.listProducts());
   driver.addProduct();

with:

   driver.processOrder();

Run the App

Run the app; does all work as expected?