Results 1 to 1 of 1
- 12-21-2007, 09:46 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 35
- Rep Power
- 0
can you help me with my program please?
hi all,
I have a problem with the sellMilk function at the Milk class I don't know how to write it right :( I've tried everything so I need you to help me.
this function should check the expirey date of the milk and sell the required amount if it is not expired. if it was expired just delete the milkbox.
I have cases like if the first box has 5 kg and not expired , second box has 10 kg and expired, third box has 8 kg and not expired .. and if the required amount to sell is 6 kg for example it should work like this: first box should become zero because 5 kg has been sold and remainder is 1 .. so it should check the expirey date of the second box and it is expired so delete it. and then check the third box's expirey date and it is not expired so 8-1 = 7 .. and by that way 6 kg has been sold.
my code doesn't work well like that! here is the full program so you can check the code to help me please ..
the problem is with SellMilk() at the Milk Class
Thank you
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 { //add attributes including list of Coffee 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()); } //Add new bpx 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()); } //sell a specific coffee 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(); // display specific coffee stock info including info of batches and total stock } } 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() { // display batch info including the stock and date 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() { // display coffee info including batches info 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"); } }
Similar Threads
-
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
cannot run the program
By amiey in forum New To JavaReplies: 1Last Post: 11-20-2007, 04:13 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
Why does this program not end?
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks