2. Add products
Add the product, add the incoming item element to the root element cart.
The item includes product details.
The value of total is calculated at the same time. The code is as follows:
public void addItemToCart(String stringItem)
throws IOException,SAXException{
//Convert item from String to XMLDocument
XMLDocument itemAdded=parseString(stringItem);
//Take out the item node and copy it
NodeList itemList=(“item”);
Node item=(0);
Node cloneItem=(true);
// If the cart is empty, construct a new cart
if(isCartEmpty()){
();
}
//If the item is not in the shopping cart, insert the item and calculate the total amount
if(!isItemExist(item,myCart)){
//Take the root element of myCart and add the copied item node to the back
Element cartRoot=();
Node cartNode=(cloneItem);
computeTotal(); //Calculate the total amount
}
}
3. Delete the product
Delete the product, that is, the item element of the product is used according to the product code.
Remove from the root element of myCart,
And recalculate the total value:
public void moveItemFromCart(String id){
//Fetch out the node set cartList and the root element cartRoot in item units
NodeList cartList=(“item”);
Element cartRoot=();
//Finding the product with the code selected id in cartList
for(int x=0;x< ();x++){
Node itemNode=(x);
String idValue=().
getFirstChild().getNodeValue();
//If found, delete the node from cartRoot and jump out of the loop
if((id)){
itemNode=(itemNode);
break;
}
}
computeTotal(); //Calculate the total amount
}
4. Change the quantity of goods
According to the number of fill-in by the customer on the page, modify the quantity in myCart.
And recalculate total:
public void addQuantityToCart(String qnty) throws
IOException,SAXException{
//Convert a set of XML strings that are passed into an XML document containing the quantity of products
XMLDocument quantityChanged=parseString(qnty);
//Fetch out the quantity node set containing a new number and the quantity node set in myCart
NodeList quantityList=(“quantity”);
NodeList cartList=(“quantity”);
//Cycle to change the quantity of goods
for(int x=0;x< ();x++){
// Assign the value of the new quantity to the corresponding quantity in myCart
String quantity=(x).getFirstChild().getNodeValue();
(x).getFirstChild().setNodeValue(quantity);
}
computeTotal(); //Calculate the total amount
}
5. Calculate the total amount
That is, calculate the value of total, where total=∑(price*quantity):
public void computeTotal(){
NodeList quantityList=(“quantity”);
NodeList priceList=(“price”);
float total=0;
//The total amount accumulated
for(int x=0;x< ();x++){
float quantity=((x)
.getFirstChild().getNodeValue());
float price=((x).getFirstChild().getNodeValue());
total=total+quantity*price;
}
//Att total to myCart's total
String totalString=(total);
(“total”).
item(0).getFirstChild().setNodeValue(totalString);
}
6. Determine whether the shopping cart is empty
Usually when adding new items, you also need to know whether the shopping cart is empty.
If empty, a new shopping cart is to be generated.
public boolean isCartEmpty(){
//Item's node set. If the number of nodes contained in this node set is 0, there will be no products in the shopping cart, and return true
NodeList itemList=(“item”);
if(()==0) return true;
else return false;
}
7. Determine whether the selected item is in the shopping cart
That is, determine whether the item of the newly transmitted product has existed in myCart. If it exists, return true.
public boolean isItemExist(Node item, XMLDocument cart){
NodeList itemList=(“item”);
Node id=();
String idValue=().getNodeValue();
if(()!=0){
for(int x=0;x< ();x++){
Node itemTemp = (x);
7Node idTemp=();
String idTempValue=().getNodeValue();
if((idTempValue)) return true;
}
return false;
}
return false;
}
Add the product, add the incoming item element to the root element cart.
The item includes product details.
The value of total is calculated at the same time. The code is as follows:
public void addItemToCart(String stringItem)
throws IOException,SAXException{
//Convert item from String to XMLDocument
XMLDocument itemAdded=parseString(stringItem);
//Take out the item node and copy it
NodeList itemList=(“item”);
Node item=(0);
Node cloneItem=(true);
// If the cart is empty, construct a new cart
if(isCartEmpty()){
();
}
//If the item is not in the shopping cart, insert the item and calculate the total amount
if(!isItemExist(item,myCart)){
//Take the root element of myCart and add the copied item node to the back
Element cartRoot=();
Node cartNode=(cloneItem);
computeTotal(); //Calculate the total amount
}
}
3. Delete the product
Delete the product, that is, the item element of the product is used according to the product code.
Remove from the root element of myCart,
And recalculate the total value:
public void moveItemFromCart(String id){
//Fetch out the node set cartList and the root element cartRoot in item units
NodeList cartList=(“item”);
Element cartRoot=();
//Finding the product with the code selected id in cartList
for(int x=0;x< ();x++){
Node itemNode=(x);
String idValue=().
getFirstChild().getNodeValue();
//If found, delete the node from cartRoot and jump out of the loop
if((id)){
itemNode=(itemNode);
break;
}
}
computeTotal(); //Calculate the total amount
}
4. Change the quantity of goods
According to the number of fill-in by the customer on the page, modify the quantity in myCart.
And recalculate total:
public void addQuantityToCart(String qnty) throws
IOException,SAXException{
//Convert a set of XML strings that are passed into an XML document containing the quantity of products
XMLDocument quantityChanged=parseString(qnty);
//Fetch out the quantity node set containing a new number and the quantity node set in myCart
NodeList quantityList=(“quantity”);
NodeList cartList=(“quantity”);
//Cycle to change the quantity of goods
for(int x=0;x< ();x++){
// Assign the value of the new quantity to the corresponding quantity in myCart
String quantity=(x).getFirstChild().getNodeValue();
(x).getFirstChild().setNodeValue(quantity);
}
computeTotal(); //Calculate the total amount
}
5. Calculate the total amount
That is, calculate the value of total, where total=∑(price*quantity):
public void computeTotal(){
NodeList quantityList=(“quantity”);
NodeList priceList=(“price”);
float total=0;
//The total amount accumulated
for(int x=0;x< ();x++){
float quantity=((x)
.getFirstChild().getNodeValue());
float price=((x).getFirstChild().getNodeValue());
total=total+quantity*price;
}
//Att total to myCart's total
String totalString=(total);
(“total”).
item(0).getFirstChild().setNodeValue(totalString);
}
6. Determine whether the shopping cart is empty
Usually when adding new items, you also need to know whether the shopping cart is empty.
If empty, a new shopping cart is to be generated.
public boolean isCartEmpty(){
//Item's node set. If the number of nodes contained in this node set is 0, there will be no products in the shopping cart, and return true
NodeList itemList=(“item”);
if(()==0) return true;
else return false;
}
7. Determine whether the selected item is in the shopping cart
That is, determine whether the item of the newly transmitted product has existed in myCart. If it exists, return true.
public boolean isItemExist(Node item, XMLDocument cart){
NodeList itemList=(“item”);
Node id=();
String idValue=().getNodeValue();
if(()!=0){
for(int x=0;x< ();x++){
Node itemTemp = (x);
7Node idTemp=();
String idTempValue=().getNodeValue();
if((idTempValue)) return true;
}
return false;
}
return false;
}