Thompson Rivers University
Product Class
Assignment 2 Product - SM 2025What is the Product class for?
The Product class is the main class in this assignment. Each object of this class represents one food product. It stores important details about the product like its code, description, price, and category. It also protects the product from being updated by using a password lock.
This class brings everything together by implementing all three interfaces:
public class Product implements Category, PasswordLockable, Comparable<Product> {}Let’s break that down:
implements Categorymeans the class promises to follow the rules in theCategoryinterface. So it must includesetCategory(int)andgetCategory().implements PasswordLockablemeans it must include the four locking methods:setPassword,lock,unlock, andisLocked.implements Comparable<Product>means it must include acompareTo(Product)method. This will let us compare two products to see which one is cheaper.
Private Fields
These are the variables that each Product object will store. All fields are marked private so they cannot be accessed directly from outside the class. This follows the idea of encapsulation.
private String code; // unique ID for the product
private String description; // short name or label for the product
private double price; // price of the product
private int category; // category code (e.g., MEAT, DAIRY)
private String password; // the password used to lock/unlock the product
private boolean productIsLocked; // tells us if the record is lockedKeep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 54 more lessons in Thompson Rivers University.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.