Question 2
Question 2
Problem Statement:
Write a Java program to perform the following tasks:
1.Create a price tracker for an e-commerce platform where:
Each product has a product ID, name, and price.
The platform allows updating the price of a product frequently.
Tasks:
Filter Products:
Filter products from the list where the price is greater than 5000.
Insert into Database:
Insert the filtered products into a database table named Products with the
following structure:
CREATE TABLE Products (
ProductNumber INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DOUBLE
);
Use JDBC to connect to the database and perform the insertion.
Print a message for each product inserted into the database.
Ensure the program handles exceptions and logs errors for any issues encountered
(e.g., file reading errors, database errors)
===================================================================================
=========================================
PROGRAM :
================
import java.io.*;
import java.sql.*;
import java.util.*;
class Product {
private int productNumber;
private String productName;
private double price;
@Override
public String toString() {
return "Product Number: " + productNumber + ", Product Name: " +
productName + ", Price: " + price;
}
}
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
reader.close();
}
preparedStatement.executeUpdate();
System.out.println("Inserted: " + product);
}
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
}
INPUT:
101,Smartphone,12000.50
102,Laptop,45000.00
103,Tablet,8000.75
104,Smartwatch,3500.00
105,Headphones,2500.00
OUTPUT:
Products sorted by Product Number:
Product Number: 101, Product Name: Smartphone, Price: 12000.5
Product Number: 102, Product Name: Laptop, Price: 45000.0
Product Number: 103, Product Name: Tablet, Price: 8000.75
Product Number: 104, Product Name: Smartwatch, Price: 3500.0
Product Number: 105, Product Name: Headphones, Price: 2500.0
==================================================
101,Smartphone,12000.50
102,Laptop,45000.00
103,Tablet,8000.75
104,Smartwatch,3500.00
105,Headphones,2500.00
Expected Output:
Products read from CSV:
Product Number: 101, Product Name: Smartphone, Price: 12000.5
Product Number: 102, Product Name: Laptop, Price: 45000.0
Product Number: 103, Product Name: Tablet, Price: 8000.75
Product Number: 104, Product Name: Smartwatch, Price: 3500.0
Product Number: 105, Product Name: Headphones, Price: 2500.0
=============================================================
Test Case 2: Sort Products by Product Number
Input
CSV File: products.csv
101,Smartphone,12000.50
102,Laptop,45000.00
103,Tablet,8000.75
104,Smartwatch,3500.00
105,Headphones,2500.00
Expected Output
Products sorted by Product Number:
Product Number: 101, Product Name: Smartphone, Price: 12000.5
Product Number: 102, Product Name: Laptop, Price: 45000.0
Product Number: 103, Product Name: Tablet, Price: 8000.75
Product Number: 104, Product Name: Smartwatch, Price: 3500.0
Product Number: 105, Product Name: Headphones, Price: 2500.0
====================================================================
Test Case 3: Sort Products by Price
Input
CSV File: products.csv
101,Smartphone,12000.50
102,Laptop,45000.00
103,Tablet,8000.75
104,Smartwatch,3500.00
105,Headphones,2500.00
Expected Output
Products sorted by Price:
Product Number: 105, Product Name: Headphones, Price: 2500.0
Product Number: 104, Product Name: Smartwatch, Price: 3500.0
Product Number: 103, Product Name: Tablet, Price: 8000.75
Product Number: 101, Product Name: Smartphone, Price: 12000.5
Product Number: 102, Product Name: Laptop, Price: 45000.0
=================================================================
Test Case 4: Add a New Product
Input
CSV File: products.csv
Add Product:
Expected Output