943,587 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 790
  • Java RSS
Sep 27th, 2008
0

Need a little help

Expand Post »
Hello,

I am trying to get my cost, salestax, totalamount to come out in the output dialogbox to have only two decimals places. I know how to do on the printf but not on the dialogbox. Please help. Here in my complete code.

Java Syntax (Toggle Plain Text)
  1.  
  2. // Displaying multiple strings
  3. import javax.swing.*; // to use JOptionPane.show InputDialog box
  4.  
  5. public class Assignment3
  6. {
  7. public static void main( String args[] )
  8. {
  9. final double taxes = 0.0825; // rate for taxes calculation (constant)
  10.  
  11. int
  12. moreInt = 1, // loop control variable
  13. days = 0; // the number of days wanted
  14.  
  15. double
  16. totalPrice = 0, // the sum of calculated rental days
  17. salesTax = 0, // the calculated sales tax
  18. cost = 0, // fixed cost on the amount of rental days
  19. totalAmount = 0; // the total amount for rental days
  20.  
  21. String numberDays; // number of days
  22. String output = ""; // total rental message
  23. String input = ""; // users input
  24.  
  25.  
  26. // process unknown number of days, boolean controled loop
  27. while ( moreInt == 1 ) {
  28. numberDays = JOptionPane.showInputDialog(
  29. "Enter the number of rental days" );
  30.  
  31. // convert numbers from type String
  32. days = Integer.parseInt( numberDays );
  33.  
  34.  
  35. // to determine the cost on the rental days
  36. if( days >= 1 && days <= 3)
  37. {
  38. cost = 35.00;
  39. }
  40. else if( days >= 4 && days <= 7 )
  41. {
  42. cost = 30.00;
  43. }
  44. else if( days >= 8)
  45. {
  46. cost = 26.00;
  47. }
  48.  
  49.  
  50. // calculations
  51. totalAmount = days * cost;
  52. salesTax = totalAmount * taxes;
  53. totalPrice = totalAmount + salesTax;
  54.  
  55.  
  56. // message total output
  57. JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
  58. "\n------------------------------" +
  59. "\n" + days + " days $" + cost + " per day " + totalAmount +
  60. "\nSales taxes @ 8.25% $ " + salesTax +
  61. "\nTotal Price $ " + totalPrice +
  62. " \n \n "));
  63.  
  64. // display the "Thank you" message
  65. if (totalPrice < 100)
  66. output = "Thank you - We hope to see you again!";
  67. else if (totalPrice > 100)
  68. output = "Thank you - We appreciate your business!";
  69.  
  70. // display message total
  71. JOptionPane.showMessageDialog( null, output );
  72.  
  73. // display the "Another Rental" message
  74. input = JOptionPane.showInputDialog(
  75. "Another rental?\n 1 - yes\n 2 - no\n" );
  76.  
  77. moreInt = Integer.parseInt( input );
  78.  
  79. } //while loop ends
  80.  
  81.  
  82.  
  83. } // end method main
  84.  
  85.  
  86. } // end method Assignment3
Last edited by countrygirl1970; Sep 27th, 2008 at 1:42 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
countrygirl1970 is offline Offline
23 posts
since Feb 2007
Sep 27th, 2008
0

Re: Need a little help

Hello,

I am trying to get my cost, salestax, totalamount to come out in the output dialogbox to have only two decimals places. I know how to do on the printf but not on the dialogbox. Please help. Here in my complete code.

Java Syntax (Toggle Plain Text)
  1.  
  2. // Displaying multiple strings
  3. import javax.swing.*; // to use JOptionPane.show InputDialog box
  4.  
  5. public class Assignment3
  6. {
  7. public static void main( String args[] )
  8. {
  9. final double taxes = 0.0825; // rate for taxes calculation (constant)
  10.  
  11. int
  12. moreInt = 1, // loop control variable
  13. days = 0; // the number of days wanted
  14.  
  15. double
  16. totalPrice = 0, // the sum of calculated rental days
  17. salesTax = 0, // the calculated sales tax
  18. cost = 0, // fixed cost on the amount of rental days
  19. totalAmount = 0; // the total amount for rental days
  20.  
  21. String numberDays; // number of days
  22. String output = ""; // total rental message
  23. String input = ""; // users input
  24.  
  25.  
  26. // process unknown number of days, boolean controled loop
  27. while ( moreInt == 1 ) {
  28. numberDays = JOptionPane.showInputDialog(
  29. "Enter the number of rental days" );
  30.  
  31. // convert numbers from type String
  32. days = Integer.parseInt( numberDays );
  33.  
  34.  
  35. // to determine the cost on the rental days
  36. if( days >= 1 && days <= 3)
  37. {
  38. cost = 35.00;
  39. }
  40. else if( days >= 4 && days <= 7 )
  41. {
  42. cost = 30.00;
  43. }
  44. else if( days >= 8)
  45. {
  46. cost = 26.00;
  47. }
  48.  
  49.  
  50. // calculations
  51. totalAmount = days * cost;
  52. salesTax = totalAmount * taxes;
  53. totalPrice = totalAmount + salesTax;
  54.  
  55.  
  56. // message total output
  57. JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
  58. "\n------------------------------" +
  59. "\n" + days + " days $" + cost + " per day " + totalAmount +
  60. "\nSales taxes @ 8.25% $ " + salesTax +
  61. "\nTotal Price $ " + totalPrice +
  62. " \n \n "));
  63.  
  64. // display the "Thank you" message
  65. if (totalPrice < 100)
  66. output = "Thank you - We hope to see you again!";
  67. else if (totalPrice > 100)
  68. output = "Thank you - We appreciate your business!";
  69.  
  70. // display message total
  71. JOptionPane.showMessageDialog( null, output );
  72.  
  73. // display the "Another Rental" message
  74. input = JOptionPane.showInputDialog(
  75. "Another rental?\n 1 - yes\n 2 - no\n" );
  76.  
  77. moreInt = Integer.parseInt( input );
  78.  
  79. } //while loop ends
  80.  
  81.  
  82.  
  83. } // end method main
  84.  
  85.  
  86. } // end method Assignment3

use DecimalFormat

Here's an example:

http://www.dreamincode.net/code/snippet399.htm

Just set up a DecimalFormat object as in the example:

Java Syntax (Toggle Plain Text)
  1. DecimalFormat money = new DecimalFormat("$0.00");

and put it in your code (see red below):
JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
"\n------------------------------" +
"\n" + days + " days $" + cost + " per day " + totalAmount +
"\nSales taxes @ 8.25% $ " + money.format (salesTax) + 
"\nTotal Price $ " + totalPrice + 
" \n \n "));
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 27th, 2008
0

Re: Need a little help

Thank you for your help. I was able to fix my program.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
countrygirl1970 is offline Offline
23 posts
since Feb 2007
Sep 29th, 2008
0

Re: Need a little help

countrygirl1970

hi countrygirl i am giving you the right method to do it



import javax.swing.*; // to use JOptionPane.show InputDialog box

public class Assignment3
{
public static void main( String args[] )
{
final double taxes = 0.0825; // rate for taxes calculation (constant)

int
moreInt = 1, // loop control variable
days = 0, // the number of days wanted
Ta;
double
totalPrice = 0, // the sum of calculated rental days
salesTax = 0, // the calculated sales tax
cost = 0, // fixed cost on the amount of rental days
totalAmount = 0, // the total amount for rental days
tr;
String numberDays; // number of days
String output = ""; // total rental message
String input = ""; // users input


// process unknown number of days, boolean controled loop
while ( moreInt == 1 ) {
numberDays = JOptionPane.showInputDialog(
"Enter the number of rental days" );

// convert numbers from type String
days = Integer.parseInt( numberDays );


// to determine the cost on the rental days
if( days >= 1 && days <= 3)
{
cost = 35.00;
}
else if( days >= 4 && days <= 7 )
{
cost = 30.00;
}
else if( days >= 8)
{
cost = 26.00;
}


// calculations
totalAmount = days * cost;
salesTax = totalAmount * taxes;
totalPrice = totalAmount + salesTax;
tr = Math.IEEEremainder(totalPrice,1);
tr = tr*100;
tr = Math.rint(tr);
tr= tr*.01;
totalPrice = Math.rint(totalPrice) + tr;




// message total output
JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
"\n------------------------------" +
"\n" + days + " days $" + cost + " per day " + totalAmount +
"\nSales taxes @ 8.25% $ " + salesTax +
"\nTotal Price $ " + totalPrice +
" \n \n "));

// display the "Thank you" message
if (totalPrice < 100)
output = "Thank you - We hope to see you again!";
else if (totalPrice > 100)
output = "Thank you - We appreciate your business!";

// display message total
JOptionPane.showMessageDialog( null, output );

// display the "Another Rental" message
input = JOptionPane.showInputDialog(
"Another rental?\n 1 - yes\n 2 - no\n" );

moreInt = Integer.parseInt( input );

} //while loop ends



} // end method main


} // end method Assignment3
Reputation Points: 10
Solved Threads: 1
Newbie Poster
seemant gupta is offline Offline
7 posts
since May 2008
Sep 29th, 2008
0

Re: Need a little help

countrygirl1970

hi countrygirl i am giving you the right method to do it



import javax.swing.*; // to use JOptionPane.show InputDialog box

public class Assignment3
{
public static void main( String args[] )
{
final double taxes = 0.0825; // rate for taxes calculation (constant)

int
moreInt = 1, // loop control variable
days = 0, // the number of days wanted
Ta;
double
totalPrice = 0, // the sum of calculated rental days
salesTax = 0, // the calculated sales tax
cost = 0, // fixed cost on the amount of rental days
totalAmount = 0, // the total amount for rental days
tr;
String numberDays; // number of days
String output = ""; // total rental message
String input = ""; // users input


// process unknown number of days, boolean controled loop
while ( moreInt == 1 ) {
numberDays = JOptionPane.showInputDialog(
"Enter the number of rental days" );

// convert numbers from type String
days = Integer.parseInt( numberDays );


// to determine the cost on the rental days
if( days >= 1 && days <= 3)
{
cost = 35.00;
}
else if( days >= 4 && days <= 7 )
{
cost = 30.00;
}
else if( days >= 8)
{
cost = 26.00;
}


// calculations
totalAmount = days * cost;
salesTax = totalAmount * taxes;
totalPrice = totalAmount + salesTax;
tr = Math.IEEEremainder(totalPrice,1);
tr = tr*100;
tr = Math.rint(tr);
tr= tr*.01;
totalPrice = Math.rint(totalPrice) + tr;




// message total output
JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
"\n------------------------------" +
"\n" + days + " days $" + cost + " per day " + totalAmount +
"\nSales taxes @ 8.25% $ " + salesTax +
"\nTotal Price $ " + totalPrice +
" \n \n "));

// display the "Thank you" message
if (totalPrice < 100)
output = "Thank you - We hope to see you again!";
else if (totalPrice > 100)
output = "Thank you - We appreciate your business!";

// display message total
JOptionPane.showMessageDialog( null, output );

// display the "Another Rental" message
input = JOptionPane.showInputDialog(
"Another rental?\n 1 - yes\n 2 - no\n" );

moreInt = Integer.parseInt( input );

} //while loop ends



} // end method main


} // end method Assignment3
the wisdom of the ignorant????
1. the problem was fixed the day before you posted this
2. you are just formatting the data, with the chance you'll either loose data or have to write lots more code. take a look at the sollution provided by VernonDozier, and you'll see that your sollution sure is not the best in the world
Reputation Points: 919
Solved Threads: 353
Nearly a Posting Maven
stultuske is online now Online
2,473 posts
since Jan 2007
Sep 29th, 2008
0

Re: Need a little help

Thank you all for the help. I was able to fix the problem and submitted the program. I received an A.

Thank you again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
countrygirl1970 is offline Offline
23 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: please enlighten me on what i need to do to get this right
Next Thread in Java Forum Timeline: Help with implementing a Loader[Operating Systems]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC