DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Need a little help (http://www.daniweb.com/forums/thread147889.html)

countrygirl1970 Sep 27th, 2008 1:39 am
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.


// Displaying multiple strings
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
                   
                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
                             
                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;
             
       
        // 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

VernonDozier Sep 27th, 2008 1:56 am
Re: Need a little help
 
Quote:

Originally Posted by countrygirl1970 (Post 699911)
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.


// Displaying multiple strings
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
                   
                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
                             
                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;
             
       
        // 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


use DecimalFormat

Here's an example:

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

Just set up a DecimalFormat object as in the example:

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 "));

countrygirl1970 Sep 27th, 2008 2:00 pm
Re: Need a little help
 
Thank you for your help. I was able to fix my program.

seemant gupta Sep 29th, 2008 7:19 am
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

stultuske Sep 29th, 2008 8:18 am
Re: Need a little help
 
Quote:

Originally Posted by seemant gupta (Post 701178)
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

countrygirl1970 Sep 29th, 2008 6:06 pm
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.


All times are GMT -4. The time now is 6:40 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC