Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-18-2007, 02:46 PM
Member
 
Join Date: Nov 2007
Posts: 35
Rep Power: 0
java_fun2007 is on a distinguished road
Default can you help me with this for loop?
hi all,

This loop is to check the date of the milk boxes if they are expired then delete this box and it should continue looping .. and move to the next box and compare again, to sell the amount of milk.

but, it doesn't continue the loop it just delete the first box because it is expired and that's it! I want it to move to the next boxes and check the condition and sell the amount.
here is my code, I hope you can see the problem.

Code:
public double sellMilk (double amount)
{	
        //reduce the amount from the stock and return any amount not available
        // check the expirey date 
    
     for(int i=0;i<milkbox.size();i++)
      { 
            mBox milk = (mBox)milkbox.elementAt(i);
            double stock = milk.sellMilk(amount);
            double stockvalue = milk.getStock();
            Date ExpireDate = milk.getExDate();
        
            if (ExpireDate.before(new Date()))
       		{ 
       		milkbox.removeElementAt(i);
      	
      		System.out.println("it has expired date");
      	return amount;	}
      
  
      
  else   if (stock > 0 && ExpireDate.after(new Date() ) || stockvalue ==0 )
         {
                
         milkbox.remove(milk);
                
       amount = stock;
           
      }
  
  i++;
            
  }
 if(amount != 0)
 System.out.println("The extra amount  is "+amount+ " KG of milk boxes");
    return amount;    }
here is the other method in milkboxes class it might be wrong too, but i think the first code is wrong.

Code:
 public double sellMilk(double amount)
        {
                // update the inventory when sell operation is occured by subtracting the amount required from the
                // most old box, if it is not enough subtract from the next and so on.
                //remove the batch that has zero stock
                //sell returns the amount that is not available in stock


        double excessAmount = 0;
        if (amount < stock)
        {
            double newAmount = stock - amount;
            setStock(newAmount);
        }
        else    // amount >= stock
        {
            excessAmount = amount - stock;
            setStock(0);
        }
        return excessAmount;
    }

Thank you
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-18-2007, 04:11 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
Code:
            if (ExpireDate.before(new Date()))
       		{ 
       		milkbox.removeElementAt(i);
      	
      		System.out.println("it has expired date");
      	return amount;	}
      
  
      
  else   if (stock > 0 && ExpireDate.after(new Date() ) || stockvalue ==0 )
         {
It's only deleting the first expired box because of the return statement. This needs to be at the end of your method.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-18-2007, 04:23 PM
Member
 
Join Date: Nov 2007
Posts: 35
Rep Power: 0
java_fun2007 is on a distinguished road
Default
Thanks I just tried that and I still have the problem .. do I need another for loop after the first if statement or something?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-18-2007, 04:37 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
Well one thing that you don't need is the i++ at the end of your loop. The i++ in your loop header takes care of the iterations. With this at the end, your are stepping through the loop twice as fast as you need to.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-18-2007, 04:39 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
Also, did you move the return statement to the end of the method or to the end of the for loop? I sear that has to be your problem. I'm not testing it though so I could be wrong.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-22-2007, 11:15 AM
Member
 
Join Date: Nov 2007
Posts: 35
Rep Power: 0
java_fun2007 is on a distinguished road
Default
Thank you so much for you help, but I still have the problem
here is the whole code so you can test the function please.
it doesn't delete the expired box when its position is in the middle but if it was the first box it deleted.

Code:
import java.io.*;
import java.text.*;
import java.util.*;

public class Market

{

    public static void main(String args[ ])
    { System.out.print("Enter the Market name: " );
        String name1 = Stdin.readLine();
       
        Market_Store mymarketstore = new Market_Store(name1);

   System.out.println("Welcome To " +name1+" Market ");
   System.out.println("");

        System.out.println("1-Stock new Milk");
        System.out.println("2-Stock new Milk Box");
        System.out.println("3-Sell");
        System.out.println("4- Display");

        System.out.println("");
        System.out.print("Enter your choice: ");
        int choice = Stdin.readInteger();

        while (choice != 5)
        {
            switch (choice)
            {
            case 1:
             mymarketstore.stockNewMilk();
                break;
            case 2:

                mymarketstore.stockMilkBox();
                break;
                case 3:
         mymarketstore.sell();

            break;
            case 4:
           mymarketstore.display();
                        break;
                        case 5:
                
            default:
                System.out.println("wrong Number");
                System.out.println("Enter a number between 1 to 4 ");
                System.out.println("Enter 5 to Exit");
                break;
            }
            System.out.println("");
          System.out.println("Welcome To " +name1+" Market ");
   System.out.println("");

        System.out.println("1-Stock new Milk");
        System.out.println("2-Stock new Milk Box");
        System.out.println("3-Sell");
        System.out.println("4-Display");

        System.out.println("");
         System.out.print("Enter your choice: ");
            choice = Stdin.readInteger();
        } }
    }



class Market_Store
{	
        
        private String name;
        private Vector mymilk;


        public Market_Store(String n)
        {	
                name=n;
                mymilk = new Vector();
        }
        public void stockNewMilk()
        {
                
                String N;//milk type
               

                System.out.print("Enter the type of the milk: ");
                N=Stdin.readLine();
               
              
                Milk  m1 = new Milk (N);
                mymilk.addElement(m1);
        }
        public void stockMilkBox()
        {	

        System.out.println("Milk Available in stock : ");
        for (int i=0; i<mymilk.size(); i++){
        Milk m2 = (Milk)mymilk.elementAt(i);
        System.out.print(i+1+")");
        System.out.println(m2.getMilkType());  }

              
                System.out.print("Enter the number of the milk to stock new box: ");
        int	ii = Stdin.readInteger();

        ((Milk)(mymilk.elementAt(ii-1))).addNewBox();

        }//end stockMilkBox
        public void sell()
        {	
                //sell specific type of milk
               System.out.println("Milk Available in stock : ");
        for (int i=0; i<mymilk.size(); i++){
        Milk m2 = (Milk)mymilk.elementAt(i);
        System.out.print(i+1+")");
        System.out.println(m2.getMilkType());
                }

                
                System.out.print("Enter the number of the milk to sell:  ");
        int	ii = Stdin.readInteger();
System.out.print("Enter the amount required in Kg:  ");
double amount = Stdin.readDouble();
       
        ((Milk)(mymilk.elementAt(ii-1))).sellMilk(amount);


        }

        public void display()
        {	 System.out.println("Milk Available in stock : ");
        for (int i=0; i<mymilk.size(); i++){
        Milk m2 = (Milk)mymilk.elementAt(i);
        System.out.print(i+1+")");
        System.out.println(m2.getMilkType());
                }

                System.out.print("Enter the number of the milk to display:  ");
        int	ii = Stdin.readInteger();
                ((Milk)(mymilk.elementAt(ii-1))).display();

                
        }
       
       
       
}




class MilkBox
{	
        private Date expiredate;
        private Date date;
        private double stock;


        public MilkBox(double stck, Date ed)
        {	
                
                 date = new Date();
                expiredate = ed;
        }
        
      
        public double getStock()
        { 
         return stock;	}

        public Date getDate()
        { 
                 return date;

        }

public void setStock(double st)
{ stock = st;}

public void setExDate(Date dd)
{expiredate = dd;}

        public Date getExDate()
        { 
                 return expiredate;

        }

  public   double sellMilkBox(double amount)
        {
               
        double excessAmount = 0;
        if (amount < stock)
        {
            double newAmount = stock - amount;
            setStock(newAmount);
        }
        else   
        {
            excessAmount = amount - stock;
            setStock(0);
        }
        return excessAmount;
    }
 
  




public	void display()
        {	
                
                        System.out.println("The box of "+date+" has " +stock+" KG");
        }
}
class Milk
{
   
        private String Mtype;//milk type
       
        private Vector mybox;//vector of batches

        public Milk (String n)
        {
                Mtype =n;
                
        mybox = new Vector();

        }
  public  void addNewBox()
        {	
              
double stook;

 System.out.print("Enter the weight of the box: ");
 stook = Stdin.readDouble();
	Date exdate;//expirey date
   
 System.out.println("Enter the expirey date of the milk box:");
       int d; int m1; int y;
		System.out.println("Enter Year:" );
		y = Stdin.readInteger();
		System.out.println("Enter Month:" );
		m1 = Stdin.readInteger();
		System.out.println("Enter Day:" );
		d = Stdin.readInteger();
			Calendar r=new GregorianCalendar(y,m1,d);
			 exdate= r.getTime();
       
                //send the attributes to Box constructor
               MilkBox newBox = new MilkBox(stook,exdate);
                newBox.setStock(stook);
                newBox.setExDate(exdate);
                mybox.addElement(newBox);
                
                
               
			

        }

   public void display()
        { 
                
                System.out.println("Milk "+Mtype);
                        for (int i=0; i<mybox.size(); i++){
                MilkBox b= (MilkBox)mybox.elementAt(i);
                b.display();
                

            }


        }
       

   

 public double sellMilk (double amount)
{ 


   for(int i=0;i<mybox.size();i++)
      {   
           MilkBox b = (MilkBox)mybox.elementAt(i);
            double stock = b.sellMilkBox(amount);
            double value = b.getStock();
            Date ExpireyDate = b.getExDate();
  if ( ExpireyDate.before(new Date()))

{  mybox.removeElementAt(i);
         

        System.out.println("it has expired date"); 
}

      
     
         
 if (stock >1|| value  ==  0 && ExpireyDate.after(new Date()))
{       mybox.remove(b);   
      
 }
amount = stock;
 if ( ExpireyDate.before(new Date()))

{  mybox.removeElementAt(i);
          

        System.out.println("it has expired date");
}
 
 
}

  if(amount != 0)
  System.out.println("The extra amount is "+amount+ " KG");
return amount;}
        




public String getMilkType()
{ return Mtype;}
//set method
void setMilkType(String n)
{ Mtype = n;}


}//end class milk

//STDIN FILE 
////////////////



 final class Stdin
 {
   public static BufferedReader reader=new BufferedReader
    (new InputStreamReader(System.in));


   public static String readLine()
   {
   while(true)
   try{
       return reader.readLine();
       }
       catch(IOException ioe)
       {
         reportError(ioe);
         }
       catch(NumberFormatException nfe)
       {
        reportError(nfe);
        }
   }
   public static int readInteger()
   {
    while(true)
    try{
    return Integer.parseInt(reader.readLine());
       }
    catch(IOException ioe)
    {
    reportError(ioe);
    }
    catch(NumberFormatException nfe)
    {
    reportError(nfe);
    }
    }
   public static double readDouble()
   {
    while(true)
    try{
    return Double.parseDouble(reader.readLine());
       }
    catch(IOException ioe)
    {
    reportError(ioe);
    }
    catch(NumberFormatException nfe)
    {
    reportError(nfe);
    }
    }
    public static void reportError (Exception e)
    {
    System.err.println("Error input:");
    System.err.println("please re-enter data");
    }

    }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-22-2007, 11:20 AM
Member
 
Join Date: Nov 2007
Posts: 35
Rep Power: 0
java_fun2007 is on a distinguished road
Default
and here is the function of sellmilk():
what's wrong with it?

thanks

Code:
public double sellMilk (double amount)
{ 
 
 
   for(int i=0;i<mybox.size();i++)
      {   
           MilkBox b = (MilkBox)mybox.elementAt(i);
            double stock = b.sellMilkBox(amount);
            double value = b.getStock();
            Date ExpireyDate = b.getExDate();
  if ( ExpireyDate.before(new Date()))
 
{  mybox.removeElementAt(i);
         
 
        System.out.println("it has expired date"); 
}
 
      
     
         
 if (stock >1|| value  ==  0 && ExpireyDate.after(new Date()))
{       mybox.remove(b);   
      
 }
amount = stock;
 if ( ExpireyDate.before(new Date()))
 
{  mybox.removeElementAt(i);
          
 
        System.out.println("it has expired date");
}
 
 
}
 
  if(amount != 0)
  System.out.println("The extra amount is "+amount+ " KG");
return amount;}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
do...while loop eva New To Java 16 01-31-2008 07:44 AM
while loop michcio New To Java 5 01-27-2008 01:56 AM
A loop that doesn't loop MichYer New To Java 2 07-30-2007 09:44 AM
While loop leebee New To Java 1 07-18-2007 04:11 PM
Loop Help HeavyD New To Java 5 07-10-2007 02:26 PM


All times are GMT +2. The time now is 12:18 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org