JAVA LAB TEST
Q1. A small electronics shop needs a simple system to track the initial stock and sales of its
products. The system can manage a maximum of 50 distinct products. You must
use arrays of objects to store the product data and perform stock calculations.
Part 1: Design the Product Class (OOP Fundamentals)
Create a class named [Link] Product class must have the following private instance
variables:
productCode (String) - e.g., "P001"
name (String)
price (double)
stockQuantity (int) - The number of units currently available.
Implement a constructor to initialize all four instance [Link] a public getter
method for productCode and [Link] a public method named
updateStock(int quantityChange):
This method increases or decreases the stockQuantity by quantityChange.
If the result of a decrease would make the stock negative, do not update the stock,
print an error message, and return false.
Otherwise, update the stock and return true.
Override the toString() method to return a formatted string with the product's code, name,
price, and current stock.
Q2. Implement the InventoryManager Class
Create a class named InventoryManager to manage the product array and [Link]
InventoryManager class must have two private instance variables:
products (an array of Product objects, with a fixed size of 50).
productCount (int) - to track the current number of distinct products in the inventory.
Implement a constructor to initialize the products array and set productCount to
[Link] the following public methods:
public boolean addProduct(String productCode, String name, double price, int
initialStock):
Checks if the maximum product limit (50) has been reached. If so, return false.
Creates a new Product object and adds it to the products array at the index specified
by productCount.
Increment productCount.
Return true.
(Assume product codes are unique for this lab).
public boolean recordSale(String productCode, int quantitySold):
Search for the product with the matching productCode in the products array (only up
to productCount).
If found, call the product's updateStock() method with a negative value (-
quantitySold). Return the result of the updateStock() call.
If not found, print an error and return false.
public double calculateTotalInventoryValue():
Iterate through the products array (up to productCount).
For each product, calculate its value (price * stockQuantity).
Sum the values of all products and return the total inventory value.
Q3. Test the System (Main Method)
Create a main class (e.g., ShopRunner) with a main [Link] an instance of
[Link] addProduct to add at least 4 different products (e.g., Laptop,
Keyboard, Mouse).Use recordSale to sell items for two different products (successful
sales).Use recordSale to attempt an oversized sale for a third product (testing the negative
stock check).Call calculateTotalInventoryValue() and print the shop's total inventory
[Link] the toString() method for a couple of the product objects to show their final stock.
SOL: 1) package LabTest;
public class Product {
private String productCode;
private String name;
double price;
private int stockQuantity;
public Product(String productCode, String name, double price, int
stockQuantity) {
[Link] = productCode;
[Link] = name;
[Link] = price;
[Link] = stockQuantity;
}
public String getProductCode() {
return productCode;
}
public int getStockQuantity() {
return stockQuantity;
}
public boolean updateStock(int quantityChange) {
if (stockQuantity + quantityChange < 0) {
[Link]("Error: Not enough stock for product " +
productCode);
return false;
}
stockQuantity += quantityChange;
return true;
}
public String toString() {
return "Code: " + productCode + ", Name: " + name + ", Price: " +
price + ", Stock: " + stockQuantity;
}
}
2) package LabTest;
public class Product {
private String productCode;
private String name;
double price;
private int stockQuantity;
public Product(String productCode, String name, double price, int
stockQuantity) {
[Link] = productCode;
[Link] = name;
[Link] = price;
[Link] = stockQuantity;
}
public String getProductCode() {
return productCode;
}
public int getStockQuantity() {
return stockQuantity;
}
public boolean updateStock(int quantityChange) {
if (stockQuantity + quantityChange < 0) {
[Link]("Error: Not enough stock for product " +
productCode);
return false;
}
stockQuantity += quantityChange;
return true;
}
public String toString() {
return "Code: " + productCode + ", Name: " + name + ", Price: " +
price + ", Stock: " + stockQuantity;
}
}
3) package LabTest;
public class ShopRunner {
public static void main(String[] args) {
InventoryManager manager = new InventoryManager();
[Link]("P001", "Laptop", 1200.0, 10);
[Link]("P002", "Keyboard", 50.0, 30);
[Link]("P003", "Mouse", 25.0, 40);
[Link]("P004", "Monitor", 300.0, 15);
[Link]("P001", 2);
[Link]("P002", 5);
[Link]("P003", 100);
double totalValue = [Link]();
[Link]("Total Inventory Value: $" + totalValue);
[Link]([Link]("P001"));
[Link]([Link]("P002"));
}
}
OUTPUT: Error: Not enough stock for product P003
Total Inventory Value: $16350.0
Code: P001, Name: Laptop, Price: 1200.0, Stock: 8
Code: P002, Name: Keyboard, Price: 50.0, Stock: 25