Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-21-2007, 10:46 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
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


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"); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 09:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
cannot run the program amiey New To Java 1 11-20-2007 05:13 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM
Why does this program not end? trill New To Java 1 08-07-2007 08:22 AM


All times are GMT +3. The time now is 05:22 PM.


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