Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Creating a pointer/reference to another object - any help!

Hi,

I'm trying to design a basic video loans program, and currently have a class for the titles of the videos. I need to create a link to this from the Video class, which stores loan information on the videos being borrowed.

Basically, any idea how I can create a pointer/reference to the Title object from the Video class?

I also need to add a boolean to say whether the video is available or not.

I know these are a bit simple, but any help would really be appreciated.

My code so far is as follows,

1. The Video Class

package videos;

public class Video {

private int days, referenceNumber;
private static double dailyPrice;
private Title videoTitle;

public Video(int refNum, Title t) {
referenceNumber = refNum; videoTitle = t;
}

public Video(){

}

public int getReferenceNumber() {
return referenceNumber;
}

public void setReferenceNumber(int refNum) {
referenceNumber = refNum;
}

public boolean loanStatus() {

}
}



2. The code from my Title class,

package videos;

public class Title
{
private int titleCode;
private String filmName, leadActor1, leadActor2, director;

public Title(int title, String film, String lead1, String lead2, String dir)
{ titleCode = title;
filmName = film;
leadActor1 = lead1;
leadActor2 = lead2;
director = dir;
}

public void setTitleCode(int title)
{ titleCode = title; }

public int getTitleCode()
{ return titleCode; }

public void setFilmName(String film)
{ filmName = film; }

public String getFilmName()
{ return filmName; }

public void setLeadActor1(String lead1)
{ leadActor1 = lead1; }

public String getLeadActor1()
{ return leadActor1; }

public void setLeadActor2(String lead2)
{ leadActor2 = lead2; }

public String getLeadActor2()
{ return leadActor2; }

public void setDirector(String dir)
{ director = dir; }

public String getDirector()
{ return director; }

public String toString()
{ return titleCode+":"+filmName+":"+leadActor1+":"+leadActor2+":"+director; }

}



Any help appreciated!! :confused:
[2594 byte] By [fulcanelli] at [2007-11-11 7:19:08]
# 1 Re: Creating a pointer/reference to another object - any help!
Basically, any idea how I can create a pointer/reference to the Title object from the Video class?

videoTitle = new Title("AS", "XXX", "YYY", "ZZZ", "foo");
aniseed at 2007-11-11 22:38:45 >
# 2 Re: Creating a pointer/reference to another object - any help!
Hi,

I know this may sound like a really basic question, but how do I fit that into the Video class?

Many thanks :confused:
fulcanelli at 2007-11-11 22:39:45 >
# 3 Re: Creating a pointer/reference to another object - any help!
As a part of constructing your Video class you need to create an instance of the Title class which will be your "t" data member.

The "how" of your implementation will depend on how your program is obtaining the data which will be stored in the Title object.
nspils at 2007-11-11 22:40:49 >
# 4 Re: Creating a pointer/reference to another object - any help!
Hi,

Thanks for the reply. The data within the Title class is to be stored in a text file, which can be read from and written to as required.

With regard to the Video class, I've tried implementing the requirements as follows,

package videos;

public class Video {

private int days, referenceNumber;
private static double dailyPrice;
private double totalPrice;
private Title title;
public boolean available;

public Video(int refNum, int d, double dp, Title title) {
referenceNumber = refNum;
days = d;
dailyPrice = dp;
this.title = title;
}

public int getReferenceNumber() {
return referenceNumber;
}

public void setReferenceNumber(int refNum) {
referenceNumber = refNum;
}

public int getDays() {
return days;
}

public void setDays(int d) {
days = d;
}

public double getDailyPrice() {
return dailyPrice;
}

public void setDailyPrice(double dp) {
dailyPrice = dp;
}

public void getTotalPrice() {
totalPrice = days*dailyPrice;
}

public boolean isAvailable() {
if (available)
return true;
else
return false;
}

public String toString() {
return referenceNumber + "" + title + "" + totalPrice + available;
}
}



Do you think this will work correctly? Have I implemented it correctly so far?

Any help appreciated!! :confused:
fulcanelli at 2007-11-11 22:41:43 >
# 5 Re: Creating a pointer/reference to another object - any help!
Your class looks ok to me ...
nspils at 2007-11-11 22:42:47 >
# 6 Re: Creating a pointer/reference to another object - any help!
Hi,

That's great. Thank you for your help. :)
fulcanelli at 2007-11-11 22:43:46 >