Sun official shopping cart

Source: Internet
Author: User

Shoppingcart class

Bytes -----------------------------------------------------------------------------------------------------------
Import java. util .*;

Public class shoppingcart {
Hashmap items = NULL;
Int numberofitems = 0;

Public shoppingcart (){
Items = new hashmap ();
}

Public synchronized void add (string mid, merchandise m ){
If (items. containskey (MID )){
Shoppingcartitem scitem = (shoppingcartitem) items. Get (MID );
Scitem. incrementquantity ();
} Else {
Shoppingcartitem newitem = new shoppingcartitem (m );
Items. Put (MID, newitem );
}

Numberofitems ++;
}

Public synchronized void remove (string bookid ){
If (items. containskey (bookid )){
Shoppingcartitem scitem = (shoppingcartitem) items. Get (bookid );
Scitem. decrementquantity ();

If (scitem. getquantity () <= 0)
Items. Remove (bookid );

Numberofitems --;
}
}

Public synchronized collection getitems (){
Return items. Values ();
}

Protected void finalize () throws throwable {
Items. Clear ();
}

Public synchronized int getnumberofitems (){
Return numberofitems;
}
Public synchronized double gettotal (){
Double amount = 0.0;

For (iterator I = getitems (). iterator (); I. hasnext ();){
Shoppingcartitem item = (shoppingcartitem) I. Next ();
Merchandise merch = (merchandise) item. getitem ();

Amount + = item. getquantity () * merch. getprice ();
}
Return roundoff (amount );
}

Private double roundoff (Double X ){
Long val = math. Round (x * 100); // cents
Returns Val/100.0;
}

Public synchronized void clear (){
Items. Clear ();
Numberofitems = 0;
}

}

Bytes --------------------------------------------------------------------------------------------------------

Shoppingcartitem class

Bytes --------------------------------------------------------------------------------------------------------
Public class shoppingcartitem {
Object item;
Int quantity;

Public shoppingcartitem (Object anitem ){
Item = anitem;
Quantity = 1;
}

Public void incrementquantity (){
Quantity ++;
}

Public void decrementquantity (){
Quantity --;
}

Public object getitem (){
Return item;
}

Public int getquantity (){
Return quantity;
}

}

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.