0

I'm trying to create a program where you can add Student info with their address. Therefor I have created 2 classes, class Student and class Adres (dutch for address). In the Adres class I have the address data and in Student the student data.

But i'm getting stuck at creating a constructor for my Student class because when you add a new student the address of that student also needs to be added.

And I'm trying to use the toString method in Student, but the address needs to be returned aswell.

So I have 3 questions:

  1. How to set up the constructor for class Student where the address also needs to be added
  2. how to set up the toString method where it also returns the "toString" from Adres.
  3. How do I input the LocalDate and address when adding a new student. (localdate is used for the students birthday)

I'm fairly new to java and if someone could help me that would be awesome.

My Adres class:

package oop3.studenten;

public class Adres {

    private String straat;
    private Integer huisnr;
    private String postcode;
    private String plaats;

    public Adres(String straat, Integer huisnr, String postcode, String plaats) {
        this.straat = straat;
        this.huisnr = huisnr;
        this.postcode = postcode;
        this.plaats = plaats;
    }


    public String toString() {
        return straat + "" + huisnr + "," + postcode + "" + plaats;
    }




    // Using regex to check if postcode is valid
    public static boolean checkPostcode(String postCode) {
        return postCode.matches("[1-9][0-9]{3}[a-zA-Z]{2}");
    }


}

My Student class:

package oop3.studenten;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Student {

    private Integer studentnr; //StudentId
    private String voornaam; //Firstname
    private String achternaam; //Lastname
    private LocalDate geboortedatum; //Birthday
    private Adres adres; //address 

    // constructor for Student
    public Student(Integer studentnr, String voornaam, String achternaam, LocalDate geboortedatum, Adres adres){
        this.studentnr = studentnr;
        this.voornaam = voornaam;
        this.achternaam = achternaam;
        this.geboortedatum = geboortedatum;
        this.adres = new Adres(); 
    }

    // toString method for Student
    @Override
    public String toString() {
        return "Student{" +
                "studentnr=" + studentnr +
                ", voornaam='" + voornaam + '\'' +
                ", achternaam='" + achternaam + '\'' +
                ", geboortedatum=" + korteGeboortedatum(geboortedatum) +
                ", adres=" + adres +
                '}';
    }

    // method to return birthday (geboortedatum) in day month year format
    public String korteGeboortedatum(LocalDate gebdatum ){
        return gebdatum.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        }

}

And my main class where I try to add a student

package oop3.studenten;


public class Main {

    public static void main(String[] args) {

        Student student = new Student(500739074, "Ronny", "Giezen", 22111997, "?"
        );
    }

}

Thanks in advance

Ronny Giezen
  • 557
  • 2
  • 10
  • 21
  • 1
    In Student, you pass an Adres argument but then you create new one that you assign to adres field. It does not make sense – Gaël Marziou Feb 26 '20 at 22:29
  • I don't understand your question about toString(), it's OK although the one for Adres does not use a prefix like in Student, try to be consistent. – Gaël Marziou Feb 26 '20 at 22:32
  • 1
    In place of the question mark, you should add `new Adres("...")`. And then replace `this.adres = new Adres()` with `this.adres = adres`. By the way, not all addresses are compatible with this system. For example, how would you store *Kalverstraat 165B*? – MC Emperor Feb 26 '20 at 22:35
  • For geboortedatum, read the doc, you could do LocalDate.of(1997, 11, 22) – Gaël Marziou Feb 26 '20 at 22:37

2 Answers2

1

Student constructor:

public Student(Integer studentnr, String voornaam, String achternaam, LocalDate geboortedatum, Adres adres){
    this.studentnr = studentnr;
    this.voornaam = voornaam;
    this.achternaam = achternaam;
    this.geboortedatum = geboortedatum;
    this.adres = adres; //if you do "new Adres();" a completely empty adres instance would be created, instead you want to use the one passed as parameter
}

// toString method for Student
@Override
public String toString() {
    return "Student{" +
            "studentnr=" + studentnr +
            ", voornaam='" + voornaam + '\'' +
            ", achternaam='" + achternaam + '\'' +
            ", geboortedatum=" + korteGeboortedatum(geboortedatum) +
            ", adres=" + adres.toString() +
            '}'; //                ^^ calling the .toString()-method of adres and appending it to the rest
}

Main Class:

public static void main(String[] args) {
    Adres adres = new Adres("Mainstreet", 5, "48484", "Amsterdam"); //creating the adress
    LocalDate birthday = LocalDate.of(2017, 1, 13);                 //creating the birthday-localdate
    Student student = new Student(500739074, "Ronny", "Giezen", birthday, adres); //passing the birthday & adres to the student-constructor
}
Mano176
  • 168
  • 13
0

In your Main Class you can create the Address Object first and create the Student after. In the Student-Constructor you will be able to pass the address object.

public static void main(String[] args) {
        Address studentAddress = new Address("straat", 1, "postcode", "plaats")
        Student student = new Student(500739074, "Ronny", "Giezen", null, studentAddress);
    }
  1. Question You just call your toString()-Method created in your Address-Class
@Override
    public String toString() {
        return "Student{" +
                "studentnr=" + studentnr +
                ", voornaam='" + voornaam + '\'' +
                ", achternaam='" + achternaam + '\'' +
                ", geboortedatum=" + korteGeboortedatum(geboortedatum) +
                ", adres=" + adres.toString() +
                '}';
    }
  1. Question You have to create a LocalDate Object and pass it in the constructor
public static void main(String[] args) {
        Address studentAddress = new Address("straat", 1, "postcode", "plaats")
        Student student = new Student(500739074, "Ronny", "Giezen", new LocalDate.now(), studentAddress); // for example
    }
Vincentklw
  • 61
  • 1
  • 6