Skip to content

Instantly share code, notes, and snippets.

@masious
Created March 2, 2015 18:50
Show Gist options
  • Save masious/0a30b7eeef3b628eb535 to your computer and use it in GitHub Desktop.
Save masious/0a30b7eeef3b628eb535 to your computer and use it in GitHub Desktop.
AP_TA_1393_12_11
public class Boss extends Employee{
public Boss(String firstName, String lastName, int salary) {
super(firstName,lastName);
setSalary(salary);
}
@Override
protected void work() {
}
}
public abstract class Employee {
private int salary;
int id;
String firstName;
String lastName;
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
protected abstract void work();
public int getSalary() {
return salary;
}
protected void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
public class HourlyWorker extends Employee{
int spentHours;
int salaryPerHour;
public HourlyWorker(String firstName, String lastName, int salaryPerHour) {
super(firstName, lastName);
this.salaryPerHour = salaryPerHour;
spentHours = 0;
}
protected void work() {
// TODO Auto-generated method stub
}
public int getSpentHours() {
return spentHours;
}
public int getSalary() {
return spentHours * salaryPerHour;
}
public void workedAnHour(){
spentHours++;
}
public void workedNHours(int hours) {
spentHours += hours;
}
}
public class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[10];
employees[0] = new Boss("Akbar", "Asghari", 1000000);
employees[1] = new HourlyWorker("akbar2", "asghari", 10000);
((HourlyWorker) employees[1]).workedAnHour();
((HourlyWorker) employees[1]).workedNHours(5);
int totalSalary = 0;
for(int i=0;i<employees.length; i++){
if(employees[i] == null)
continue;
totalSalary += employees[i].getSalary();
}
System.out.println(totalSalary);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment