I tried calling the super.toString of the superclass in the subclass Manager, but it won't work. I want the output to be like this:
current data:
#Person (ID=1):
firstname: NN
lastname: NN
email: unknown
date of birth: 01.01.1970
annual pay: 0.00
#Manager:
annual base pay: 0.00
bonus: 0.00
firstname: lastname: email: date of birth: enter date (y m d)
but all I get is without displaying attributes from the superclass:
firstname:
lastname:
email:
date of birth: enter date (y m d)
Any suggestions will be highly appreciated thanks
public class Person {
private String fn;
private String ln;
private String email;
private Date dob;
private static int nextID=1;
private final int ID=nextID++;
Person(){
this.fn = "NN";
this.ln = "NN";
this.email = "unknown";
this.dob = new Date();
}
public String getFn(){
return fn;
}
public void setFn(String fn){
if(fn==null || fn.equals("")){
return;
}
this.fn=fn;
}
public String getLn(){
return ln;
}
public void setLn(String ln){
if(ln==null || ln.equals("")){
return;
}
this.ln=ln;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
if(ln==null || ln.equals("")){
return;
}
if(!email.contains("@")){
return;
}
this.email=email;
}
public Date getDob(){
return new Date(dob);
}
public void setDob(Date dob){
if(dob==null){
return;
}
this.dob= new Date(dob);
}
public String toString(){
return String.format("current data:\n#Person (ID=%d)\nfirstname: %s\nlastname: %s\nemail: %s\ndate of birth: %s\nannual pay: %d", getID(), getFn(), getLn(), getEmail(), getDob(), getAnnualPay());
}
public int getAnnualPay(){
return 0;
}
public void editPerson() {
//firstname: lastname: email: date of birth: enter date (y m d)
TextIO.putln("firstname:");
this.fn = TextIO.getln();
TextIO.putln("lastname:");
this.ln = TextIO.getln();
TextIO.putln("email:");
this.email = TextIO.getln();
TextIO.putln("date of birth: enter date (y m d)");
this.dob = new Date();
}
public int getID(){
return ID;
}
}
public class Manager extends Person {
private int annualBasePay;
private int bonus;
public Manager(){
this.annualBasePay = 0;
this.bonus = 0;
}
public int getAnnualPay(){
return annualBasePay+bonus;
}
public void editPerson(){
this.toString();
super.editPerson();
}
public void setAnnualBasePay(int annualBasePay){
if(annualBasePay<=0){
return;
}
this.annualBasePay=annualBasePay;
}
public int getAnnualBasePay(){
return annualBasePay;
}
public void setBonus(int bonus){
if(bonus<0){
return;
}
this.bonus=bonus;
}
public int getBonus(){
return bonus;
}
public String toString(){
//super.toString();
return String.format("%s/n#Manager:\nannual base pay: %d\nbonus: %d",super.toString(),getAnnualBasePay(), getBonus());
}
}
Forget about super.toString(). Just invoke the getters for the properties you need from Person.