Hi Jesper,
thanks so much for your advice. My most crucial qns now is I want to store my items by loading them in from the data-base, rather that hard-cording them one by one in the servlet(Catalog.java).
But i do not know how...
I decided to post all the codes that i will be using for my shopping cart below. So u can have a better idea of what i am doing.
But now, the major problems lies in Catalog.java & ShoppingCart.java.
Really need to solve this urgently. Pls help me!!! Thanks in advance.
/**** Codes ****/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import java.text.*;
public class orderPage
extends HttpServlet {
private Connection con;
public void init() throws ServletException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
System.err.println("init: class not found" + e);
}
try {
con = DriverManager.getConnection("jdbc
dbc:jspgODBC");
}
catch (SQLException e) {
System.err.println("init: ODBC fails" + e);
}
}
//check if users is adding item from the first time
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
HttpSession session = request.getSession();
ShoppingCart cart;
synchronized (session) {
cart = (ShoppingCart) session.getAttribute("shoppingCart");
if (cart == null) {
cart = new ShoppingCart();
session.setAttribute("shoppingCart", cart);
}
String itemID = request.getParameter("itemID");
if (itemID != null) {
String numItemsString = request.getParameter("numItems");
if (numItemsString == null) {
cart.addItem(itemID);
}
else {
int numItems;
try {
numItems = Integer.parseInt(numItemsString);
}
catch (NumberFormatException nfe) {
numItems = 1;
}
cart.setNumOrdered(itemID, numItems);
}
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Status of Your Order";
out.println("<html><head><title>" + title + "</title></head>");
out.println("<body><h1 align = 'center'>" + title + "</h1>");
synchronized (session) {
List itemsOrdered = cart.getItemsOrdered();
double subTotal=0;
double totalCost=0;
if (itemsOrdered.size() == 0) {
out.println("<h2><i>No items in your cart...</i><h2>");
}
else {
out.println("<table border=1 align='center'><tr bgcolor='#C0CAD6'><th>Item Serial Number<th>Product<th>Unit Cost<th>Quanity<th>Total Cost");
ItemOrder order;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
for (int i = 0; i < itemsOrdered.size(); i++) {
order = (ItemOrder)itemsOrdered.get(i);
out.println("<tr>");
out.println("<td>" + order.getItemID() + "</td>");
out.println("<td>" + order.getShortDescription() + "</td>");
out.println("<td>" + formatter.format(order.getUnitCost()) + "</td>");
out.println("<td><form><input type='hidden' name='itemID' value='" +
order.getItemID() + "'>");
out.println("<input type='text' name='numItems' size=3 value='" +
order.getNumItems() + "'><br>");
out.println("<input type='submit' value='Update Order'></form>");
out.println("<td>" + formatter.format(order.getTotalCost()));
totalCost=order.getTotalCost();
subTotal+=totalCost;
System.out.println(subTotal);
}
out.println("<tr><td colspan=5 align='right'><b>SubTotal: </b>" + formatter.format(subTotal) + "</td></tr>");
}
}
}
}
import java.util.*;
//a shopping cart to track orders
public class ShoppingCart {
private ArrayList itemsOrdered;
public ShoppingCart() {
itemsOrdered = new ArrayList();
}
public List getItemsOrdered() {
return (itemsOrdered);
}
public synchronized void addItem(String itemID) {
ItemOrder order;
for (int i = 0; i < itemsOrdered.size(); i++) {
order = (ItemOrder) itemsOrdered.get(i);
if (order.getItemID().equals(itemID)) {
order.incrementNumItems();
return;
}
}
//If i load from a database, how shd i edit this line of code below so that i can reference a non-static variable???
ItemOrder newOrder = new ItemOrder(Catalog.getItem(itemID));
itemsOrdered.add(newOrder);
}
public synchronized void setNumOrdered(String itemID, int numOrdered) {
ItemOrder order;
for (int i = 0; i < itemsOrdered.size(); i++) {
order = (ItemOrder) itemsOrdered.get(i);
if (order.getItemID().equals(itemID)) {
if (numOrdered <= 0) {
itemsOrdered.remove(i);
}
else {
order.setNumItems(numOrdered);
}
return;
}
}
ItemOrder newOrder = new ItemOrder(Catalog.getItem(itemID));
itemsOrdered.add(newOrder);
}
}
//save the item's information
public class ItemOrder {
private CatalogItem item;
private int numItems;
public ItemOrder(CatalogItem item) {
setItem(item);
setNumItems(1);
}
public CatalogItem getItem() {
return (item);
}
protected void setItem(CatalogItem item) {
this.item = item;
}
public String getItemID() {
return (getItem().getItemID());
}
public String getShortDescription() {
return (getItem().getShortDescription());
}
public String getLongDescription() {
return (getItem().getLongDescription());
}
public double getUnitCost() {
return (getItem().getCost());
}
public int getNumItems() {
return (numItems);
}
public void setNumItems(int n) {
this.numItems = n;
}
public void incrementNumItems() {
setNumItems(getNumItems() + 1);
}
public void cancelOrder() {
setNumItems(0);
}
public double getTotalCost() {
return (getNumItems() * getUnitCost());
}
}
//Describe the catalog item for the online store
public class CatalogItem {
private String itemID;
private String shortDescription;
private String longDescription;
private double cost;
public CatalogItem(String itemID, String shortDescription,
String longDescription, double cost) {
setItemID(itemID);
setShortDescription(shortDescription);
setLongDescription(longDescription);
setCost(cost);
}
public String getItemID() {
return (itemID);
}
protected void setItemID(String itemID) {
this.itemID = itemID;
}
public String getShortDescription() {
return (shortDescription);
}
protected void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getLongDescription() {
return (longDescription);
}
protected void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
public double getCost() {
return (cost);
}
protected void setCost(double cost) {
this.cost = cost;
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
//A catalog that list the items available in inventory
public class Catalog
extends HttpServlet {
private Connection con;
String strItemName;
String strItemBrand;
String strCarModel;
String strSpecifications;
String strSerialNumber;
int intQuantity;
double Price;
int count = 0;
public void init() throws ServletException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
System.err.println("init: class not found" + e);
}
try {
con = DriverManager.getConnection("jdbc
dbc:jspgODBC");
}
catch (SQLException e) {
System.err.println("init: ODBC fails" + e);
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
PrintWriter send = res.getWriter();
res.setContentType("text/html");
try {
Statement stm = con.createStatement();
String sqlStr = "SELECT * FROM ItemDetails";
System.out.println(sqlStr);
ResultSet rs = stm.executeQuery(sqlStr);
ResultSetMetaData info = rs.getMetaData();
/* String strItemName;
String strItemBrand;
String strCarModel;
String strSpecifications;
String strSerialNumber;
int intQuantity;
double Price;*/
int count = 0;
while (rs.next()) {
strItemName = rs.getString("itemName");
strItemBrand = rs.getString("itemBrand");
strCarModel = rs.getString("carModel");
strSpecifications = rs.getString("specifications");
strSerialNumber = rs.getString("serialNumber");
intQuantity = rs.getInt("quantity");
Price = rs.getDouble("sellingPrice");
count++;
}
rs.close();
stm.close();
}
catch (SQLException e) {
send.println("<html><body>");
send.println("Error" + e);
send.println("</body></html>");
}
}
public void destroy() {
try {
con.close();
}
catch (SQLException e) {
System.err.println("destroy:" + e);
}
}
//Current Problem: The inventory is stored by hard-coding, but i want to dynamically store the values that are retrived from the database.
For e.g /*private CatalogItem[] items = {
new CatalogItem(strSerialNumber, strItemName, strSpecifications,
Price)
};*/
How do i go abt it??
private static CatalogItem[] items = {
new CatalogItem("89312", "Beetle Floor Liner", "The servelst is..",
39.95),
new CatalogItem("332252", "Corolla Air Filter", "The servelst is..",
39.95),
};
public static CatalogItem getItem(String itemID) {
CatalogItem item;
if (itemID == null) {
return (null);
}
for (int i = 0; i < items.length; i++) {
item = items[i];
if (itemID.equals(item.getItemID())) {
return (item);
}
}
return (null);
}
}
/**** End of codes ********/