Sat Sep 17, 2005 7:45 pm
float subtotal;
float tip;
float total;
float percent;
String input;
//enter bill subtotal
input = JOptionPane.showInputDialog (null, "Enter amount of bill",
"Amount of Bill Before Tip", JOptionPane.QUESTION_MESSAGE);
//convert the total
subtotal = Float.parseFloat(input);
//enter desired percentage ie 15% or 20% to give server
input = JOptionPane.showInputDialog(null, "Enter the percentage you desire to tip the server \n (15% for normal service 25% for extraordinary)",
"Enter Percentage (exclude % sign)", JOptionPane.QUESTION_MESSAGE);
//convert the percentage
percent = Float.parseFloat (input);
//calculate tip
percent = (percent / 100);
tip = percent * subtotal;
total = tip + subtotal;
//display a final results broken down
System.out.println("your subtotal\t= " +subtotal);
System.out.println("your percent\t= " +percent);
System.out.println("your tip\t= " +tip);
System.out.println("your total bill\t= " +total);
String output = "Your total " + total + " consists of \n" +
subtotal + "subtotal\n" +
tip + "tip\n" ;
JOptionPane.showMessageDialog(null, output,
"Breakdown", JOptionPane.INFORMATION_MESSAGE);
Sat Sep 17, 2005 10:31 pm
public class test21Main{
public static void main(String [] args) throws NumberFormatException{
test21 testObject = new test21();
testObject.getSubtotal();
testObject.getPercent();
testObject.calculateTip();
testObject.displayResults();
}
}
import javax.swing.*;
import java.io.*;
import java.text.*;
public class test21{
float subtotal;
float tip;
float total;
float percent;
String input;
DecimalFormat df = new DecimalFormat("0.00");
//enter bill subtotal
public void getSubtotal(){
input = JOptionPane.showInputDialog (null, "Enter amount of bill",
"Amount of Bill Before Tip", JOptionPane.QUESTION_MESSAGE);
//convert the total
try{
subtotal = Float.parseFloat(input);
}
catch(NumberFormatException nfe){
//insert error message here
getSubtotal();
}
//enter desired percentage ie 15% or 20% to give server
}
public void getPercent(){
input = JOptionPane.showInputDialog(null, "Enter the percentage you desire to tip the server \n (15% for normal service 25% for extraordinary)",
"Enter Percentage (exclude % sign)", JOptionPane.QUESTION_MESSAGE);
if(input.endsWith("%")){
while(input.endsWith("%")){
input = input.substring(0, input.length()-1);
}
}
else{
//give error message here
getPercent();
}
//convert the percentage
percent = Float.parseFloat (input);
}
public void calculateTip(){
//calculate tip
percent = (percent / 100);
tip = percent * subtotal;
total = tip + subtotal;
}
public void displayResults(){
//display a final results broken down
System.out.println("your subtotal\t= " +df.format(subtotal));
System.out.println("your percent\t= " +df.format(percent*100));
System.out.println("your tip\t= " +df.format(tip));
System.out.println("your total bill\t= " +df.format(total));
String output = "Your total " + df.format(total) + " consists of \n" +
df.format(subtotal) + "subtotal\n" +
df.format(tip) + "tip\n" ;
JOptionPane.showMessageDialog(null, output,
"Breakdown", JOptionPane.INFORMATION_MESSAGE);
}
}
[b]test21 testObject = new test21();[/b]
testObject.getSubtotal();
testObject.getPercent();
testObject.calculateTip();
testObject.displayResults();
try{
subtotal = Float.parseFloat(input);
}
catch(NumberFormatException nfe){
//insert error message here
getSubtotal();
}
Sun Sep 18, 2005 1:35 am