Dave Drohan (SETU)

BACK NEXT

ShopV2.1 - Methods Operating on Arrays

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.

In this step, we will add a four new methods to the Store class so that we can do things like:

Then we will call these methods from the Driver class and print the output to the console.

You may need to refer to your lecture notes for help with coding this version of Shop.

Store Class - listCurrentProducts

In Store, add a new method with the signature public String listCurrentProducts()

Within this method, add code to handle the following:

Store Class - cheapestProduct

In Store, add a new method with the signature public Product cheapestProduct()

Within this method, add code to handle the following:

Store Class - averageProductPrice

In Store, add a new method with the signature public double averageProductPrice()

Within this method, add code to handle the following:

Store Class - listProductsAboveAPrice

In Store, add a new method with the signature public String listProductsAboveAPrice(double price)

Within this method, add code to handle the following:

Driver Class

Having written four new methods in the Store Class, we will need to write code in the Driver class to call them (and test them).

This is the current main method in Driver:

    public static void main(String[] args) {
		Driver driver = new Driver();
		driver.processOrder();
		driver.printProducts();
	}

Update your main method to make additional method calls:

    public static void main(String[] args) {
		Driver driver = new Driver();
		driver.processOrder();
		driver.printProducts();
        driver.printCurrentProducts();
        driver.printAverageProductPrice();
        driver.printCheapestProduct();
        driver.printProductsAboveAPrice();
	}

printCurrentProducts()

The code for the printCurrentProducts() method is here…TYPE in the code into your Driver class:

    //print out a list of all current products i.e. that are in the current product line.
    private void printCurrentProducts(){
        System.out.println("List of CURRENT Products are:");
        System.out.println(store.listCurrentProducts());
    }

Note how we called the method listCurrentProducts() in the Store class…we called it over the object of type Store, namely store.

printAverageProductPrice()

Next, add the method private void printAverageProductPrice() to Driver. In this method, add code that will give you the following processing:

printCheapestProduct()

Now add an new method private void printCheapestProduct() to Driver. This method should:

printProductsAboveAPrice()

Now add an new method private void printProductsAboveAPrice() to Driver. This method should:

Run the Code

When you run your code it should look something like this:

Save your work.