|
|
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.
|
|

10-13-2008, 04:21 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
|
|
|
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.
|
|

10-13-2008, 04:24 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 883
|
|
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, 07:13 AM
|
 |
Senior Member
|
|
Join Date: Oct 2008
Location: Guadalajara, Mexico
Posts: 201
|
|
|
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?
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.
|
|

10-13-2008, 02:21 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
|
|
|
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, 03:27 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
|
|
Hi, guys I done my first java program...I am so happy
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.
|
|

10-14-2008, 03:50 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
|
|
You mean that you want to rounding a double number? You can easily rounding the number as follows.
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.
|
|

10-14-2008, 04:04 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
|
|
|
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, 04:54 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
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
|
|

10-14-2008, 07:06 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
|
|
Originally Posted by javaseek
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.
|
|

10-14-2008, 07:15 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
|
|
Using Math class you can do it so easily.
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.
|
|

10-14-2008, 09:18 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 475
|
|
Originally Posted by javaseek
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.
|
|

10-14-2008, 10:04 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
|
|
|
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.
|
|

10-14-2008, 10:25 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 475
|
|
Originally Posted by Eranga
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.
|
|

10-14-2008, 10:29 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 475
|
|
|
|
|

10-14-2008, 12:12 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
|
|
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.
|
|

10-14-2008, 08:17 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 475
|
|
|
You should run the code, its small, in the sample.
It clearly shows one of the many problems with using double/float for money
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|