/** * File name: WageCalculatorCUI.java * Author: Hongyan Wang * converted to Scanner for class demo: David Wills * * 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 SavitchIn for input. */ import java.text.*; // to use DecimalFormat import java.util.*; //Scanner public class WageCalculator { // 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 Scanner input = new Scanner(System.in); while (true) { // Get info. System.out.println ("Enter pay rate (enter 0 or a negative number to quit): "); payRate = input.nextDouble (); if (payRate <= 0.0) break; // end of all employees; exit the loop System.out.println ("Enter hours worked: "); hours = input.nextDouble (); // 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"); System.out.println ("\nThe wage should be " + d2.format (wages) + "\n"); } } }