Pemrograman Lanjut Dosen: Dr. Eng. Herman Tolle
2
Case Study: Payroll System Using Polymorphism • Create a payroll program – Use abstract methods and polymorphism
• Problem statement – 4 types of employees, paid weekly • • • •
Salaried (fixed salary, no matter the hours) Hourly (overtime [>40 hours] pays time and a half) Commission (paid percentage of sales) Base-plus-commission (base salary + percentage of sales) – Boss wants to raise pay by 10%
2003 Prentice Hall, Inc. All rights reserved.
3
10.9 Case Study: Payroll System Using Polymorphism • Superclass Employee – Abstract method earnings (returns pay) • abstract because need to know employee type • Cannot calculate for generic employee
– Other classes extend Employee Employee
SalariedEmployee
CommissionEmployee
BasePlusCommissionEmployee
2003 Prentice Hall, Inc. All rights reserved.
HourlyEmployee
4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Outline
// Fig. 10.12: Employee.java // Employee abstract superclass. public abstract class Employee { private String firstName; private String lastName; private String socialSecurityNumber;
Declares class Employee as abstract class.
// constructor public Employee( String first, String last, String ssn ) { firstName = first; lastName = last; socialSecurityNumber = ssn; }
Employee.java Line 4 Declares class Employee as abstract class.
// set first name public void setFirstName( String first ) { firstName = first; }
2003 Prentice Hall, Inc. All rights reserved.
5 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// return first name public String getFirstName() { return firstName; }
Outline Employee.java
// set last name public void setLastName( String last ) { lastName = last; } // return last name public String getLastName() { return lastName; } // set social security number public void setSocialSecurityNumber( String number ) { socialSecurityNumber = number; // should validate }
2003 Prentice Hall, Inc. All rights reserved.
6 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
Outline
// return social security number public String getSocialSecurityNumber() { return socialSecurityNumber; }
Employee.java
// return String representation of Employee object public String toString() { return getFirstName() + " " + getLastName() + "\nsocial security number: " + getSocialSecurityNumber(); } // abstract method overridden by subclasses public abstract double earnings();
Line 61 Abstract method overridden by subclasses.
Abstract method overridden by subclasses
} // end abstract class Employee
2003 Prentice Hall, Inc. All rights reserved.
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
// Fig. 10.13: SalariedEmployee.java // SalariedEmployee class extends Employee.
7
Outline
public class SalariedEmployee extends Employee { private double weeklySalary; // constructor Use superclass public SalariedEmployee( String first, String last, basic fields. String socialSecurityNumber, double salary ) { super( first, last, socialSecurityNumber ); setWeeklySalary( salary ); }
SalariedEmployee.jav a constructor for Line 11 Use superclass constructor for basic fields.
// set salaried employee's salary public void setWeeklySalary( double salary ) { weeklySalary = salary < 0.0 ? 0.0 : salary; } // return salaried employee's salary public double getWeeklySalary() { return weeklySalary; }
2003 Prentice Hall, Inc. All rights reserved.
8 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// calculate salaried employee's pay; // override abstract method earnings in Employee public double earnings() { return getWeeklySalary(); } Must implement abstract
method earnings. // return String representation of SalariedEmployee object public String toString() { return "\nsalaried employee: " + super.toString(); }
Outline SalariedEmployee.jav a Lines 29-32 Must implement abstract method earnings.
} // end class SalariedEmployee
2003 Prentice Hall, Inc. All rights reserved.
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
// Fig. 10.14: HourlyEmployee.java // HourlyEmployee class extends Employee. public class HourlyEmployee extends Employee { private double wage; // wage per hour private double hours; // hours worked for week
9
Outline HourlyEmployee.java
// constructor public HourlyEmployee( String first, String last, String socialSecurityNumber, double hourlyWage, double hoursWorked ) { super( first, last, socialSecurityNumber ); setWage( hourlyWage ); setHours( hoursWorked ); } // set hourly employee's wage public void setWage( double wageAmount ) { wage = wageAmount < 0.0 ? 0.0 : wageAmount; } // return wage public double getWage() { return wage; }
2003 Prentice Hall, Inc. All rights reserved.
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
// set hourly employee's hours worked public void setHours( double hoursWorked ) { hours = ( hoursWorked >= 0.0 && hoursWorked <= 168.0 ) ? hoursWorked : 0.0; } // return hours worked public double getHours() { return hours; }
10
Outline HourlyEmployee.java Lines 44-50 Must implement abstract method earnings.
// calculate hourly employee's pay; // override abstract method earnings in Employee public double earnings() Must implement abstract { method earnings. if ( hours <= 40 ) // no overtime return wage * hours; else return 40 * wage + ( hours - 40 ) * wage * 1.5; } // return String representation of HourlyEmployee object public String toString() { return "\nhourly employee: " + super.toString(); } } // end class HourlyEmployee
2003 Prentice Hall, Inc. All rights reserved.
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
// Fig. 10.15: CommissionEmployee.java // CommissionEmployee class extends Employee. public class CommissionEmployee extends Employee { private double grossSales; // gross weekly sales private double commissionRate; // commission percentage
11
Outline CommissionEmployee.j ava
// constructor public CommissionEmployee( String first, String last, String socialSecurityNumber, double grossWeeklySales, double percent ) { super( first, last, socialSecurityNumber ); setGrossSales( grossWeeklySales ); setCommissionRate( percent ); } // set commission employee's rate public void setCommissionRate( double rate ) { commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; } // return commission employee's rate public double getCommissionRate() { return commissionRate; }
2003 Prentice Hall, Inc. All rights reserved.
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
12 // set commission employee's weekly base salary public void setGrossSales( double sales ) { grossSales = sales < 0.0 ? 0.0 : sales; } // return commission employee's gross sales amount public double getGrossSales() { return grossSales; }
Outline CommissionEmployee.j ava Lines 44-47 Must implement abstract method earnings.
// calculate commission employee'sMust pay; implement abstract // override abstract method earnings in Employee method earnings. public double earnings() { return getCommissionRate() * getGrossSales(); } // return String representation of CommissionEmployee object public String toString() { return "\ncommission employee: " + super.toString(); } } // end class CommissionEmployee
2003 Prentice Hall, Inc. All rights reserved.
13 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
// Fig. 10.16: BasePlusCommissionEmployee.java // BasePlusCommissionEmployee class extends CommissionEmployee. public class BasePlusCommissionEmployee extends CommissionEmployee { private double baseSalary; // base salary per week
Outline BasePlusCommissionEm ployee.java
// constructor public BasePlusCommissionEmployee( String first, String last, String socialSecurityNumber, double grossSalesAmount, double rate, double baseSalaryAmount ) { super( first, last, socialSecurityNumber, grossSalesAmount, rate ); setBaseSalary( baseSalaryAmount ); } // set base-salaried commission employee's base salary public void setBaseSalary( double salary ) { baseSalary = salary < 0.0 ? 0.0 : salary; } // return base-salaried commission employee's base salary public double getBaseSalary() { return baseSalary; }
2003 Prentice Hall, Inc. All rights reserved.
14 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// calculate base-salaried commission employee's earnings; // override method earnings in CommissionEmployee public double earnings() Override method earnings { in CommissionEmployee return getBaseSalary() + super.earnings(); } // return String representation of BasePlusCommissionEmployee public String toString() { return "\nbase-salaried commission employee: " + super.getFirstName() + " " + super.getLastName() + "\nsocial security number: " + super.getSocialSecurityNumber(); }
Outline BasePlusCommissionEm ployee.java Lines 30-33 Override method earnings in CommissionEmployee
} // end class BasePlusCommissionEmployee
2003 Prentice Hall, Inc. All rights reserved.
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
// Fig. 10.17: PayrollSystemTest.java // Employee hierarchy test program. import java.text.DecimalFormat; import javax.swing.JOptionPane; public class PayrollSystemTest {
15
Outline PayrollSystemTest.ja va
public static void main( String[] args ) { DecimalFormat twoDigits = new DecimalFormat( "0.00" ); // create Employee array Employee employees[] = new Employee[ 4 ]; // initialize array with Employees employees[ 0 ] = new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 ); employees[ 1 ] = new CommissionEmployee( "Sue", "Jones", "222-22-2222", 10000, .06 ); employees[ 2 ] = new BasePlusCommissionEmployee( "Bob", "Lewis", "333-33-3333", 5000, .04, 300 ); employees[ 3 ] = new HourlyEmployee( "Karen", "Price", "444-44-4444", 16.75, 40 ); String output = "";
2003 Prentice Hall, Inc. All rights reserved.
16 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
// generically process each element in array employees for ( int i = 0; i < employees.length; i++ ) { output += employees[ i ].toString(); // determine whether element is a BasePlusCommissionEmployee if ( employees[ i ] instanceof BasePlusCommissionEmployee ) {
Determine whether element is a BasePlusCommissionEmpl downcast Employee reference to oyee BasePlusCommissionEmployee reference
// // BasePlusCommissionEmployee currentEmployee = ( BasePlusCommissionEmployee ) employees[ i ]; double output
Downcast Employee reference to BasePlusCommissionEmployee oldBaseSalary = currentEmployee.getBaseSalary(); += "\nold base salary: reference $" + oldBaseSalary;
currentEmployee.setBaseSalary( 1.10 * oldBaseSalary ); output += "\nnew base salary with 10% increase is: $" + currentEmployee.getBaseSalary();
Outline PayrollSystemTest.ja va Line 32 Determine whether element is a BasePlusCommissionEm ployee
Line 37 Downcast Employee reference to BasePlusCommissionEm ployee reference
} // end if
output += "\nearned $" + employees[ i ].earnings() + "\n"; } // end for
2003 Prentice Hall, Inc. All rights reserved.
17 52 53 54 55 56 57 58 59 60 61 62
// get type name of each object in employees array for ( int j = 0; j < employees.length; j++ ) output += "\nEmployee " + j + " is a " + employees[ j ].getClass().getName(); JOptionPane.showMessageDialog( null, output ); System.exit( 0 ); } // end main } // end class PayrollSystemTest
Get type name of each object in employees // display output array
Outline PayrollSystemTest.ja va Lines 53-55 Get type name of each object in employees array
2003 Prentice Hall, Inc. All rights reserved.
Sebuah perusahaan PUBLISHER mempunyai PEGAWAI Setiap Pegawai memiliki data: Nama, NIP, Alamat, Masa Kerja, Divisi, Jabatan
Status Pegawai: Pegawai Tetap, Pegawai Honorer, Trainee Pegawai dibedakan atas Divisi: (Marketing, CopyWriter, Designer, OB) Pegawai dibedakan oleh Jabatan: Manajer dan Staff Setiap divisi ada 1 Manager dan sisanya adalah Staff
Base Salary (Gaji Pokok): 4 juta rupiah
Bonus / Komisi per Divisi:
Gaji Staff & Trainee : 1x Gaji Pokok
Marketing: 100 ribu / proyek
Gaji Manajer: 2x Gaji Pokok
Designer & CopyWriter: 200 ribu / proyek
Tunjangan Masa Kerja:
Masa Kerja > 5 tahun: 0,5 Gaji Pokok Masa Kerja > 10 tahun: 1x Gaji Pokok
Tunjangan Pegawai Tetap:
Uang Transport: 400 ribu Uang Makan: 400 ribu Uang Lembur: 50 ribu / jam
Tunjangan Pegawai Honorer: Uang Transport: 400 ribu Uang Lembur: 50 ribu / jam
Buatlah Diagram Class untuk representasi Class Pegawai dan kelas turunannya untuk Pegawai Tetap, Pegawai Honorer dan Trainee
1.
Lengkapi dengan atribut dan method yang berkesesuaian
Buat ABSTRACT Method untuk perhitungan Gaji Take Home Pay (Total)
Buat Implementasi Code Class dan Implementasi Program Test dengan kondisi sbb:
2.
Minimal ada 10 orang pegawai (disebar pada setiap Divisi, ada yang berstatus pegawai tetap, honorer maupun Trainee) Output berupa rekap data Pegawai dalam bentuk Tabel beserta jumlah Gaji Bulan Mei 2014 serta Total Jumlah Gaji yang dibayarkan Perusahaan. Program bersifat Modular
1. 2. 3.
Set GajiPokok sebagai KONSTANTA (Final Atribut) Set Nilai Koefisien Perhitungan Gaji dalam bentuk Variabel
Membuat Diagram Class Menentukan Atribut
Menentukan Method
Membuat Code Program Running Program Membuat Laporan Mengirimkan ke Email Dosen
Pegawai Marketing1 = new PegawaiTetap (“David Becham”,”050611”,6,“manager”,”marketing”) Marketing1.setJumlahProyek(10); Pegawai[] ArrayEmployee=new Pegawai[10]; ArrayEmployee[0]=Marketing1; ArrayEmployee[1]=b; ArrayEmployee[2]=c; ArrayEmployee[3]=d; for(int i=0;i
APLIKASI PAYROL PT XXX Daftar Gaji Pegawai Bulan: Mei 2014 No
NAMA
NIP
Divisi
1.
Jabatan
Rincian Gaji Gaji Pokok: Tunjangan A: Tunjangan B:
Gaji Total Rp. xxx.xxx.xxx
2. 3. Total Gaji Pegawai Dibuat menggunakan array dan looping
Rp. xxx.xxx.xxx
Gaji THP PegawaiTetap =
(GP * koefisienJabatan) + TunjanganMasaKerja + TunjanganPegawaiTetap + Bonus;
TunjanganMasaKerja =
(MasaKerja > 5 ? 0.5*GP : 0)
TunjanganMasaKerja =
(MasaKerja > 10 ? GP : 0)
TunjanganPegawaiTetap = Bonus = koefisienDivisi *
jumlahProyek