Payroll Program using GUI
I am suppose to modify the current payroll system program to include an additional method to enter data from the user, rather then just display on command screen. Assume the employees names, salary, base salary, commission rate, hourly rate are already entered into the system (from the database, for example).
* Salarid employee: No data to be entered.
* Commission employee: Enter the amount of gross sales.
* Base plus commission employee: Enter the amount of gross sales.
* Hourly employee: Enter the hours worked.
I am suppose to add an abstract method, e.g. enterData into the superclass Employee, and override it in each of its subclasses. To test, create an array of Employees. Loop through the array to enter data. Then loop through the array again to calculate and output their earnings. but i don't know how... here are the completed super class and subclasses so far.
This is what I have so far,
Code:
001
public class PayrollSystemTest
002
{
003
public static void main( String args[] )
004
{
005
// create subclass objects
006
SalariedEmployee salariedEmployee =
007
new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
008
HourlyEmployee hourlyEmployee =
009
new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
010
CommissionEmployee commissionEmployee =
011
new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000, .06 );
012
BasePlusCommissionEmployee basePlusCommissionEmployee =
013
new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
014
015
System.out.println( "Employees processed individually:\n" );
016
017
System.out.printf( "%s\n%s: $%,.2f\n\n",
018
salariedEmployee, "earned", salariedEmployee.earnings() );
019
System.out.printf( "%s\n%s: $%,.2f\n\n",
020
hourlyEmployee, "earned", hourlyEmployee.earnings() );
021
System.out.printf( "%s\n%s: $%,.2f\n\n",
022
commissionEmployee, "earned", commissionEmployee.earnings() );
023
System.out.printf( "%s\n%s: $%,.2f\n\n",
024
basePlusCommissionEmployee,
025
"earned", basePlusCommissionEmployee.earnings() );
026
027
// create four-element Employee array
028
Employee employees[] = new Employee[ 4 ];
029
030
// initialize array with Employees
031
employees[ 0 ] = salariedEmployee;
032
employees[ 1 ] = hourlyEmployee;
033
employees[ 2 ] = commissionEmployee;
034
employees[ 3 ] = basePlusCommissionEmployee;
035
036
System.out.println( "Employees processed polymorphically:\n" );
037
038
// generically process each element in array employees
039
for ( Employee currentEmployee : employees )
040
{
041
System.out.println( currentEmployee ); // invokes toString
042
043
// determine whether element is a BasePlusCommissionEmployee
044
if ( currentEmployee instanceof BasePlusCommissionEmployee )
045
{
046
// downcast Employee reference to
047
// BasePlusCommissionEmployee reference
048
BasePlusCommissionEmployee employee =
049
( BasePlusCommissionEmployee ) currentEmployee;
050
051
double oldBaseSalary = employee.getBaseSalary();
052
employee.setBaseSalary( 1.10 * oldBaseSalary );
053
System.out.printf(
054
"new base salary with 10%% increase is: $%,.2f\n",
055
employee.getBaseSalary() );
056
} // end if
057
058
System.out.printf(
059
"earned $%,.2f\n\n", currentEmployee.earnings() );
060
} // end for
061
062
// get type name of each object in employees array
063
for ( int j = 0; j < employees.length; j++ )
064
System.out.printf( "Employee %d is a %s\n", j,
065
employees[ j ].getClass().getName() );
066
} // end main
067
} // end class PayrollSystemTest
068
069
_____________________________________
070
071
public abstract class Employee
072
{
073
private String firstName;
074
private String lastName;
075
private String socialSecurityNumber;
076
077
// three-argument constructor
078
public Employee( String first, String last, String ssn )
079
{
080
firstName = first;
081
lastName = last;
082
socialSecurityNumber = ssn;
083
} // end three-argument Employee constructor
084
085
// set first name
086
public void setFirstName( String first )
087
{
088
firstName = first;
089
} // end method setFirstName
090
091
// return first name
092
public String getFirstName()
093
{
094
return firstName;
095
} // end method getFirstName
096
097
// set last name
098
public void setLastName( String last )
099
{
100
lastName = last;
101
} // end method setLastName
102
103
// return last name
104
public String getLastName()
105
{
106
return lastName;
107
} // end method getLastName
108
109
// set social security number
110
public void setSocialSecurityNumber( String ssn )
111
{
112
socialSecurityNumber = ssn; // should validate
113
} // end method setSocialSecurityNumber
114
115
// return social security number
116
public String getSocialSecurityNumber()
117
{
118
return socialSecurityNumber;
119
} // end method getSocialSecurityNumber
120
121
// return String representation of Employee object
122
public String toString()
123
{
124
return String.format( "%s %s\nsocial security number: %s",
125
getFirstName(), getLastName(), getSocialSecurityNumber() );
126
} // end method toString
127
128
// abstract method overridden by subclasses
129
public abstract double earnings(); // no implementation here
130
} // end abstract class Employee
131
132
__________________________________
133
134
public class SalariedEmployee extends Employee
135
{
136
private double weeklySalary;
137
138
// four-argument constructor
139
public SalariedEmployee( String first, String last, String ssn,
140
double salary )
141
{
142
super( first, last, ssn ); // pass to Employee constructor
143
setWeeklySalary( salary ); // validate and store salary
144
} // end four-argument SalariedEmployee constructor
145
146
// set salary
147
public void setWeeklySalary( double salary )
148
{
149
weeklySalary = salary < 0.0 ? 0.0 : salary;
150
} // end method setWeeklySalary
151
152
// return salary
153
public double getWeeklySalary()
154
{
155
return weeklySalary;
156
} // end method getWeeklySalary
157
158
// calculate earnings; override abstract method earnings in Employee
159
public double earnings()
160
{
161
return getWeeklySalary();
162
} // end method earnings
163
164
// return String representation of SalariedEmployee object
165
public String toString()
166
{
167
return String.format( "salaried employee: %s\n%s: $%,.2f",
168
super.toString(), "weekly salary", getWeeklySalary() );
169
} // end method toString
170
} // end class SalariedEmployee
171
172
__________________________________
173
174
public class HourlyEmployee extends Employee
175
{
176
private double wage; // wage per hour
177
private double hours; // hours worked for week
178
179
// five-argument constructor
180
public HourlyEmployee( String first, String last, String ssn,
181
double hourlyWage, double hoursWorked )
182
{
183
super( first, last, ssn );
184
setWage( hourlyWage ); // validate and store hourly wage
185
setHours( hoursWorked ); // validate and store hours worked
186
} // end five-argument HourlyEmployee constructor
187
188
// set wage
189
public void setWage( double hourlyWage )
190
{
191
wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
192
} // end method setWage
193
194
// return wage
195
public double getWage()
196
{
197
return wage;
198
} // end method getWage
199
200
// set hours worked
201
public void setHours( double hoursWorked )
202
{
203
hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
204
hoursWorked : 0.0;
205
} // end method setHours
206
207
// return hours worked
208
public double getHours()
209
{
210
return hours;
211
} // end method getHours
212
213
// calculate earnings; override abstract method earnings in Employee
214
public double earnings()
215
{
216
if ( getHours() <= 40 ) // no overtime
217
return getWage() * getHours();
218
else
219
return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
220
} // end method earnings
221
222
// return String representation of HourlyEmployee object
223
public String toString()
224
{
225
return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f",
226
super.toString(), "hourly wage", getWage(),
227
"hours worked", getHours() );
228
} // end method toString
229
} // end class HourlyEmployee
230
231
_________________________________
232
233
public class CommissionEmployee extends Employee
234
{
235
private double grossSales; // gross weekly sales
236
private double commissionRate; // commission percentage
237
238
// five-argument constructor
239
public CommissionEmployee( String first, String last, String ssn,
240
double sales, double rate )
241
{
242
super( first, last, ssn );
243
setGrossSales( sales );
244
setCommissionRate( rate );
245
} // end five-argument CommissionEmployee constructor
246
247
// set commission rate
248
public void setCommissionRate( double rate )
249
{
250
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
251
} // end method setCommissionRate
252
253
// return commission rate
254
public double getCommissionRate()
255
{
256
return commissionRate;
257
} // end method getCommissionRate
258
259
// set gross sales amount
260
public void setGrossSales( double sales )
261
{
262
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
263
} // end method setGrossSales
264
265
// return gross sales amount
266
public double getGrossSales()
267
{
268
return grossSales;
269
} // end method getGrossSales
270
271
// calculate earnings; override abstract method earnings in Employee
272
public double earnings()
273
{
274
return getCommissionRate() * getGrossSales();
275
} // end method earnings
276
277
// return String representation of CommissionEmployee object
278
public String toString()
279
{
280
return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",
281
"commission employee", super.toString(),
282
"gross sales", getGrossSales(),
283
"commission rate", getCommissionRate() );
284
} // end method toString
285
} // end class CommissionEmployee
286
287
__________________________________
288
289
public class BasePlusCommissionEmployee extends CommissionEmployee
290
{
291
private double baseSalary; // base salary per week
292
293
// six-argument constructor
294
public BasePlusCommissionEmployee( String first, String last,
295
String ssn, double sales, double rate, double salary )
296
{
297
super( first, last, ssn, sales, rate );
298
setBaseSalary( salary ); // validate and store base salary
299
} // end six-argument BasePlusCommissionEmployee constructor
300
301
// set base salary
302
public void setBaseSalary( double salary )
303
{
304
baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative
305
} // end method setBaseSalary
306
307
// return base salary
308
public double getBaseSalary()
309
{
310
return baseSalary;
311
} // end method getBaseSalary
312
313
// calculate earnings; override method earnings in CommissionEmployee
314
public double earnings()
315
{
316
return getBaseSalary() + super.earnings();
317
} // end method earnings
318
319
// return String representation of BasePlusCommissionEmployee object
320
public String toString()
321
{
322
return String.format( "%s %s; %s: $%,.2f",
323
"base-salaried", super.toString(),
324
"base salary", getBaseSalary() );
325
} // end method toString
326
} // end class BasePlusCommissionEmployee
Re: Payroll Program using GUI
Don't post silly line numbers in your code. The days of GWBASIC are long gone.
So, You Need to Write a Program but Don't Know How to Start
db