You are on page 1of 2

Gratuity

/*
* Filename: Gratuity.java
* Created by 1,000_naymes
* Purpose: to compute a gratuity and total when a user enters their subtotal &
gratuity rate
*/

import javax.swing.JOptionPane;
public class Gratuity
{
public static void main( String[] args )
{
/**************************************************************
declare and initialize variables
**************************************************************/
String openingMessage,
subtotalInputMessage,
rateInputMessage,
subtotalString,
rateString,
outputMessage;
double subtotal, rate, properRate, gratuity, total;
/**************************************************************
display opening message
******************************************y********************/
openingMessage = "Welcome to 1KN's gratuity program, which will calculate your
gratuity and total.\n";
JOptionPane.showMessageDialog( null, openingMessage );

/**************************************************************
input
**************************************************************/
subtotalInputMessage = "Please enter the subtotal for your meal: $";
subtotalString = JOptionPane.showInputDialog( subtotalInputMessage );
subtotal = Double.parseDouble( subtotalString ); //converting string to
double
rateInputMessage = "Please enter your gratuity rate as an integer (do not
include %, \nfor example 5.25 or 6): ";
rateString = JOptionPane.showInputDialog( rateInputMessage);
rate = Double.parseDouble( rateString ); //converting string to
double
properRate = rate / 100;

/**************************************************************
processing
**************************************************************/
// calculating the gratuity
gratuity = properRate * subtotal;
// calculating the total
total = subtotal + gratuity;
/**************************************************************
output
**************************************************************/
outputMessage = "You entered a subtotal of $" + subtotal + "\n and a gratuity
rate of " +
rate + "%. " + "\n The gratuity for your meal is $" + gratuity
+ "\n and your total is $" + total + ".";
Page 1
Gratuity
JOptionPane.showMessageDialog( null, outputMessage );

System.exit(0);
} //end main
} //end class

Page 2