- import java.text.DecimalFormat;
-
import javax.swing.JOptionPane;
-
-
public class PayrollSystemTest {
-
-
public static void main( String[] args )
-
{
-
String workerType;
-
String first;
-
String last;
-
String ssn;
-
int month;
-
int day;
-
int year;
-
float salary;
-
float rate;
-
float hourlyWage;
-
float hours;
-
-
float sales;
-
-
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
-
-
// create Employee array
-
Employee employees[] = new Employee[ 5 ];
-
-
// generically process each element in array employees
-
for ( int empNo = 1; empNo <=5; empNo++ ) {
-
-
-
-
-
workerType=JOptionPane.showInputDialog(
-
"Please enter the type of worker:\n\n" +
-
"Salaried\nHourly\nCommission\n" +
-
"Base Plus Commission");
-
-
-
if(workerType.equalsIgnoreCase("Salaried")){
-
first=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" first name: ");
-
last=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" last name: ");
-
ssn=JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " ssn: ");
-
month=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth month(1-12): "));
-
day=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth day(1-31): "));
-
year=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth year(yyyy): "));
-
salary=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " weekly salary: "));
-
employees[empNo-1] = new SalariedEmployee(first, last,
-
ssn, month, day, year, salary);
-
}
-
-
-
else if(workerType.equalsIgnoreCase("Hourly")){
-
first=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" first name: ");
-
last=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" last name: ");
-
ssn=JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " ssn: ");
-
month=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth month(1-12): "));
-
day=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth day(1-31): "));
-
year=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth year(yyyy): "));
-
hourlyWage=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " hourly wage: "));
-
hours=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " hours worked: "));
-
employees[empNo-1] = new HourlyEmployee(first, last, ssn,
-
month, day, year, hourlyWage, hours);
-
}
-
-
-
else if(workerType.equalsIgnoreCase("Commission")){
-
first=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" first name: ");
-
last=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" last name: ");
-
ssn=JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " ssn: ");
-
month=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth month(1-12): "));
-
day=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth day(1-31): "));
-
year=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth year(yyyy): "));
-
sales=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " weekly sales: "));
-
rate=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " commission rate: "));
-
employees[empNo-1] = new CommissionEmployee(first, last,
-
ssn, month, day, year, sales, rate);
-
}
-
-
-
else if(workerType.equalsIgnoreCase("Base Plus Commission")){
-
first=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" first name: ");
-
last=JOptionPane.showInputDialog("Enter employee " + empNo +
-
" last name: ");
-
ssn=JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " ssn: ");
-
month=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth month(1-12): "));
-
day=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth day(1-31): "));
-
year=Integer.parseInt(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " birth year(yyyy): "));
-
sales=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " weekly sales: "));
-
rate=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " commission rate: "));
-
salary=Float.parseFloat(JOptionPane.showInputDialog(
-
"Enter employee " + empNo + " base salary: "));
-
employees[2] =
-
new BasePlusCommissionEmployee(first, last, ssn, month,
-
day, year, sales, rate, salary);
-
}
-
-
-
-
}
-
-
-
// initialize array with Employees
-
//employees[ 0 ] = new SalariedEmployee( "John", "Smith",
-
// "111-11-1111", 1000, 1, 1, 1960 );
-
//employees[ 1 ] = new CommissionEmployee( "Sue", "Jones",
-
// "222-22-2222", 12000, .05, 2, 1, 1962 );
-
//employees[ 2 ] = new BasePlusCommissionEmployee( "Bob", "Lewis",
-
// "333-33-3333", 10000, .03, 500, 3, 1, 1963 );
-
//employees[ 3 ] = new HourlyEmployee( "Karen", "Price",
-
// "444-44-4444", 20., 40, 4, 1, 1964 );
-
//employees[ 4 ] = new SalariedEmployee( "Bruce", "Chiesa",
-
// "555-55-5555", 1000, 11, 1, 1961 );
-
-
String output ="";
-
-
for(int i=1; i<employees.length+1; i++){
-
output += employees[i-1].toString();
-
-
// determine whether element is a BasePlusCommissionEmployee
-
if ( employees[ i-1 ] instanceof BasePlusCommissionEmployee ) {
-
-
// downcast Employee reference to
-
// BasePlusCommissionEmployee reference
-
BasePlusCommissionEmployee currentEmployee =
-
( BasePlusCommissionEmployee ) employees[ i-1 ];
-
-
double oldBaseSalary = currentEmployee.getBaseSalary();
-
output += "\nold base salary: $" + oldBaseSalary;
-
-
currentEmployee.setBaseSalary( 1.10 * oldBaseSalary );
-
output += "\nnew base salary with 10% increase is: $" +
-
currentEmployee.getBaseSalary();
-
-
} // end if
-
-
output += "\nearned $" + employees[ i-1 ].earnings()*4 + "\n";
-
-
} // end for
-
-
// get type name of each object in employees array
-
for ( int j = 1; j < employees.length+1; j++ )
-
output += "\nEmployee " + j + " is a " +
-
employees[ j-1 ].getClass().getName();
-
-
JOptionPane.showMessageDialog( null, output ); // display output
-
System.exit( 0 );
-
-
} // end main
-
-
} // end class PayrollSystemTest
-
-
error is on this line on 148 - output += employees[i-1].toString();
3 2590 Laharl 849
Recognized Expert Contributor
i goes from 1 to employees.lengt h+1, so i-1 goes from 0->employees.leng th. However, because arrays are zero-indexed, employees[employees.lengt h] is the employees.lengt h+1'th item. Thus, you're outside your array. Quite frankly, I'm surprised you got a NullPointerExce ption, as you should probably have gotten an ArrayIndexOutOf BoundsException first when it tries to resolve the element.
How do I fix it? Do I make i=0?
JosAH 11,448
Recognized Expert MVP
How do I fix it? Do I make i=0?
See if there are actual employees in your array: -
for (int i= 0; i < employees.length; i++)
-
System.out.println(i+": "+employees[i]);
-
If there is an employee present at slot i something will be printed, otherwise just
'null' will be printed. You attempt to override the toString() methond so something
sensible should be printed if an employee is printed.
Als note how the little loop above iterates over the array, i.e. there is no need to
start from 1 and subtract one from the index value again in the body of the loop.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Andy Howells |
last post by:
Can anybody help me on this? I am getting the below error but have not
got a clue why. The file in my classpath eing used has the class that
it says is not defined. Any ideas? I am running java version 1.4.0 and
WMQ Series version 5.3 with CSD04.
Exception in thread "main" java.lang.NoClassDefFoundError:
com/ibm/mq/server/MQSESSION
at com.ibm.mq.MQSESSIONServer.getMQSESSION(MQSESSIONServer.java:67)
at...
|
by: GHKASHYAP |
last post by:
Hi this is the exception i am getting when i am trying to run this application:
Exception in thread "main" java.lang.NullPointerException
thanks in advance..
package com.inventive.StockMarketTrading;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
|
by: jmitch89 |
last post by:
I don't why I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError
The statement below works just fine:
java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp
However, the statement below produces the error:
java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar" -jar CurrentStrobe.jar
Exception in thread "main"...
|
by: ohadr |
last post by:
hi,
i get Exception in thread "main" java.lang.NullPointerException
when i run my application.
the exact error is:
"Exception in thread "main" java.lang.NullPointerException
at sortmergejoin.MergeJoin.Field(MergeJoin.java:204)
at sortmergejoin.MergeJoin.SMJoin(MergeJoin.java:84)
at sortmergejoin.MergeJoin.<init>(MergeJoin.java:34)
at sortmergejoin.Main.main(Main.java:24)
Java Result: 1"
|
by: onlinegear |
last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro
run:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:365)
at orderingsystem.OrderingSystem.<init>(OrderingSystem.java:261)
at...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |