Hello.

I got an assignment and I've ran into some troubles. I have 3 classes. The first class could be labeled as the main class, and the other two classes must be linked to the first one.

The 1st class is named "Holiday" and has 2 key attributes: tour_guide and destination.
Now, the 2nd class is called "Person", and the 3rd class "Destination". Every class MUST be in its own file.

The problem is that I don't know how to link the 2nd, and 3rd class to the two attributes of the 1st class.

Example: I want to create a new object for the class Destination. I input all the attributes, but when I get to the attribute tour_guide, instead of inputting a string ("Name Surname") I have to input a new Person object with all its own attributes(name, surname, date_of_b,...). Same for Destination.

Basically I can only create a new Person or Destination object if I create a new Holiday object. And when I'm creating a new Holiday object I HAVE to create a new Person and Destination object too, and link them to the Holiday object as his attributes.

How do I do that? So far I have the 3 classes with no links between them, a 4th class "Run" that includes main and all the other methods for input/output including an ArrayList where I save the Holiday objects. I just can't seem to figure out how to do the linking.

Any pointers to a solution would be greatly appreciated.

Recommended Answers

All 7 Replies

What do you mean by linking an object to another? Like making one object a function of the other?

I'm having to fill in a few blanks in the requirements here, but probably:
Think about the real world. You go to a travel agent and they already have a list of destinations. They add you to their customer database, and then you can book a holiday.
So in Run you need a way to create Destinations and People. You'll need to keep a collection (eg ArrayList) for each of those. Then you can create Holiday by linking it to an existing Person and an existing Destination.

commented: That I got... By my post, I meant that :) +4

Good idea James, this could work. I'm just wondering, is there a direct way to do that? See I can't create a Person or Destination before creating a Holiday. I have to create a new Holiday first. I create a new Person and Destination when get to the input for the attributes tour_guide and destination.

Here's a little image I made that may help picture my problem a little better:
http://s9.postimage.org/4tnh3vla5/classes.png

Sounds like your code is one big chunk (although I'm guessing, because I haven't seen it). Stage 1 would be to break out the code for creating a new Person into its own method that can be called from anywhere. Ditto Destination. Then you can get the flow of the logic right.

I would post the code, but all the instructions, variables etc are in my native language so it's just way too much to translate.

The arhitecture of my project:

Holiday.java
- attributes: title, destination, tour_guide, start_date, end_date, price, spaces_left
- default constructor
- another constructor with the parameter "title"
- get/set methods for all the attributes

Person.java
- attributes: name, surname, email, date_of_birth
- default constructor
- another constructor with the parameters "name" and "surname"
- get/set methods for all the attributes

Destination.java
- attributes: title, location, country
- default constructor
- another constructor with the parameter "title"
- get/set methods for all the attributes

Run.java
- main method with a two-options switch statement for calling either the method for inputting a new Holiday, or returning all the previously inputted Holidays
- method inputHoliday
- method returnHolidays
- some other unimportant methods like dateToString

They gave us many limitations as to what we are allowed to do and what not, like the whole project can't have any static variables. And the arhitecture has to be exactly as shown above.

Below is the person class. Holiday and Destination are basically the same, but with their own attributes. In Run there's nothing really special either. A lot of instructions for the user to input all the information, and 'object.setAttribute()' statements for setting the values. Is there any particular part of the code you'd like to see? Note that I haven't started writing the returnHolidays method yet, because I want to resolve this "linking" issue (I don't really know what to call it, that's why I'm having trouble googling for a solution).

package assignment1;
import java.util.GregorianCalendar;

public class Person{
    private String name;
    private String surname;
    private String email;
    private GregorianCalendar date_of_birth;

    public Person(){}
   
    public Person(String name, String surname){
        this.name = name;
        this.surname= surname;
    }
    
    public String getName() {
        return name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
    
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public GregorianCalendar getDate_of_birth() {
        return Date_of_birth;
    }

    public void setDate_of_birth(GregorianCalendar Date_of_birth) {
        this.Date_of_birth = Date_of_birth;
    }  
}

looks to me like your tour_guide variable in holiday is of the type 'Person'
so you'll have a

private Person tour_guide;

in your Holiday class, and the mutators (setter and getter) to set and get the values.

I think I managed to get it to work now. If anyone's interested here's what I did.

Basically I did what stultuske suggested. In my Holiday class I switched
"String tour_guide" to "Person tour_guide" and
"String destination" to "Destination destination".

In the holidayInput method in the Run class I input holidays with a for loop. In the loop I create 3 objects: person, destination and holiday for which I set the values that the user inputs. For tour_guide and destination I then do
"holiday.setTour_guide(person)" and "holiday.setDestination(destination).

For returning all the holidays in the ArrayList I get the values for tour_guide and destination with something like "holidays.get(i).getTour_guiden().getName()"; "holidays" being the ArrayList where holidays are saved and "i" being the counter in the for loop.

It was really much simpler than I imagined, my mistake was to overcomplicate it.
Anyway, thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.