Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 10-13-2008, 04:21 AM
Member
 
Join Date: Oct 2008
Posts: 4
javaseek is on a distinguished road
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 04:23 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-13-2008, 04:24 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 883
Fubarable is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-13-2008, 07:13 AM
CJSLMAN's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Guadalajara, Mexico
Posts: 201
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-13-2008, 02:21 PM
Member
 
Join Date: Oct 2008
Posts: 4
javaseek is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-14-2008, 03:27 AM
Member
 
Join Date: Oct 2008
Posts: 4
javaseek is on a distinguished road
Hi, guys I done my first java program...I am so happy

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 03:38 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-14-2008, 03:50 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You mean that you want to rounding a double number? You can easily rounding the number as follows.

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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-14-2008, 04:04 AM
Member
 
Join Date: Oct 2008
Posts: 4
javaseek is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-14-2008, 04:54 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-14-2008, 07:06 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-14-2008, 07:15 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Using Math class you can do it so easily.

Code:
double result = Math.round(double_value * Math.pow(10, 2)) / Math.pow(10, 2);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 10-14-2008, 09:18 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 10-14-2008, 10:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
fishtoprecords, I'm totally confusing with your comments. You advice not to use double because of decimal points? Is it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 10-14-2008, 10:25 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 10-14-2008, 10:29 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
See example code in
Using double for money is a sin
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 10-14-2008, 12:12 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 10-14-2008, 08:17 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
You should run the code, its small, in the sample.
It clearly shows one of the many problems with using double/float for money
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
[Montreal] Recruiting Mobile Application Expert Developer montreal.hr Jobs Offered 0 08-30-2008 10:17 PM
Want Some Expert Advice? JAX India 2008 Might Be the Right Place james Java Announcements 2 03-03-2008 10:05 PM


All times are GMT +3. The time now is 11:11 AM.


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