0% found this document useful (0 votes)
67 views

Java 2

Uploaded by

Mrabet Mustapha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Java 2

Uploaded by

Mrabet Mustapha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Certainly, here's another Java exercise that involves classes, inheritance, error

handling with try-catch, arrays, and array sorting. In this exercise, we'll create
a simple product inventory management system with three classes: Product,
Inventory, and InventoryManager. Products can be added, removed, and sorted by
price.

java
Copy code
import java.util.Arrays;
import java.util.Comparator;

class Product {
private String name;
private double price;
private int stockQuantity;

public Product(String name, double price, int stockQuantity) {


this.name = name;
this.price = price;
this.stockQuantity = stockQuantity;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

public int getStockQuantity() {


return stockQuantity;
}

public void setStockQuantity(int stockQuantity) {


this.stockQuantity = stockQuantity;
}

@Override
public String toString() {
return "Product [Name: " + name + ", Price: $" + price + ", Stock Quantity:
" + stockQuantity + "]";
}
}

class Inventory {
private Product[] products;
private int productCount;

public Inventory(int maxProducts) {


this.products = new Product[maxProducts];
this.productCount = 0;
}
public void addProduct(Product product) {
if (productCount < products.length) {
products[productCount] = product;
productCount++;
} else {
System.out.println("Error: The inventory is full. Cannot add more
products.");
}
}

public void removeProduct(String productName) {


for (int i = 0; i < productCount; i++) {
if (products[i].getName().equals(productName)) {
for (int j = i; j < productCount - 1; j++) {
products[j] = products[j + 1];
}
products[productCount - 1] = null;
productCount--;
return;
}
}
System.out.println("Error: Product not found in the inventory.");
}

public Product[] getProducts() {


return Arrays.copyOf(products, productCount);
}
}

class InventoryManager {
public static void sortProductsByPrice(Product[] products) {
Arrays.sort(products, Comparator.comparingDouble(Product::getPrice));
}

public static void main(String[] args) {


// Create an inventory
Inventory inventory = new Inventory(5);

// Add products
inventory.addProduct(new Product("Laptop", 800.0, 10));
inventory.addProduct(new Product("Smartphone", 500.0, 15));
inventory.addProduct(new Product("Tablet", 300.0, 20));

// Remove a product
inventory.removeProduct("Tablet");

// Sort products by price


Product[] sortedProducts = inventory.getProducts();
sortProductsByPrice(sortedProducts);

System.out.println("Inventory Products:");
for (Product product : inventory.getProducts()) {
System.out.println(product);
}

System.out.println("\nInventory Products (Sorted by Price):");


for (Product product : sortedProducts) {
System.out.println(product);
}
}
}
In this exercise, we have three classes: Product, Inventory, and InventoryManager.
Products can be added, removed, and sorted by price. The InventoryManager class
provides a method to sort products by price and demonstrates the use of try-catch
blocks to handle exceptions when attempting to remove a product that doesn't exist
in the inventory.

You might also like