Question

Okaii so i wrote the two classes , did the main too (Still testing, not done yet ) , after running it , it gives null in the output , which means the default value.. why ?

Location class ( With setters and getters )

public class Location {

public String city;
public String street;

public Location() {
}

public Location(String city, String street) {
    this.city = city;
    this.street = street;
}



@Override
public String toString(){
    return "City Is : " + city + " \nStreet Is : " + street;

}

}

And this is the composition in property class : (Also with setters and getters )

public Location location = new Location();
    public static int counter = 0;

    public Property(int ID, Location location) {
        this.ID = ID;
        this.location = new Location();
        counter++;
    }
public String toString() {
        return "\nID is : " + ID + "\nLocation is : \n" + location.toString();
    }

And the output is :

Enter Propery ID : 123 Enter Property City and Street : test test1 Do you want to Enter more Property Data ? no Choose One of the Options from the following menu : 1- List all properties. For each property display its id and location
2- List all properties locations. For each property display only the location
3- List all the ids of all properties that are in a certain given city (read the city)
4- Given a property id change its location city (read the id and the new value of city)
5- Display the location of a property given its id if it is found in the array (read the id)
6- Display the number of properties
7- Exit 1


Property 1 ID is : 123 Location is : City Is : null Street Is : null

Was it helpful?

Solution

In Property constructor, I think you should have

this.location = location;

OTHER TIPS

In the Property class: this.location = new Location(); instead of using the location parameter.

Your location's empty constructor doesn't initialize the fields city and street so when you build a Location object with that constructor you need to ensure to set those values manually. Or just delete that constructor and use only this:

public Location(String city, String street) {
    this.city = city;
    this.street = street;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top