/** * File name: WageCalculatorOptionPane.java * Author: Hongyan Wang. Modified by David Wills to use JOptionPane * * Description: The program accepts an employee's pay rate and number of hours * worked in a week, and calculates the wage the employee earned for that week. * * Others: The program has minimum error checking. It doesn't check the validity * of number of hours worked. The program uses JOptionPane for input. */ import java.text.*; // to use DecimalFormat import javax.swing.*; public class WageCalculatorOptionPane { // Constants private static final double MAX_HOURS = 40.0; // Maximum normal work hours private static final double OVERTIME = 1.5; // Overtime pay rate factor // main function public static void main (String[] args) { double payRate; // Employee's pay rate double hours; // Hours worked double wages; // Wages earned String input; while (true) { // Get info. input = JOptionPane.showInputDialog ("Enter pay rate (enter 0 or a negative number to quit): "); payRate = Double.parseDouble( input ); if (payRate <= 0.0) break; // end of all employees; exit the loop input = JOptionPane.showInputDialog ("Enter hours worked: "); hours = Double.parseDouble( input ); // Calculate wage based on pay rate and hours worked if (hours > MAX_HOURS) // Is there overtime? wages = (MAX_HOURS * payRate) + // Yes (hours - MAX_HOURS) * payRate * OVERTIME; else wages = hours * payRate; // No // Display the result DecimalFormat d2 = new DecimalFormat ("0.00"); JOptionPane.showMessageDialog (null, "The wage should be " + d2.format (wages)); } } }