I dont expect you to read the whole code. If you copy and paste it in your IDE you should find the issue.
I’m new to Java and this code is eating up my time because of this constructor issue.
All in all the code works fine but doesnt return the string reprsentation of objects
public class TestContact {
public static void main(String[] args) {
// Juiste invoer begins with 0 and 9 digits
Phone vasteTel1 = new Phone("027261747", "FIX");
// wrong fix phone
Phone vasteTel2 = new Phone("27261447", "FIX");
// Juiste invoer begins with 0 and 10 digits
Phone mobieleTel1 = new Phone("0495556080", "MOBIELE");
// // wrong mobile phone
Phone mobieleTel2 = new Phone("10495557841", "MOBIELE");
Contact contact1 = new Contact("Joske Ergens", "mobiele",
"jos.ergens@where.com", "0495558041");
Contact contact2 = new Contact("Joseline Peremans", "mobiele",
"joseline.peremans@where.com", "024596247");
System.out.println("NUMMER " + vasteTel1.getNummer() + "\nSOORT: "
+ vasteTel1.getSoort() + "\nGELDIG NUMMER ?: "
+ vasteTel1.isGeldigNummer() + "\n");
System.out.println("NUMMER " + vasteTel2.getNummer() + "\nSOORT: "
+ vasteTel2.getSoort() + "\nGELDIG NUMMER ?:"
+ vasteTel2.isGeldigNummer() + "\n");
System.out.println("NUMMER " + mobieleTel1.getNummer() + "\nSOORT: "
+ mobieleTel1.getSoort() + "\nGELDIG NUMMER ?: "
+ mobieleTel1.isGeldigNummer() + "\n");
System.out.println("NUMMER " + mobieleTel2.getNummer() + "\nSOORT: "
+ mobieleTel2.getSoort() + "\nGELDIG NUMMER ?: "
+ mobieleTel2.isGeldigNummer() + "\n");
System.out.println(contact2);
}
}
public class Contact extends ContactGegevens {
ContactGegevens gegevens;
String naam;
public Contact() {
}
public Contact(String naam, String soort, String mail, String nummer) {
this.naam = naam;
gegevens = new ContactGegevens(getNummer(), getSoort(), getEmail());
}
public void toonInfo() {
}
public String toString() {
return "NAAM: " + naam + "|\n" + gegevens.getNummer();
}
}
public class ContactGegevens extends Phone {
private String eMail;
private Phone gsm;
private Phone telefoon;
public ContactGegevens() {
}
public ContactGegevens(String nummer, String soort, String email) {
super(nummer, soort);
this.eMail = email;
gsm = new Phone(getNummer(), getSoort());
telefoon = new Phone(getNummer(), getSoort());
}
public String getEmail() {
return eMail;
}
public Phone getGsm() {
return gsm;
}
public Phone getTelefoon() {
return telefoon;
}
public String toString() {
// return email adress vast foon, gsm
return "" + eMail + " " + gsm;
}
}
public class Phone {
private static final int LENGTE_MOBIEL = 10;
private static final int LENGTEVAST = 9;
private String nummer;
private String soort;
public Phone() {
}
public Phone(String nummer, String soort) {
this.nummer = nummer;
this.soort = soort;
}
public String getNummer() {
return nummer;
}
public String getSoort() {
return soort;
}
public boolean isGeldigNummer() {
if ((nummer.startsWith(nummer, 0) && nummer.length() == LENGTE_MOBIEL))
return true;
else if ((nummer.startsWith(nummer, 0) && nummer.length() == LENGTEVAST))
return true;
else
return false;
}
}
Your bug is actually here:
You’re setting the gegevens object as a new ContactGegevens, and calling the getters of your object, which will return all nulls as its fields have not been set. Adapt as follows:
Furthermore, you’re extending and composing the same classes, please take a look at some Object Orientation books… You should
a. Either extend the ContactGegevens class, and call the super(…) constructor and not make it a field.
b. Either not extend it and compose Contact and ContactGegevens by making the ContactGegevens a field like you’re doing now.