Skip to content

Instantly share code, notes, and snippets.

@lb7n
Created September 11, 2014 22:06
Show Gist options
  • Save lb7n/576f6a21b4f3e9907922 to your computer and use it in GitHub Desktop.
Save lb7n/576f6a21b4f3e9907922 to your computer and use it in GitHub Desktop.
Service Level generates tip %
import javax.swing.*;
import javax.swing.text.StringContent;
public class mainClass {
public static void main(String[] args) {
double bill=0;
double tip=0;
int serviceLevel=0;
int periodCounter = 0;
String diningExperience = JOptionPane.showInputDialog(null, "Using numbers from 1 - 10 please rate your dining experience." +
"1 being very unsatisfied, 10 being extremely satisfied.");
if (diningExperience.contains(".")){
JOptionPane.showMessageDialog(null, "You must enter an INTEGER value");
System.exit(0);
}
try {
serviceLevel = Integer.parseInt(diningExperience);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "You must enter an INTEGER value, "+diningExperience+" is NOT an integer value");
System.exit(0);
}
if (serviceLevel > 10 || serviceLevel < 1){
JOptionPane.showMessageDialog(null, "You must enter an integer between 1 and 10");
System.exit(0);
}
String diningCost = JOptionPane.showInputDialog(null, "How much was the bill?");
for (int i = 0; i < diningCost.length(); i++) {
char buffer = diningCost.charAt(i);
if (buffer == '.') {
periodCounter++;
}
if (periodCounter > 1) {
JOptionPane.showMessageDialog(null, "Your bill must be a valid currency amount (this is not an IP address!)");
System.exit(0);
}
}
try {
bill = Double.parseDouble(diningCost);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "The bill you entered was NOT a number :(");
System.exit(0);
}
String formattedCost = String.format("%.2f", bill);
JOptionPane.showMessageDialog(null, "Your bill is " + formattedCost);
tip = 0;
if(serviceLevel < 5){
tip = .10 * bill;
}
if(serviceLevel >= 5){
tip = .15 * bill;
}
JOptionPane.showMessageDialog(null, "Based on your service level, your tip is " + tip);
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment