views:

370

answers:

2

hi i am trying to create an array that doesnt add items if the value/size is more than 20. this solution adds only 1 item and then stops even if the value is less than 20. how do i change it so that it accepts values only upto 20 max.

package business;
import java.io.Serializable;
import java.util.ArrayList;

public class Cart implements Serializable
{
private ArrayList<LineItem> items;

    public Cart()
{
    items = new ArrayList<LineItem>();
}

    public ArrayList<LineItem> getItems()
{
    return items;
}

    public int getCount()
{
    return items.size();
}

    public void addItem(LineItem item)
{
    String code = item.getProduct().getCode();
    int quantity = item.getQuantity();
    double credit = item.getProduct().getCHours();
    String credit2 = Double.toString(item.getProduct().getCHours());
    int isize = items.size();


    for (int i = 0; i < items.size(); i++)
    {
        if(isize <= 20)
        {
        LineItem lineItem = items.get(i);
            lineItem.setQuantityCredit(credit);
            return;
       }
    }
    items.add(item);
    }

    public void addItemCredit(LineItem item)
{
        double credit = item.getProduct().getCHours();
        String credit2 = Double.toString(item.getProduct().getCHours());
        String code = item.getProduct().getCode();

    for (int i = 0; i < 20; i++)
    {
        LineItem lineItem2 = items.get(i);
            lineItem2.setQuantityCredit(credit);
            return;

        }
    items.add(item);

    }

public void removeItem(LineItem item)
{
        String code = item.getProduct().getCode();
        for (int i = 0; i < items.size(); i++)
        {
            LineItem lineItem = items.get(i);
            if (lineItem.getProduct().getCode().equals(code))
            {
                items.remove(i);
                return;
            }
        }
    }
}

i think wat u said is right so i tried this

if(isize <= 20) 
{ 
LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); 
} 
return; 
} 
items.add(item); 
} 


and 


if(isize <= 20) 
{ 
LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); 
} 
} 
items.add(item); 
return; 
}

but neither worked. so whr do i place d return statement?

+1  A: 

You're returning from the addItem method before adding the item to the list.

if(isize <= 20)
{
     LineItem lineItem = items.get(i);
     lineItem.setQuantityCredit(credit);
     return; // This is the problem
}
Apocalisp
hi thanks alot for takin time to reply i think wat u said is right so i tried this if(isize <= 20) { LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); } return; } items.add(item); } and if(isize <= 20) { LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); } } items.add(item); return; } but neither worked. so whr do i place d return statement?
A: 

I just want to clarify your code before I attempt to answer.

// get the total number of items in the cart
int isize = items.size();

// loop through the items
   // if there are more than 20 items then set the credit quantity and
     // return without adding
   // otherwise do nothing 
for (int i = 0; i < items.size(); i++)
{
    if(isize <= 20)
    {
    LineItem lineItem = items.get(i);
        lineItem.setQuantityCredit(credit);
        return;
   }
}
// if there are more than 20 items add another
items.add(item);

The reason that it is adding only one item and no more is as follows: The first time this method is called "isize" equals zero and therefore the for loop is skipped. The second time this method is called the "isize" equals zero and therefore the loop goes once and returns before adding.

I'm not certain what you are attempting to do inside of the for loop as it really doesn't make sense. If you just want the items array to be limited to 20 or less just add

if (items.size() < 20) {
  items.add(item);
}

to your code. If there is some more complex logic that you are attempting then explain your goals.

martinatime