Javaweb Study Record (14)--shopping mall's string stitching to achieve the most recently viewed items and shopping cart features

Source: Internet
Author: User

First, the string splicing tool class

Package blank.util;

Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.TreeMap;
Import Java.util.Vector;
Import Java.util.Map.Entry;

public class Stringutil {

public static string Jointstr (string sid,string ID) {
Vector<string> ids=new vector<string> ();
if (Sid!=null&&sid.equals (ID)) {
return SID;
}else{
Split
String[] Sids=sid.split (":");
if (sids.length==1) {
Return id+ ":" +SID;
}else{
for (String i:sids) {
Ids.add (i);
}
if (Ids.contains (ID)) {
String first=ids.firstelement ();
if (First.equals (ID)) {
return SID;
}else{
Delete First
Ids.remove (ID);
Ids.add (0, id);
String newvalue= "";
for (int i=0;i<ids.size (); i++) {
String Value=ids.elementat (i);
if (i==0) {
Newvalue=value;
}
else{
Newvalue=newvalue+ ":" +value;
}
}
return newvalue;
}
}else{
Return id+ ":" +SID;
}
}
}
}

public static string Jointstr (string ids,string id,integer num) {
Map<string,integer> map=new treemap<string, integer> ();
if ("". Equals (IDs)) {
Return id+ ":" +num;
}else{
String[] Tids=ids.split (",");
if (tids!=null) {
for (String tid:tids) {
String[] Idnum=tid.split (":");
Map.put (Idnum[0], integer.valueof (idnum[1]));
}
}
if (Map.containskey (ID)) {
Map.put (ID, map.get (ID) +num);
Set<entry<string, integer>> entities=map.entryset ();
Iterator<entry<string, integer>> iter=entities.iterator ();
int count=0;
String newvalue= "";
while (Iter.hasnext ()) {
Entry<string, integer> entity=iter.next ();
String Key=entity.getkey ();
Integer Value=entity.getvalue ();
String temp=key+ ":" +value;
if (count==0) {
Newvalue=temp;
}else{
Newvalue=newvalue+ "," +TEMP;
}
count++;
}
return newvalue;
}else{
Ids=ids+ "," +id+ ":" +num;
return IDs;
}
}
}
}

Description: The first method is used to record the ID number of the most recently browsed item, and the stitching form is 2:3:4. When there is repeated browsing, the original duplicates are deleted and the most recent browsing is placed before.

The second method is used to record the item ID and quantity of the shopping cart, and the stitching form is 2:1,3:4, which means that the number of goods with an ID of 2 1,id 3 is 4.

Javaweb Study Record (14)--shopping mall's string stitching to achieve the most recently viewed items and shopping cart features

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.