Java Objects and Classes Independent Practice

Learning objective: By the end of this lesson, students will be able to write a Java program that demonstrate their conceptual understanding of packages, classes, objects, instance and static members, and constructors.

Online store inventory

You are hired to create a simple program to manage the inventory of an online store. The store sells multiple products, each with a unique name, price, and quantity. Additionally, the store keeps track of the total number of products in stock (shared across all products).

Exercise requirements

  1. Create a package com.store.inventory containing a Product class.
  2. The Product class should have:
    • The following instance variables:
      • String name
      • double price
      • int quantity
    • The following static variable:
      • int totalProducts
    • A default constructor that initializes name to "Unknown", price to 0.0, and quantity to 0.
    • A parameterized constructor that initializes all three instance variables and increments the static totalProducts by 1.
    • A method to display product details: void displayDetails().
  3. Write a Main class in the com.store.app package to:
    • Instantiate multiple Product objects.
    • Display details for each product and the total number of products.