Java Bootcamp – Workbooks and Challenges

Shopping Cart – Part 1

From Java Bootcamp Resources -> Module 2 -> 8. Exception Handling -> Challenge, open the folder: shopping-cart.

The requirements.

Based on the requirements, there are three types of objects:

We can describe each item using two fields: name, price.

The store divides into aisles. Each aisle shelves a category of items.

The cart stores the user's items. It also performs the actions: add, remove, and checkout.

The tasks in this workbook apply to the Item class.

Task 1 – Fields

Add the necessary fields to the Item class. Protect each field using the private keyword.

Task 2 – The Big 3

If a class has fields, you need to apply the Big 3:

  • Constructor

  • Getters

  • Setters

Task 3 – Copy Constructor

To avoid the reference trap, we need a way to copy Item objects. So, add a copy constructor.

Task 4 – toString

Every class that models an object should have a toString method.

Add a toString method to your class, and return a String that adheres to this format:

return name + ": $" + price + " ";

Task 5 – Test your code.

Inside main(), import the Item class. Then, create three new objects of the Item class:

  • "Celery", 0.99

  • "Spinach", 0.99

  • "Coriander", 1.29

Print every object on the same line:

Feedback Summary
43 students





Written Reviews
There are no written reviews yet.