divider

Objective

Design a Product class that models items in a small retail business. Use constructors, encapsulation, static members, and instance methods with logic to track inventory and pricing.

Skills to Practice

  • Defining classes with private instance variables
  • Writing no-argument and full-argument constructors
  • Using a static variable to track shared information
  • Writing getters and methods that modify object state
  • Using conditionals to enforce valid values and invariants
  • Interacting with objects in a main/driver program
divider

Tasks

  • Create a new project named Retail-Product-Challenge.
  • Create a Product class with the following private instance variables:
    • id (a numeric product ID)
    • name (text)
    • price (number, e.g. 19.99)
    • stock (number of units currently in stock)
  • Add a static variable to track how many products have been created so far. Use it to assign a unique id to each new Product object.
  • Add two constructors:
    • No-argument constructor:
      • Assigns a unique id using the static counter.
      • Sets a default name like "Unnamed Product".
      • Sets price to 0.0.
      • Sets stock to 0.
    • Full-argument constructor:
      • Accepts a name, price, and starting stock.
      • Assigns a unique id using the static counter.
      • Enforces invariants:
        • If price is negative, set price to 0.0.
        • If starting stock is negative, set stock to 0.
  • Add the following methods:
    • Getters for at least id, name, price, and stock.
    • restock(amount) Increases stock by amount if amount > 0.
    • sell(amount) Attempts to reduce stock by amount if:
      • amount > 0, and
      • amount ≤ stock
      Returns a boolean (or your language's equivalent) indicating whether the sale was successful.
    • applyDiscount(percent) Lowers the price by a given percentage (for example, 20 means 20% off). Only apply the discount if percent is between 0 and 100. Price should never become negative.
    • static getProductCount() Returns how many Product objects have been created so far.
    • (Optional) A method that returns a formatted string like: [id] name - $price (Stock: stock)
  • In your Main program:
    • Create one Product using the full constructor (for example, "T-Shirt", 19.99, 10).
    • Create another Product using the no-argument constructor, then manually set its name, price, and stock using setters (if you choose to add setters) or by using a full constructor instead.
    • Print both products’ information.
    • Restock one product and print the updated information.
    • Sell a valid quantity of one product and print the updated stock.
    • Attempt to sell more units than are in stock and print a message if the sale fails.
    • Apply a discount (for example, 20%) to one product and print the new price.
    • At the end, print how many products have been created using your static method.
divider

Sample Output

Sample Output
Created products:
[1001] T-Shirt - $19.99 (Stock: 10)
[1002] Mug - $7.50 (Stock: 0)
Restocking Mug by 5...
[1002] Mug - $7.50 (Stock: 5)
Selling 3 T-Shirts...
[1001] T-Shirt - $19.99 (Stock: 7)
Applying 20% discount to T-Shirts...
[1001] T-Shirt - $15.99 (Stock: 7)
Attempting to sell 10 Mugs...
Not enough stock to complete sale.
[1002] Mug - $7.50 (Stock: 5)
Total products created: 2

Begin Challenge