This is the question of the program:
Write a Java Application program that will determine the gross pay for each of several employees (Use either ‘while’, ‘for’, or ‘do/while’ loop to input several employees). The company pays "straight time" for the first 40 hours worked by each employee and pays "time-and-a-half" for all hours worked in excess of 40 hours. Your program should ask the user to input the hours worked and the rate per hour. Put all output in one window. Format your gross pay in two decimal places.
This is what I have coded:
/*
* This is a program that determines the gross pay for employees of a
* company that pays "straight time" for the first 40 hours and "time
* and a half" for all hours excess. The program asks the user to
* input the hours worked and the rate per hour.
*/
// Imports.
import java.text.DecimalFormat;
import javax.swing.*;
public class SalaryCalculator
{
public static void main( String[] args )
{
// Ask for number of employees.
String num = JOptionPane.showInputDialog( "How many employees?" );
int employees = Integer.parseInt( num );
for (int counter = 0; counter != employees; counter++)
{
// Ask for employee info.
String name = JOptionPane.showInputDialog ( "Enter employee name:" );
String rate = JOptionPane.showInputDialog( "Enter standard rate per hour in $:" );
String hours = JOptionPane.showInputDialog( "Enter number of hours worked:" );
// Convert string to double.
double money = Double.parseDouble( rate );
double time = Double.parseDouble( hours );
// Create number format and text area.
DecimalFormat noDecimal = new DecimalFormat( "0.00" );
JTextArea outputArea = new JTextArea( 30, 30 );
// Format text area.
outputArea.append( "Name \t Rate \t Hours \t Total \n\n" );
// Calculations and all.
if ( time <= 40 )
{
outputArea.append( name + "\t" + "$"
+ money + "\t"
+ hours + "\t" + "$"
+ noDecimal.format( money * time ) + "\n" );
}
else if( time > 40 )
{
outputArea.append( name + "\t" + "$"
+ money + "\t"
+ hours + "\t" + "$"
+ noDecimal.format( ( money * 1.5 ) * time ) + "\n" );
}
// Output.
JOptionPane.showConfirmDialog( null,
outputArea, "Gross Pay", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE );
}
}
}
When the code is executed, it asks for the number of employees. Say I enter 2, for example. The program then proceeds to ask the employee name, pay per hour, and number of hours worked. When I enter all of these, it shows the output nicely in a window like I want. Picture: Data set 1. After that, it again asks for employee name, pay, and hours (since I said there are 2 employees). This is where my problem is. When I input the second set of data, it replaces the first in the result window. Picture: Data set 2 replaces set 1. I want it to be displayed in a new line below the first on the same window, without having the first omitted.
This is my first time posting on stack overflow. Any help will be greatly appreciated!
You're creating a new JTextArea with each iteration of the loop. Don't do that. Create the JTextArea before the loop, and use the same one.