Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Cart
package obs;
import java.util.*;
import java.sql.*;
import obs.*;
import javax.ejb.*;
import javax.naming.*;
import obs.order.*;




public class Cart
{
ArrayList items = new ArrayList();

public Item find(String isbn)
{
Iterator itr = items.iterator();
Item itm;
while ( itr.hasNext())
{
itm = (Item) itr.next();
if ( itm.getIsbn().equals(isbn))
{
return itm;
}
} // end of while

return null;
}

// adds an item if not already existing
// otherwise add 1 to qty
public void addItem(String isbn )
{
//check whether isbn is already present
Item item = find(isbn);
if ( item != null)
item.addQty(1);
else
{
// get detais from Books tables

Connection con =null;
obs.User user = new obs.User();
try
{
con = user.getConnection();
PreparedStatement ps = con.prepareStatement("select title,price from books where isbn = ? ");
ps.setString(1,isbn);

ResultSet rs = ps.executeQuery();
if ( rs.next())
{
item = new Item(isbn, rs.getString(1), rs.getInt(2) );
items.add(item);
}

rs.close();
ps.close();

}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
finally
{
try {con.close();} catch(Exception ex) {}
}
} // end of else
}

public ArrayList getItems()
{ return items; }

public void removeItem(String isbn)
{
Item item = find(isbn);
if ( item != null)
items.remove(item);
} // end or removeItem

public void clearAll()
{
items.clear();
}

public void updateQty(String isbn, int qty)
{
Item item = find(isbn);
if ( item != null)
item.setQty(qty);
} // end of updateQty()


public String finalizeOrder(int userid)
{

try
{
Context ctx = getInitialContext();
// get access to bean

OrderHome home = (OrderHome) ctx.lookup("obs.order");
Order order = home.create();

String id= order.addOrder(userid,items);
return id;
}
catch(Exception ex)
{
System.out.println( ex.getMessage());
return null;

}

} // end of finalizeOrder



public boolean cancelOrder(int ordid)
{

try
{
Context ctx = getInitialContext();
// get access to bean

OrderHome home = (OrderHome) ctx.lookup("obs.order");
Order order = home.create();

return order.cancelOrder(ordid);

}
catch(Exception ex)
{
System.out.println( ex.getMessage());
return false;

}


} // end of finalizeOrder

public Context getInitialContext()
{

String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

try
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL,"t3://localhost:7001");
return new InitialContext(env);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
return null;
}

}

}




Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// User Bean

package cs;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.*;



public class Customer
{


private String custname;
private String password;
private String email;
private String phoneno;

private Context ctx;

public Context getJNDIContext()
{
return ctx;
}

public Customer()
{
ctx = getInitialContext();
}

public void setCustname(String custname)
{ this.custname = custname; }

public String getCustname()
{ return custname; }


public void setPhoneno (String phoneno)
{ this.phoneno = phoneno; }

public String getPhoneno()
{ return phoneno; }

public void setPassword(String password)
{ this.password= password; }

public String getPassword()
{ return password; }

public void setEmail (String email)
{ this.email = email; }

public String getEmail()
{ return email; }

// returns true if uname and pwd are valid
public boolean isValid()
{
Connection con = null;
PreparedStatement ps = null;
try
{
con = getConnection();
ps = con.prepareStatement("select phoneno, email from customers where custname = ? and pwd= ?");
ps.setString(1,custname);
ps.setString(2,password);

ResultSet rs = ps.executeQuery();
boolean found = false;

if ( rs.next())
{ phoneno = rs.getString("phoneno");
email = rs.getString("email");
found = true;
}
return found;
}
catch(Exception ex)
{
System.out.println( ex.getMessage());
return false;
}
finally
{
clean(con,ps);
}

} // end of isValid

public String updatePassword(String newpassword)
{
Connection con = null;
PreparedStatement ps= null;

try
{
con = getConnection();
ps = con.prepareStatement("update customers set pwd = ? where custname = ?");
ps.setString(1,newpassword);
ps.setString(2,custname);

int cnt = ps.executeUpdate();
if ( cnt==1 )
return null;
else
return "Invalid Username!";

}
catch(Exception ex)
{
System.out.println( ex.getMessage());
return ex.getMessage();
}
finally
{
clean(con,ps);
}

} // end of updatePassword

public String registerUser()
{
Connection con = null;
PreparedStatement ps = null;

try
{
con = getConnection();
ps = con.prepareStatement("insert into customers values (?,?,?,?)");
ps.setString(1,custname);
ps.setString(2,password);
ps.setString(3,email);
ps.setString(4,phoneno);
ps.executeUpdate();
return null;

}
catch(Exception ex)
{
return ex.getMessage();
}
finally
{ clean(con,ps); }
}




public void clean(Connection con, PreparedStatement ps)
{
try
{ if ( ps != null ) ps.close();
if ( con != null) con.close();
}
catch(Exception ex)
{ System.out.println(ex.getMessage()); }
}

public Connection getConnection() throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
// connect using Thin driver
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle8i",
"custsup","custsup");

return con;
}


public Context getInitialContext()
{

String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

try
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL,"t3://localhost:7001");
return new InitialContext(env);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
return null;
}

}


} // end of bean








Binary file not shown.
Loading