oop.py 1.3 KB
Newer Older
C
Corey Schafer 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

class Employee:

    num_of_emps = 0
    raise_amt = 1.04

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.email = first + '.' + last + '@email.com'
        self.pay = pay

        Employee.num_of_emps += 1

    def fullname(self):
        return '{} {}'.format(self.first, self.last)

    def apply_raise(self):
        self.pay = int(self.pay * self.raise_amt)

C
Corey Schafer 已提交
21 22 23
    @classmethod
    def set_raise_amt(cls, amount):
        cls.raise_amt = amount
C
Corey Schafer 已提交
24

C
Corey Schafer 已提交
25 26 27 28
    @classmethod
    def from_string(cls, emp_str):
        first, last, pay = emp_str.split('-')
        return cls(first, last, pay)
C
Corey Schafer 已提交
29

C
Corey Schafer 已提交
30 31 32 33 34
    @staticmethod
    def is_workday(day):
        if day.weekday() == 5 or day.weekday() == 6:
            return False
        return True
C
Corey Schafer 已提交
35 36


C
Corey Schafer 已提交
37 38
emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Test', 'Employee', 60000)
C
Corey Schafer 已提交
39

C
Corey Schafer 已提交
40
Employee.set_raise_amt(1.05)
C
Corey Schafer 已提交
41

C
Corey Schafer 已提交
42 43 44
print(Employee.raise_amt)
print(emp_1.raise_amt)
print(emp_2.raise_amt)
C
Corey Schafer 已提交
45

C
Corey Schafer 已提交
46 47 48
emp_str_1 = 'John-Doe-70000'
emp_str_2 = 'Steve-Smith-30000'
emp_str_3 = 'Jane-Doe-90000'
C
Corey Schafer 已提交
49

C
Corey Schafer 已提交
50
first, last, pay = emp_str_1.split('-')
C
Corey Schafer 已提交
51

C
Corey Schafer 已提交
52 53
#new_emp_1 = Employee(first, last, pay)
new_emp_1 = Employee.from_string(emp_str_1)
C
Corey Schafer 已提交
54

C
Corey Schafer 已提交
55 56
print(new_emp_1.email)
print(new_emp_1.pay)
C
Corey Schafer 已提交
57

C
Corey Schafer 已提交
58 59
import datetime
my_date = datetime.date(2016, 7, 11)
C
Corey Schafer 已提交
60

C
Corey Schafer 已提交
61
print(Employee.is_workday(my_date))