Need a little help

Thread Solved
Reply

Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster

Need a little help

 
0
  #1
Sep 27th, 2008
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,756
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need a little help

 
0
  #2
Sep 27th, 2008
Originally Posted by countrygirl1970 View 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.

  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:

  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 "));
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster

Re: Need a little help

 
0
  #3
Sep 27th, 2008
Thank you for your help. I was able to fix my program.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: seemant gupta is an unknown quantity at this point 
Solved Threads: 1
seemant gupta seemant gupta is offline Offline
Newbie Poster

Re: Need a little help

 
0
  #4
Sep 29th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Need a little help

 
0
  #5
Sep 29th, 2008
Originally Posted by seemant gupta View Post
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster

Re: Need a little help

 
0
  #6
Sep 29th, 2008
Thank you all for the help. I was able to fix the problem and submitted the program. I received an A.

Thank you again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC