IntelliJ · ShopV2.0 · ShopV2.1 · array · algorithms


This console based app will ask the user how many products they would like to enter. 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:

  • products; an array of Products

  • total; an int representing the number of products currently in the array and also the next available index number in the array.

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. The total field is initialised to zero.

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.

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.

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:

  • true is returned then the add method should return a value of false i.e. the array is full, so we could not add a product.

  • false is returned, then add the product object that was passed as a parameter to the products array using the total field as the index location. Increment the total field by 1. return true, indicating that the product was successfully added to the array.

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:

  • true is returned i.e. no products have been added, return the text "No products".

  • false is returned, use a for loop to cycle over the elements in the array, building a String of all products. Return this string.

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.

  • Delete the Product instance field and include a Store instance field instead.

  • Create a new method called processOrder that takes no parameters and has a void return type. In this method, type in the following code:

    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++){
  • In your addProduct() method, change your code so that it looks like this (first and last line are changed):
    private void addProduct(){  
        //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();
        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;

        store.add(new Product(productName, productCode, unitCost, inCurrentProductLine));
  • Note: there is a bug in Scanner. When you read an int and then do a String read, Scanner ignores the String read. For this reason, you need to put in a "dummy" read of a String (i.e. input.nextLine() as the first line of code in the method).
  • In your printProduct() method, change the line of code to be:
  • In the main method, replace this line of code:



Run the App

Run the app; does all work as expected?

  • Are you asked how many products you would like to enter?
  • Can you enter details for each product?
  • When finished the data entry, are the product details printed to the console?


In IntelliJ, create a new project called ShopV2.1.

Copy your completed ShopV2.0 java files (Driver, Store and Product) into this new project. Alternatively, you can use our solution: ShopV2.0.

Add a new method to the Store class for each piece of functionality listed below:

  • list the products that are in the current product line (testing the boolean variable here).

  • list the cheapest product entered.

  • list the average product price.

  • list the products above a specific price (read from the user and passed as a parameter).

To test these new methods, call them from the main method in your Driver class.

Your output should look something like this:

ShopV2.1 Output