Skip to content

Commit eed7ebd

Browse files
committed
added custom exceptions
1 parent c4c8d8e commit eed7ebd

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

persona.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@
6565
}
6666

6767

68+
class LevelError(Exception):
69+
def __init__(self, msg):
70+
super().__init__(msg)
71+
72+
73+
class MansioneError(Exception):
74+
def __init__(self, msg):
75+
super().__init__(msg)
76+
77+
78+
class IllegalAgeError(Exception):
79+
def __init__(self, msg):
80+
super().__init__(msg)
81+
82+
6883
class Persona(metaclass=ABCMeta):
6984
def __init__(self,nome, cognome, data_nascita, sesso, peso):
7085
self._nome=nome
@@ -116,6 +131,9 @@ class Lavoratore(Persona, metaclass=ABCMeta):
116131

117132
def __init__(self,nome, cognome, data_nascita, sesso, peso, idbadge, mansione):
118133
super().__init__(nome, cognome, data_nascita, sesso, peso)
134+
if data_nascita.year < 18:
135+
raise IllegalAgeError("Dipendente")
136+
119137
Lavoratore.check_id(idbadge)
120138
self._idbadge = idbadge
121139
self._mansione = mansione
@@ -318,10 +336,13 @@ class Dipendente(Lavoratore):
318336
@staticmethod
319337
def check_liv(liv):
320338
if not isinstance(liv, int) or not 0 < liv <= 7:
321-
raise ValueError('Inserire un livello corretto')
339+
raise LevelError('Inserire un livello corretto')
322340

323341
def __init__(self, nome, cognome, data_nascita, sesso, peso, idbadge, mansione, livello):
342+
324343
super().__init__(nome, cognome, data_nascita, sesso, peso, idbadge, mansione)
344+
if mansione not in stipendi.keys():
345+
raise MansioneError("Mansione non presente.")
325346
Dipendente.check_liv(livello)
326347
self._livello = livello
327348
logger.info("E' stato inserito un dipendente.")
@@ -345,8 +366,6 @@ def set_livello(self, x):
345366
Dipendente.check_liv(x)
346367
self._livello = x
347368

348-
349-
350369
def calcolo_stipendio(self, mansione, livl):
351370
return stipendi.get(mansione).get(livl)
352371

@@ -391,9 +410,9 @@ def is_ubriaco(studenti):
391410
@staticmethod
392411
def aumento_a_livello(mansione, livello, aumento):
393412
if mansione not in stipendi.keys():
394-
raise ValueError("Mansione non presente.")
413+
raise MansioneError("Mansione non presente.")
395414
if livello not in stipendi[mansione]:
396-
raise ValueError("Livello non in mansione")
415+
raise LevelError("Livello non in mansione")
397416
if isinstance(aumento, int):
398417
raise ValueError("Aumento deve essere di tipo intero")
399418
stipendi[mansione][livello] = + aumento

0 commit comments

Comments
 (0)