// 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
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)
// 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
DecimalFormat money = new DecimalFormat("$0.00");
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
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
| DaniWeb Message | |
| Cancel Changes | |