- Add static count of object. Increment in constructor and decrement in overidden 'finalize' method
	- Throw IllegalArgumentException instead of Exception for passport and age
This commit is contained in:
HeshamTB 2020-10-11 04:10:36 +03:00
parent 3dbb440370
commit 58dfb37437
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -1,5 +1,8 @@
public class Pilgrim { public class Pilgrim {
//keep track of all pilgrim object count.
private static int totalPilgrims;
private String passport; private String passport;
private boolean local; private boolean local;
private int age; private int age;
@ -12,20 +15,26 @@ public class Pilgrim {
setPassport(passport); setPassport(passport);
this.gender = gender; this.gender = gender;
this.priority = priority; this.priority = priority;
totalPilgrims++; //Added a pilgrim
} }
public Priority getPriority() { public Priority getPriority() { return priority; }
return priority;
}
private void setAge(int age) throws Exception { private void setAge(int age) throws IllegalArgumentException {
if (age < 7 || age > 85) throw new Exception("Age out of permitted range"); if (age < 7 || age > 85) throw new IllegalArgumentException("Age out of permitted range");
else this.age = age; else this.age = age;
} }
private void setPassport(String passport) throws Exception { private void setPassport(String passport) throws IllegalArgumentException{
if (passport.length() == 8) //assuming standard passport number lengths if (passport.length() == 8) //assuming standard passport number lengths
this.passport = passport; this.passport = passport;
else throw new Exception("Invalid passport format"); else throw new IllegalArgumentException("Invalid passport format");
//TODO:Make exception classes //TODO:Make exception classes
} }
@Override
protected void finalize() throws Throwable {
super.finalize();
//Decrement the count of total objects when GC cleans unneeded.
totalPilgrims--;
}
} }