Simple shopping cart

Source: Internet
Author: User
Simple shopping cart

Click to view the demo

 

Download source code


【Author】: Unknown <size: 3 k <Release Date: <browse: 145 〗
In DummyCart. the java program defines the basic model of the shopping cart. The DummyCart class uses the Vector Data Structure to simulate the shopping cart function. DummyCart has three attributes: submit item v, if the value of "submit" is "add", the item is added to the shopping cart. If it is "remove", the item is deleted from the shopping cart, item indicates the name of the Item to be added or discarded. v is a Vector type data that stores all information about the shopping cart. The method of the Vector class can be used to add or delete items to the shopping cart. processRequest () is the most important method in the DummyCart class. This method determines the value of submit, call addItem () or removeItem () to complete the basic shopping cart operations.
Package test;
Import javax. servlet. http .*;
Import java. util. Vector;
Import java. util. Enumeration;
Public class DummyCart {// fictitious shopping cart
Vector v = new Vector (); // The blue sub that stores the goods
String submit = null; // submit = "add" add goods, submit = "remove" discard goods
String item = null; // goods name

Private void addItem (String name ){
V. addElement (name );
}

Private void removeItem (String name ){
V. removeElement (name );
}

Public void setItem (String name ){
Item = name;
}

Public void setSubmit (String s ){
Submit = s;
}

Public String [] getItems (){
String [] s = new String [v. size ()];
V. copyInto (s );
Return s;
}

Public void processrequest (httpservletrequest request ){
// Null value for submit-user hit enter instead of clicking on
// "Add" or "Remove"
If (submit = NULL)
Additem (item );
If (submit. Equals ("add "))
Additem (item );
Else if (submit. Equals ("Remove "))
Removeitem (item );
// Reset at the end of the request
Reset ();
}
// Reset
Private void reset (){
Submit = NULL;
Item = NULL;
}
}
The following code is used for testing:
The user opens carts.html first, selects a product on the page, and then click Submit to submit the data to carts. JSP, carts. JSP first uses <JSP: usebean> to create a new session scope-type JavaBean Component Object cart. If this cart object already exists, you do not need to create it and use it directly. The carts. JSP program then assigns a value to the carts object using the <JSP: setproperty> Operation Command. Then the carts. JSP program calls the processrequest () method to complete the corresponding shopping cart operation based on the value of the submit attribute. Finally, the carts. JSP program uses the getitems () method of the cart object in combination with the for loop structure to output all the contents of the shopping cart.
<HTML>
<JSP: usebean id = "cart" Scope = "session" class = "test. dummycart"/>
<JSP: setproperty name = "cart" property = "*"/>
<%
Cart. processrequest (request );
%>
<Font size = 5 color = "# cc0000">
<Br> you have the following items in your cart:
<Ol>
<%
String [] items = cart. getItems ();
For (int I = 0; I <items. length; I ++ ){
%>
<Li> <% = items [I] %>
<%
}
%>
</Ol>
</FONT>
<Hr>
<% @ Include file = "carts.html" %>
</Html>

Below is carts.html
<Title> carts </title>
</Head>
<Body bgcolor = "white">
<Font size = 5 color = "# CC0000">
<Form type = POST action = carts. jsp>
<BR>
Please enter item to add or remove:
<Br>
Add Item:
<Select name = "item">
<OPTION> Beavis & Butt-head Video collection
<OPTION> X-files movie
<OPTION> Twin peaks tapes
<OPTION> NIN CD
<OPTION> JSP Book
<OPTION> Concert tickets
<OPTION> Love life
<OPTION> Switch blade
<OPTION> Rex Rugs & Rock n' Roll
</SELECT>
<Br>
<Input type = submit name = "submit" value = "add">
<Input type = submit name = "submit" value = "remove">
</Form>
</FONT>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.