Results 1 to 16 of 16
Thread: Need help from java expert
- 10-13-2008, 03:21 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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 appreciatedLast edited by javaseek; 10-13-2008 at 03:23 AM.
-
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
- 10-13-2008, 06:13 AM #3
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 10-13-2008, 01:21 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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.
- 10-14-2008, 02:27 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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
Last edited by javaseek; 10-14-2008 at 02:38 AM.
- 10-14-2008, 02:50 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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);
Or you can use round() method on the Math class as well.
- 10-14-2008, 03:04 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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
- 10-14-2008, 03:54 AM #8I want to 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
- 10-14-2008, 06:06 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 10-14-2008, 06:15 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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);
- 10-14-2008, 08:18 AM #11
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.
- 10-14-2008, 09:04 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
fishtoprecords, I'm totally confusing with your comments. You advice not to use double because of decimal points? Is it?
- 10-14-2008, 09:25 AM #13
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.
- 10-14-2008, 09:29 AM #14
See example code in
http://www.java-forums.org/new-java/...html#post38681
- 10-14-2008, 11:12 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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.
- 10-14-2008, 07:17 PM #16
Similar Threads
-
[Montreal] Recruiting Mobile Application Expert Developer
By montreal.hr in forum Jobs OfferedReplies: 0Last Post: 08-30-2008, 09:17 PM -
Want Some Expert Advice? JAX India 2008 Might Be the Right Place
By james in forum Java SoftwareReplies: 2Last Post: 03-03-2008, 09:05 PM
Bookmarks