Results 1 to 16 of 16
  1. #1
    javaseek is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default Need help from java expert

    Hi guys,

    I have an assignment to do in Java, My tutor want me to create a simple java application for a pies shop. The staff can use this program to enter the number of ordered pies and calculate how much money that the shop earn after a working day.

    I did search on google for a solution, but there's no help at all.

    I'm new to java programming but I'm from a PHP programming background. Please can someone experience in Java programming guide me on how to achieve this simple program.

    Your help will appreciated
    Last edited by javaseek; 10-13-2008 at 03:23 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Welcome to the forum. The first thing most of us will suggest to you is to break your problem down into small bits and solve each small bit in isolation, then you will have solved the big problem.

    Now since your question is very general I cannot give any specific recommendations. All I can say is a good site for learning and source of inspiration is the Sun Java tutorial located here:
    The Java Tutorials

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Design...

    You need to design what your program is going to do. For example:
    • Is your program going to have a GUI or is it going to be command line based.

    • Is it going to display the amount of pies ordered?

    • Is it going to write any info to a file?

    • Is it going to take any info from a file, like prices, etc?

    • etc


    Then do as Fubarable suggests and tackle each part separately.
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    javaseek is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks guys for your suggestion. I think the way to achieve this program is to read java tutorials and also the link that you provided.

    Cheers.

  5. #5
    javaseek is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi, guys I done my first java program...I am so happy:D

    Java Code:
    public class Pizza
    {   
       private int totalAmount = 0;     //total sales amount per day
       private int totalQuantity = 0;     //total ordered pies quantity per day
       private int totalOrder = 0;     //total number of order per day
       final int price = 4;   //pizza pies price $4.
       
       public static void main(String[] args)
       {
         new Pizza();
       }
       /**
       * Constructor for objects of class Pizza
       */
       public Pizza()
       {
         // get the input data   
           
         welcome();  //print welcome and instruction message       
            
         int quantity = 1;
         
         while(quantity != -1)
         {  
             switch(quantity)
            {            
                case 0:
                    output("0 pies ordered");
                    quantity = 1;
                    break;
                case 1:                          
                case 2:               
                case 3:               
                case 4:
                    quantity = inputInt("Enter number of pies: "); 
                    if((quantity > 4) || (quantity == 0) || (quantity < 0))break;
                    int customerPrice = (quantity * this.price); //customer price calculation 
                    output("Total price : $"+customerPrice+".00"); 
                    this.totalQuantity += quantity;
                    this.totalAmount += customerPrice;
                    this.totalOrder += 1;
                    break;
                 default:                
                    output("Error: You entered an invalid number of pies");              
                    quantity = 1;              
                    break;
                 
            }//end switch
            
         }//end while 
         
         SummaryRun(); //call method summaryRun
         
       }//end Pizza
       
       public void SummaryRun()
       {
           String msg ="\n\nToday's sales summary report\n";      
           msg += "===============================================\n";
           msg += "Total number of order: "+this.totalOrder+"\n";
           msg += "Total number of ordered pizza pies : "+this.totalQuantity+"\n";
           msg += "Total amount of sales : $"+this.totalAmount+".00\n";
           msg += "Good bye\n";
           msg += "===============================================\n";       
                  
           System.out.print(msg);
          
       }//end SummaryRun method  
    
       static void output(String info) { System.out.println(info); }
      
       public void welcome(){
           
           String msg ="Welcome to Pizza pies order program\n";      
           msg += "===============================================\n";
           msg += "The valid range number of pies is 1 to 4\n";
           msg += "The price is $"+this.price+".00 dollars per pies\n";
           msg += "To close this program, you need to enter -1\n";     
           msg += "===============================================\n";       
                  
           System.out.print(msg);
       }//end welcome message
       
       static int inputInt(String Prompt)
       {
         int result=0;
         try{result=Integer.parseInt(input(Prompt).trim());}
         catch (Exception e){result = -2;}
         return result;
       }
       
       //handle Input type
       static String input(String prompt)
       {
         String inputLine = "";
         System.out.print(prompt);
         try
         {
           java.io.InputStreamReader sys = new java.io.InputStreamReader(System.in);
           java.io.BufferedReader inBuffer = new java.io.BufferedReader(sys);
           inputLine = inBuffer.readLine();
         }
         catch (Exception e)
         {
           String err = e.toString();
           System.out.println(err);
         }
         return inputLine;
         
       }//end input method 
    
    }//end Pizza class
    My program is running in command line based. There is one thing is left, I need to do. I was wondering if there any built-in method in java that can round the double number to two decimal places. I need it for the pies price. I want to change the price data type to double.
    Last edited by javaseek; 10-14-2008 at 02:38 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You mean that you want to rounding a double number? You can easily rounding the number as follows.

    Java Code:
    double number = 45.987654546;
            String roundStr = String.format("%.2f", number);
    But there you get a string as a result, need to convert into a double again.

    Or you can use round() method on the Math class as well.

  7. #7
    javaseek is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi mate,

    Yes I want to round a double to 2 decimal places. Do you think that double is the right data type for price? What do you suggest for a right data type to use on price.

    I'm appreciated your help.
    thanks

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,781
    Rep Power
    25

    Default

    I want to round a double to 2 decimal places.
    This code will round a double to 2 decimal places:
    double roundFactor = 0.005;
    double d = 1.234455 + roundFactor; // round
    int x = (int)(d * 100); // to two decimal places, drops rest
    d = x / 100.0; // back to double = 1.23

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by javaseek View Post
    Hi mate,

    Yes I want to round a double to 2 decimal places. Do you think that double is the right data type for price? What do you suggest for a right data type to use on price.

    I'm appreciated your help.
    thanks
    Yes, double is ok to handling the price variable, since there have two decimal places for cents it's the best type. Or else you can use float as well, since price doesn't have that mush of larger value.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Using Math class you can do it so easily.

    Java Code:
    double result = Math.round(double_value * Math.pow(10, 2)) / Math.pow(10, 2);

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by javaseek View Post
    There is one thing is left, I need to do. I was wondering if there any built-in method in java that can round the double number to two decimal places. I need it for the pies price. I want to change the price data type to double.
    It is a sin to use double or float for currency. Do not do it.
    Never do it.

    You can not get proper accounting accuracy, ever, with doubles for currency. So its better to get into the habit, today, of doing it properly.

    The cleanest approach is to have a Money or Currency class, and incapsulate all the complexity.

    What you must do is store the currency as a fixed point number.
    So you store $9.93 per pizza as 993 pennies. Of if you are in the UK, store the number of pence.

    Never use double for currency.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    fishtoprecords, I'm totally confusing with your comments. You advice not to use double because of decimal points? Is it?

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    fishtoprecords, I'm totally confusing with your comments. You advice not to use double because of decimal points? Is it?
    Yes, you are confused.
    My advice is to never use double or float for currency.

    Floating point numbers are great for engineering. Engineers design for limited accuracy in numbers. Floating point numbers are rarely exact (they are only exact for powers of two, such as 1/2, 1/4, etc.)

    Accountants insist on exact numbers. Any system that uses money must exactly add up. You simply can not do this with float or double.

    The solution is to use fixed point. The easiest is to use the integer number of pennies, and accept the decimal point on input, and put it there on output.

  14. #14
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    See example code in
    Using double for money is a sin

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Thanks for the explanation. As you said in engineering level, floating point has a great value. That's why I advice to use them. And I don't know much about currency and accounts. ;) Anyway, thanks for the explanation as well as for the link.

  16. #16
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You should run the code, its small, in the sample.
    It clearly shows one of the many problems with using double/float for money

Similar Threads

  1. [Montreal] Recruiting Mobile Application Expert Developer
    By montreal.hr in forum Jobs Offered
    Replies: 0
    Last Post: 08-30-2008, 09:17 PM
  2. Replies: 2
    Last Post: 03-03-2008, 09:05 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
  •