public class Employee {
public String name;
public float salary;
public Date birth;
public Employee(String name, float salary, Date birth) {
this.name = name;
this.salary = salary;
this.birth = birth;
}
public float GiveSalary(float percent) {
this.salary *= (1 + percent/100.);
return this.salary;
}
}
public class Dept {
public static int SizeOfCompany = 0;
public ArrayList <Employee> DeptEmployees;
public String dept_name;
public Dept(String dept_name) {
this.dept_name= dept_name;
this.DeptEmployees = new ArrayList <> ();
}
public void Hire(String name, float salary, Date birth) {
Employee new_hire = new Employee(name, salary, birth);
DeptEmployees.add(new_hire);
SizeOfCompany += 1;
}
public void Fire(String name) {
for (Employee a: DeptEmployees) {
if (a.name == name) {
DeptEmployees.remove(a);
SizeOfCompany -= 1;
break;
}
}
}
public Employee GetEmpl(String name) {
for (Employee a: DeptEmployees) {
if (a.name == name)
return a;
}
return null;
}
}
# Let's do it in Python (roughly the same)
class Employee:
def __init__(self,name,salary,date_of_birth):
self.emp_name = name
self.salary = salary
self.date_of_birth = date_of_birth
def GiveRaise(self,percent):
self.salary *= 1 + percent/100.
return self.salary
class Dept:
# This is a class variable, all departments will share it
SizeOfCompany = 0
def __init__(self,dept_name):
self.DeptEmployees = {}
self.dept_name = dept_name
def Hire(self,name,salary,birthday):
new_empl = Employee(name,salary,birthday)
# new_empl is a reference (pointer) to an instance of Employee
self.DeptEmployees[name] = new_empl
Dept.SizeOfCompany += 1
def Fire(self, name):
if name in self.DeptEmployees:
del self.DeptEmployees[name]
Dept.SizeOfCompany -= 1
def GetEmpl(self,name):
if name in self.DeptEmployees:
return self.DeptEmployees[name]
return None
Note that "SizeOfCompany" is independent of department -- it's a class variable shared by all instances of Dept
CS_Dept = Dept('Comp Sci')
CS_Dept.Hire('Fred',29999.95,'01/02/2003')
CS_Dept.Hire('George',40000,'04/05/2006')
TheGreatFred = CS_Dept.GetEmpl('Fred')
TheGreatFred.GiveRaise(15)
print ('After creating CS_Dept: %d people in the company' % Dept.SizeOfCompany)
Engl_Dept = Dept('English')
Engl_Dept.Hire('Harry',50000,'07/08/2009')
print ('After creating Emplish dept: %d people in the company' % Dept.SizeOfCompany)
CS_Dept.Fire('Fred')
print ('After firing Fred: %d people in the company' % Dept.SizeOfCompany)
Georgy = CS_Dept.GetEmpl('George')
print ('before: ',Georgy.salary)
Georgy.salary -= 15000
print ('after: ',Georgy.salary)
Georgy has won an award. Let's note that in his instance, by giving him an "award" parameter and setting it to the name of the award. Note that ONLY GEORGY, amonst all employees, has an award parameter in addition to emp_name and salary. All other employees have only emp_name and salary.
Georgy.award = 'Nobel'
Harold = Engl_Dept.GetEmpl('Harry')
def ShowAward(employee):
# Let's check first whether the employee has a parameter named "award" or not.
if 'award' in employee.__dict__:
print ('%s has %s' % (employee.emp_name,employee.award))
else:
print ('%s has no award parameter' % employee.emp_name)
ShowAward(Georgy)
ShowAward(Harold)
print (Georgy.__dict__)
print (Harold.__dict__)