Skip to content
Permalink
a200e4a00e
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
133 lines (112 sloc) 4.06 KB
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.ecommerce.dao;
import com.ecommerce.loginpack.model.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
/**
*
* @author oladimeji
*/
public class ProductDao {
private Connection con;
private String query;
private PreparedStatement pst;
private ResultSet rs;
public ProductDao(Connection con) {
this.con = con;
}
public List<Product> getAllProducts(){
List<Product> products = new ArrayList<Product>();
try {
query = "select * from products";
pst = this.con.prepareStatement(query);
rs = pst.executeQuery();
while(rs.next()){
Product row = new Product();
row.setProductid(rs.getInt("productid"));
row.setName(rs.getString("name"));
row.setPrice(rs.getDouble("price"));
row.setCategory(rs.getString("category"));
row.setImage(rs.getString("image"));
products.add(row);
}
}catch(Exception e){
e.printStackTrace();
}
return products;
}
public Product getSingleProduct(int id) {
Product row = null;
try {
query = "select * from products where productid=? ";
pst = this.con.prepareStatement(query);
pst.setInt(1, id);
rs = pst.executeQuery();
while (rs.next()) {
row = new Product();
row.setProductid(rs.getInt("productid"));
row.setName(rs.getString("name"));
row.setCategory(rs.getString("category"));
row.setPrice(rs.getDouble("price"));
row.setImage(rs.getString("image"));
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return row;
}
public List<Cart> getCartProducts(ArrayList<Cart> cartList){
List<Cart> products = new ArrayList<Cart>();
try{
if(cartList.size()>0){
for(Cart item:cartList){
query = "select * from products where productid=?";
pst = this.con.prepareStatement(query);
pst.setInt(1,item.getProductid());
rs = pst.executeQuery();
while(rs.next()){
Cart row = new Cart();
row.setProductid(rs.getInt("productid"));
row.setName(rs.getString("name"));
row.setCategory(rs.getString("category"));
row.setPrice(rs.getDouble("price")*item.getQuantity());
row.setQuantity(item.getQuantity());
products.add(row);
}
}
}
}
catch(Exception e){
System.out.print(e.getMessage());
}
return products;
}
//creating an array list for all the products
public double getTotalCartPrice(ArrayList<Cart> cartList){
double sum =0;
try{
if(cartList.size()>0){
for(Cart item:cartList){
query = "select price from products where productid=?";
pst = this.con.prepareStatement(query);
pst.setInt(1, item.getProductid());
rs = pst.executeQuery();
//adding the total price
while(rs.next()){
sum+=rs.getDouble("price")*item.getQuantity();
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
return sum;
}
}