function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tyler HarrisTyler Harris 

Increment weirdness in Visualforce (Controller)

Hi All,

My visualforce controller is incrementing my shopping cart in strange ways. I've got an Apex class that is adding count variables when droping a list into a "Shopping Cart" Map. It will increment 1 no problem, but when I add 2 for the same item it will total to 4 instead of 3. What can I do in my controller and/or Visualforce to make this work. Also I noticed it was slow to load. How could I increase performance.

Apex
public class StoreFront2 {
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;

    public PageReference shop(){
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.count > 0){
               message += p.merchandise.name + ' (' + p.count + ') ' ;
               }
            }
        return null;
    }

    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.count > 0){
            if(cart.containsKey(c.merchandise.Id)){
               
                cart.get(c.merchandise.Id).count += c.count;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
            } 
        
        }
        }
        
    }
    
    public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>{};
                }
       
        return cart;
    }

    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
        }
    }

    public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>{};


            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }
}

Visualforce
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
  <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
  <h1>Store Front</h1>
  
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.Price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.count}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart" />
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
<form>    
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
          
          <apex:column headerValue='Product'>
              <apex:outputText value="{!cart[carti].merchandise.name}" />
          </apex:column>
          <apex:column headervalue='Price'>
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue='#Items'>
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
   
      </apex:dataTable>
    </form>
    
  
</apex:page>

 
Best Answer chosen by Tyler Harris
BalajiRanganathanBalajiRanganathan
you are dealing with the same object 
the following line
cart.get(c.merchandise.Id).count += c.count;
is same as c.count +=c.count.

In DisplayMerchandise add a new variable called tempCount and use this on the first datatable of your VF

then if tempCount is > 0
cart.get(c.merchandise.Id).count += c.tempCount;

 

All Answers

BalajiRanganathanBalajiRanganathan
you are dealing with the same object 
the following line
cart.get(c.merchandise.Id).count += c.count;
is same as c.count +=c.count.

In DisplayMerchandise add a new variable called tempCount and use this on the first datatable of your VF

then if tempCount is > 0
cart.get(c.merchandise.Id).count += c.tempCount;

 
This was selected as the best answer
Hargobind_SinghHargobind_Singh
Hi, can you also add to your post, how are you specifying which item to add ? Seems like you are running a loop on your whole list and adding to the shopping cart everytime ? 
 
Tyler HarrisTyler Harris
How can I total them? When I change the data table it throws a null pointer exception?
BalajiRanganathanBalajiRanganathan
can you post your updated code?