View Single Post
  #6 (permalink)  
Old 12-22-2007, 12:15 PM
java_fun2007 java_fun2007 is offline
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
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"); } }
Reply With Quote