0

We have simple homework. I need to create a user with address in java. But I can't figure out where to write address or how to connect the address to user.

My User

package xxx;

import java.util.List;
import java.util.UUID;

public class User {

    private String ID;
    private String firstName;
    private String lastName;
    private int idNumber;
    private String email;
    private Address officialAddress;
    private Address postAddress;
    private List contracts;



    public User(String firstName, String lastName, int idNumber, String email,Address officialAddress, Address postAddress) {
        ID = UUID.randomUUID().toString();
        setFirstName(firstName);
        setLastName(lastName);
        setIdNumber(idNumber);
        setEmail(email);
    }


    public void setFirstName(String firstName) {
        if(firstName == null){
            throw new IllegalArgumentException("Please fill firstName");
        } this.firstName = firstName;
    }

    public String getFirstName(){
        return firstName;
    }

    public void setLastName(String lastName) {
        if(lastName == null){
            throw new IllegalArgumentException("Please fill lastName");
        } this.lastName = lastName;
    }

    public String getLastName(){
        return lastName;
    }

    public void setIdNumber(int idNumber) {
        if(idNumber > 0){
            throw new IllegalArgumentException("Please fill idNumber");
        } this.idNumber = idNumber;
    }

    public int getIdNumber(){
        return idNumber;
    }

    public void setEmail(String email) {
        if(email == null){
            throw new IllegalArgumentException("Please fill email");
        } this.email = email;
    }

    public String getEmail(){
        return email;
    }

}

My address

package xxx;

public class Address {

    private int zipcode;
    private String region;
    private String streetName;
    private int streetNumber;

    public Address(int zipcode, String region, String streetName, int streetNumber) {
        setZipcode(zipcode);
        setRegion(region);
        setStreetName(streetName);
        setStreetNumber(streetNumber);

    }

    private void setZipcode(int zipcode) {
        if(zipcode > 0){
            throw new IllegalArgumentException("Please fill zipcode");
        } this.zipcode = zipcode;
    }

    private int getZipcode(){
        return zipcode;
    }

    private void setRegion(String region) {
        if(region == null){
            throw new IllegalArgumentException("Please fill region");
        } this.region = region;
    }

    private String getRegion(){
        return region;
    }

    private void setStreetName(String streetName) {
        if(streetName == null){
            throw new IllegalArgumentException("Please fill region");
        } this.streetName = streetName;
    }

    private String getStreetName(){
        return streetName;
    }

    private void setStreetNumber(int streetNumber) {
        if(streetNumber > 0){
            throw new IllegalArgumentException("Please fill zipcode");
        } this.streetNumber = streetNumber;
    }

    private int getStreetNumber(){
        return streetNumber;
    }
}

But how i can create user if there is no address in arguments. But i dont know how to create it with that address argument.(Maybe i need it to put out of arguments and connect somehow different).

And this is my try but i get error that i need to fill zipcode.

private Scanner scannerBuff = new Scanner(System.in);
private String firstName;
private User customer;
private Address officialAddress;
private Address postAddress;

public Engine() {

    officialAddress = new Address(484984,"gdfgfd","gdfgdfgdf",4);
    postAddress = new Address(484984,"gdfgdf","gdfgdfg",4);
    customer = new User("gdfgdf","gdfgdf",123456,"fdsfd",officialAddress,postAddress );

Thanks for any help.

3
  • 2
    Why don't do the getter/setter like the others for both Adress in User class ? If officialAddress and postAddress are attributs of User, I think you shouldn't make them attribut of Engine Commented May 10, 2020 at 16:57
  • @azro I am sorry but I don't understand how it is meant. Commented May 10, 2020 at 17:00
  • 1
    What is the problem ? Connect the addresses to the user the same way you connect the name, ID, number, ... into the User constructor Commented May 10, 2020 at 17:02

1 Answer 1

2

To handle addresses, or no addresses, make multiple constructors

public User(String firstName, String lastName, int idNumber, String email) {
    ID = UUID.randomUUID().toString();
    setFirstName(firstName);
    setLastName(lastName);
    setIdNumber(idNumber);
    setEmail(email);
}


public User(String firstName, String lastName, int idNumber, String email, Address officialAddress, Address postAddress) {
    this(firstName, lastName, idNumber, email); // call the first constructor to avoid duplicate code
    setOfficialAdress(officialAddress);
    setPostAdress(postAddress);
}

And the setters like the others

public void setOfficialAdress(Address officialAddress) {
    if(officialAddress == null){
        throw new IllegalArgumentException("Please fill officialAddress");
    } 
    this.officialAddress = officialAddress;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@JurajJakubov I've just edit the 2nd constructor so it calls the first one to avoid code duplication

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.