Results 1 to 7 of 7
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    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.

    Java 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.

    Java 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

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Java 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.

  3. #3
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    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?

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    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.

  5. #5
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    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.

  6. #6
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    Thank you so much for you help, but I still have the problem :confused:
    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.

    Java 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");
        }
    
        }

  7. #7
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    and here is the function of sellmilk():
    what's wrong with it?

    thanks

    Java 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;}

Similar Threads

  1. Loop Help
    By HeavyD in forum New To Java
    Replies: 7
    Last Post: 09-22-2010, 09:55 PM
  2. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 06:44 AM
  3. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 AM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •