Repository navigation
Expand file tree
/
Copy pathPieceWorker
More file actions
648 lines (531 loc) · 23.2 KB
/
Copy pathPieceWorker
File metadata and controls
648 lines (531 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//<PieceWorker> An employee whose pay
// is based on the number of pieces of
// merchandise produced
//CSIS312 - <Assignment3>
//Citations if necessary>--
package pieceworker;
/**
*
* @author lirby
*/
public class PieceWorker extends Employee{
private double wage = 0;
private int pieces = 0;
// constructor
public PieceWorker(String firstName, String lastName,
String socialSecurityNumber, double wage, int pieces)
{
super(firstName, lastName, socialSecurityNumber);
if (wage < 0.0)
throw new IllegalArgumentException(
"Wage must be >= 0.0");
if (pieces < 0)
throw new IllegalArgumentException(
"Pieces must be >= 0");
this.wage = wage;
this.pieces = pieces;
}
// set salary
public void setwage(double wage)
{
if (wage < 0.0)
throw new IllegalArgumentException(
"Wage must be >= 0.0");
this.wage = wage;
}
// set pieces
public void setpieces(int pieces)
{
if (pieces < 0)
throw new IllegalArgumentException(
"Pieces must be >= 0.0");
this.pieces = pieces;
}
// return salary
public double getwage()
{
return wage;
}
// return pieces
public int getpieces()
{
return pieces;
}
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
double earnings = pieces * wage;
return getpieces();
}
// return String representation of SalariedEmployee object
@Override
public String toString()
{
return String.format("salaried employee: %s%n%s: $%,.2f",
super.toString(), "wage", getwage());
}
}
-----------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pieceworker;
// Fig. 10.4: Employee.java
// Employee abstract superclass.
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
// constructor
public Employee(String firstName, String lastName,
String socialSecurityNumber)
{
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}
// return first name
public String getFirstName()
{
return firstName;
}
// return last name
public String getLastName()
{
return lastName;
}
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
}
Employee employees[] = new Employee[5];
// return String representation of Employee object
@Override
public String toString()
{
return String.format("%s %s%nsocial security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber());
}
// abstract method must be overridden by concrete subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
---------------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pieceworker;
/**
*
* @author lirby
*/
// Fig. 10.5: SalariedEmployee.java
// SalariedEmployee concrete class extends abstract class Employee.
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// constructor
public SalariedEmployee(String firstName, String lastName,
String socialSecurityNumber, double weeklySalary)
{
super(firstName, lastName, socialSecurityNumber);
if (weeklySalary < 0.0)
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
this.weeklySalary = weeklySalary;
}
// set salary
public void setWeeklySalary(double weeklySalary)
{
if (weeklySalary < 0.0)
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
this.weeklySalary = weeklySalary;
}
// return salary
public double getWeeklySalary()
{
return weeklySalary;
}
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
return getWeeklySalary();
}
// return String representation of SalariedEmployee object
@Override
public String toString()
{
return String.format("salaried employee: %s%n%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary());
}
} // end class SalariedEmployee
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
----------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pieceworker;
/**
*
* @author lirby
*/
// Fig. 10.7: CommissionEmployee.java
// CommissionEmployee class extends Employee.
public class CommissionEmployee extends Employee
{
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// constructor
public CommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate)
{
super(firstName, lastName, socialSecurityNumber);
if (commissionRate <= 0.0 || commissionRate >= 1.0) // validate
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
if (grossSales < 0.0) // validate
throw new IllegalArgumentException("Gross sales must be >= 0.0");
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
// set gross sales amount
public void setGrossSales(double grossSales)
{
if (grossSales < 0.0) // validate
throw new IllegalArgumentException("Gross sales must be >= 0.0");
this.grossSales = grossSales;
}
// return gross sales amount
public double getGrossSales()
{
return grossSales;
}
// set commission rate
public void setCommissionRate(double commissionRate)
{
if (commissionRate <= 0.0 || commissionRate >= 1.0) // validate
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
this.commissionRate = commissionRate;
}
// return commission rate
public double getCommissionRate()
{
return commissionRate;
}
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
// return String representation of CommissionEmployee object
@Override
public String toString()
{
return String.format("%s: %s%n%s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate());
}
} // end class CommissionEmployee
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
--------------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pieceworker;
/**
*
* @author lirby
*/
// Fig. 10.6: HourlyEmployee.java
// HourlyEmployee class extends Employee.
public class HourlyEmployee extends Employee
{
private double wage; // wage per hour
private double hours; // hours worked for week
// constructor
public HourlyEmployee(String firstName, String lastName,
String socialSecurityNumber, double wage, double hours)
{
super(firstName, lastName, socialSecurityNumber);
if (wage < 0.0) // validate wage
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0");
if ((hours < 0.0) || (hours > 168.0)) // validate hours
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");
this.wage = wage;
this.hours = hours;
}
// set wage
public void setWage(double wage)
{
if (wage < 0.0) // validate wage
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0");
this.wage = wage;
}
// return wage
public double getWage()
{
return wage;
}
// set hours worked
public void setHours(double hours)
{
if ((hours < 0.0) || (hours > 168.0)) // validate hours
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");
this.hours = hours;
}
// return hours worked
public double getHours()
{
return hours;
}
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
if (getHours() <= 40) // no overtime
return getWage() * getHours();
else
return 40 * getWage() + (getHours() - 40) * getWage() * 1.5;
}
// return String representation of HourlyEmployee object
@Override
public String toString()
{
return String.format("hourly employee: %s%n%s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours());
}
} // end class HourlyEmployee
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
---------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pieceworker;
/**
*
* @author lirby
*/
// Fig. 10.8: BasePlusCommissionEmployee.java
// BasePlusCommissionEmployee class extends CommissionEmployee.
public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary; // base salary per week
// constructor
public BasePlusCommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate, double baseSalary)
{
super(firstName, lastName, socialSecurityNumber,
grossSales, commissionRate);
if (baseSalary < 0.0) // validate baseSalary
throw new IllegalArgumentException("Base salary must be >= 0.0");
this.baseSalary = baseSalary;
}
// set base salary
public void setBaseSalary(double baseSalary)
{
if (baseSalary < 0.0) // validate baseSalary
throw new IllegalArgumentException("Base salary must be >= 0.0");
this.baseSalary = baseSalary;
}
// return base salary
public double getBaseSalary()
{
return baseSalary;
}
// calculate earnings; override method earnings in CommissionEmployee
@Override
public double earnings()
{
return getBaseSalary() + super.earnings();
}
// return String representation of BasePlusCommissionEmployee object
@Override
public String toString()
{
return String.format("%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary());
}
} // end class BasePlusCommissionEmployee
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
-----------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pieceworker;
/**
*
* @author lirby
*/
// Fig. 10.9: PayrollSystemTest.java
// Employee hierarchy test program.
public class PayrollSystemTest
{
public static void main(String[] args)
{
System.out.println("Lisa Tidball - Assignment 3");
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee("John", "Smith", "111-11-1111", 800.00);
HourlyEmployee hourlyEmployee =
new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75, 40);
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300);
PieceWorker pieceWorker =
new PieceWorker("Terri", "Sipantzi", "888-88-8888", .09, 900);
System.out.println("Employees processed individually:");
System.out.printf("%n%s%n%s: $%,.2f%n%n",
salariedEmployee, "earned", salariedEmployee.earnings());
System.out.printf("%s%n%s: $%,.2f%n%n",
hourlyEmployee, "earned", hourlyEmployee.earnings());
System.out.printf("%s%n%s: $%,.2f%n%n",
commissionEmployee, "earned", commissionEmployee.earnings());
System.out.printf("%s%n%s: $%,.2f%n%n",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings());
System.out.printf("%s%n%s: $%, .2f%n%n",
pieceWorker, "earned", pieceWorker.earnings());
// create four-element Employee array
Employee[] employees = new Employee[5];
// initialize array with Employees
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = commissionEmployee;
employees[3] = basePlusCommissionEmployee;
employees[4] = pieceWorker;
System.out.printf("Employees processed polymorphically:%n%n");
// generically process each element in array employees
for (Employee currentEmployee : employees)
{
System.out.println(currentEmployee); // invokes toString
// determine whether element is a BasePlusCommissionEmployee
if (currentEmployee instanceof BasePlusCommissionEmployee)
{
// downcast Employee reference to
// BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee =
(BasePlusCommissionEmployee) currentEmployee;
employee.setBaseSalary(1.10 * employee.getBaseSalary());
System.out.printf(
"new base salary with 10%% increase is: $%,.2f%n",
employee.getBaseSalary());
}
System.out.printf(
"earned $%,.2f%n%n", currentEmployee.earnings());
}
// get type name of each object in employees array
for (int j = 0; j < employees.length; j++)
System.out.printf("Employee %d is a %s%n", j,
employees[j].getClass().getName());
} // end main
} // end class PayrollSystemTest
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/