Java Bootcamp – Workbooks and Challenges
)}

Shopping Cart – Part 6

In this workbook, you will apply the Big 3 steps to the Store class.

Requirements

Task 1 – Add the field


The store divides into aisles (rows), and each aisle (row) shelves items. So, items should be a 2D array.

Task 2 – The Big 3


  • Constructor

    • receives no parameters.

    • sets the field equal to a new 2D array with 7 rows and 3 columns.

  • Getter: getItem

    • receives two parameters: int row, int column.

    • returns an object at the requested row and column.

  • Setter: setItem

    • receives three parameters: int row, int column, Item item.

    • updates the element at the requested row and column.

As you're applying each step, careful not to fall into the reference trap.

Task 3 – main()


Remove all your code from main(). Add the following 2D array:

        Item[][] inventory = new Item[][] { 
            { new Item("Pepsi", 1.99), new Item("Crush", 1.99), new Item("Cola", 1.99) },
            { new Item("Honey Oats", 3.99), new Item("Fruit Loops", 1.99), new Item("Cheerios", 2.99) },
            { new Item("Milk", 4.99), new Item("Eggs", 0.99), new Item("Cheese", 1.89) },
            { new Item("Pepperoni", 2.99), new Item("Salami", 4.49), new Item("Mortadella", 4.99) },
            { new Item("Celery", 0.99), new Item("Spinach", 0.99), new Item("Coriander", 1.29) },
            { new Item("Shirt", 12.99), new Item("Pants", 24.99), new Item("Sweater", 18.99) }, 
            { new Item("Phone", 549.99), new Item("Printer", 349.99), new Item("Television", 1099) } 
        };

Create an object of the Store class. Then, use a nested loop to pass every element from inventory into Store.setItem.

Task 4.1 – toString


  • return a String variable temp that equals "".

  • Using a nested loop, append the toString of every item to temp.

  • Inside main():

    • System.out.println("\n\t******************************JAVA GROCERS******************************\n");

    • System.out.println(store + "\n");

Expected output:

Task 4.2 – toString


After the inner loop runs through an entire row, add two line separators \n\n to temp.

Expected output:

Task 4.3 – toString


Snippet:

    switch (i) {
        case 0: temp += "\tDRINKS:        "; break;
        case 1: temp += "\tCEREAL:        "; break;
        case 2: temp += "\tDAIRY:         "; break; 
        case 3: temp += "\tDELI:          "; break; 
        case 4: temp += "\tGREENS:        "; break; 
        case 5: temp += "\tCLOTHING:      "; break; 
        case 6: temp += "\tELECTRONICS:   "; break; 
    }        

Add the switch statement to replicate the expected output:

Task 4.4 – toString


After the nested loop runs to completion, add this line for aesthetics:

temp +="\t************************************************************************\n";

Good Luck!

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.