10-04-2017, 09:13 PM
package payrollsystem;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author import java.asg
*/
class Employee {
private String employeeName;
private final int employeeType;
private double salary;
Employee(String name, int type) {
this.employeeName = name;
this.employeeType = type;
this.salary = 0;
}
public String getName() {
return this.employeeName;
}
public void setName(String name) {
this.employeeName = name;
}
public String getType() {
switch (employeeType) {
case 1:
return "Hourly Rate Employee\t";
case 2:
return "Salaried Employee\t";
case 3:
return "Salaried + Comission Employee";
default:
return "Employee Type NOT DEFINED";
}
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
if (salary > 1000) {
salary = 1000;
}
return salary;
}
@Override
public String toString() {
return employeeName + " \t" + getType() + " \t$ " + getSalary();
}
}
// Overridable method call in constuctor
final class HourlyEmployee extends Employee {
private double hourlyRate;
private int workHours;
public HourlyEmployee(String name, double hr, int wh) {
super(name, 1);
hourlyRate = hr;
workHours = wh;
this.setSalary();
}
public void setHourlyRate(double rate) {
this.hourlyRate = rate;
}
public double getHourlyRate() {
return this.hourlyRate;
}
public void setWorkHours(int d) {
this.workHours = d;
}
public int getWorkHours() {
return this.workHours;
}
public void setSalary() {
double sal;
sal = hourlyRate * workHours;
super.setSalary(sal);
}
}
// Overridable method call in constuctor
class SalariedEmployee extends Employee {
protected double weeklySalary;
protected int workDays;
public SalariedEmployee(String name, double ws, int wd) {
super(name, 2);
weeklySalary = ws;
workDays = wd;
this.setSalary();
}
public void setWorkingDays(int d) {
this.workDays = d;
}
public int getWorkingDays() {
return workDays;
}
public void setWeeklySalary(double d) {
this.weeklySalary = d;
}
public double getWeeklySalary() {
return this.weeklySalary;
}
public void setSalary() {
double sal;
sal = (weeklySalary * workDays) / 5;
setSalary(sal);
}
}
class SalariedPlusComissionEmployee extends SalariedEmployee {
double comission;
public SalariedPlusComissionEmployee(String name, double ws, int wd, double d) {
super(name, ws, wd);
comission = d;
this.setSalary();
}
public void setComission(double comission) {
this.comission = comission;
}
public double getComission() {
return this.comission;
}
@Override
public void setSalary() {
double sal;
sal = ((weeklySalary * workDays) / 5) + this.comission;
setSalary(sal);
}
}
class ErrorHandling {
public int getIntegerInput(String inputText) {
int intValue = 0;
boolean isError;
do {
try {
System.out.println(inputText);
isError = true;
Scanner s = new Scanner(System.in);
intValue = s.nextInt();
} catch (InputMismatchException e) {
System.out.println("Please enter only INTEGER values");
isError = false;
}
} while (!isError);
return intValue;
}
public double getDoubleInput(String inputText) {
double doubleVal = 0;
boolean isError;
do {
try {
System.out.println(inputText);
isError = true;
Scanner s = new Scanner(System.in);
doubleVal = s.nextInt();
} catch (InputMismatchException e) {
System.out.println("Please enter only DOUBLE values");
isError = false;
}
} while (!isError);
return doubleVal;
}
public String getStringInput(String inputText) {
String stringVal = null;
boolean isError;
do {
try {
System.out.println(inputText);
isError = true;
Scanner s = new Scanner(System.in);
stringVal = s.next();
} catch (InputMismatchException e) {
System.out.println("Please enter only STRING values");
isError = false;
}
} while (!isError);
return stringVal;
}
}
public class PayrollSystem {
public static void main(String[] args) {
// Employee e = new Employee("name", 0);
// e.setSalary(4000);
// System.out.println(e.getSalary());
//
// HourlyEmployee h = new HourlyEmployee("asdb", 12, 5);
// System.out.println(h.getHourlyRate());
// System.out.println(h.getWorkHours());
// h.setSalary();
// System.out.println(h.toString());
//
// SalariedEmployee s = new SalariedEmployee("asd", 220, 4);
// s.setSalary();
//
// System.out.println(s.toString());
//
// SalariedPlusComissionEmployee sc = new SalariedPlusComissionEmployee("ad", 500, 5, 30);
// sc.setSalary();
// System.out.println(sc.toString());
ArrayList<Employee> employees = new ArrayList<>();
char answer = 'N';
// do {
SalariedPlusComissionEmployee sc = new SalariedPlusComissionEmployee("Abid Rasool", 500, 5, 30);
SalariedEmployee s = new SalariedEmployee("Amol Wankhede", 520, 4);
HourlyEmployee h = new HourlyEmployee("Carlos Bello", 202, 5);
employees.add(s);
employees.add(sc);
employees.add(h);
// } while (answer != 'N');
double totalSalary = 0;
System.out.println("#\tName\t\tType\t\t\t\tSalary");
System.out.println("--");
for (int a = 0; a < employees.size(); a++) {
System.out.println((a + 1) + "\t" + employees.get(a).toString());
totalSalary += employees.get(a).getSalary();
}
System.out.println("--");
System.out.println("\t\t\t\tTotal Salary to go : \t$ " + totalSalary);
}
}