In Java, there's usually more than one way to do even the simplest of problems. I tried thinking of an easier way to do this, but really couldn't think of anything. o_o
Here's what I have (2 different files)...
Code:
public class test21Main{
public static void main(String [] args) throws NumberFormatException{
test21 testObject = new test21();
testObject.getSubtotal();
testObject.getPercent();
testObject.calculateTip();
testObject.displayResults();
}
}
Code:
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);
}
}
First off, sorry for it being so messy. I'm not big on keeping it neat as I go along as you can see.
Feel free to change the names of variables, classes, etc. Just be sure to change this bit of code
Code:
[b]test21 testObject = new test21();[/b]
testObject.getSubtotal();
testObject.getPercent();
testObject.calculateTip();
testObject.displayResults();
You're creating an object of a class (the second, longer file), so you'll need to update that line with the name of the file.
1) Did you want the user to have the option of either entering a number or a number with a percentage sign (ex: 15 or 15%)? The way it's set up, it requires the user to enter it in the format of a percentage sign (15%).
2)
Code:
try{
subtotal = Float.parseFloat(input);
}
catch(NumberFormatException nfe){
//insert error message here
getSubtotal();
}
This is the only way I thought of doing this. You can use try/catch to catch the specific exception it gives you in the command window (in this case, it's the NumberFormatException). I believe the problem with the if statement in this case is that you'll have to make many if statements to satisfy all of the possible inputs your program can get.
3) DecimalFormat df = new DecimalFormat("0.00");
When applying it to doubles/floats, use df.format(DOUBLE/FLOAT);