Help with class object (relationship)

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,365
Reaction score
346
Location
Pretoria
So I have 3 objects (Car, Employee, and Department)
A car can belong to either an employee or department - it cannot belong to both. The car can also be available (meaning it hasn't been assigned to either an employee or department).

How can I represent this in a class?
At the moment I have something like this, but it doesn't stop the car from belonging to both employee and department.
Code:
class Car {
public int CarId {get; set;}
.
.
.
.
public Employee Emp {get;set;}
public Department Dept {get;set;}
}

How can I have something like:
Code:
public [Employee || Department] Owner {get;set;}
This is probably a stupid question.

//Feels embarrassed
 
Heh? No such thing as a stupid question dude

I'm no pro (cause I'm self taught), but I'd consider doing an interface Owner and have Employee and Department implement the interface. Owner will then implement the Car class in a function so that Employee/Department can take ownership of a car
 
Heh? No such thing as a stupid question dude

I'm no pro (cause I'm self taught), but I'd consider doing an interface Owner and have Employee and Department implement the interface. Owner will then implement the Car class in a function so that Employee/Department can take ownership of a car

Perhaps it's just the way you have worded your reply, but:
+1 on creating an interface for Owner.
However, I would have Employee and Department implement the Owner interface, and I'd have Car contain a data member of type (that implements) Owner.
Then Car's Owner can be set to either an Employee or Department.

I think that's possible in Java (I'm assuming we're talking Java here).
 
A simplie way to do it would be to have a property in the car object say assigned and set it to false. When it gets assigned check that assigned is not equal to true then change it to true otherwise throw an exception.
 
Perhaps it's just the way you have worded your reply, but:
+1 on creating an interface for Owner.
However, I would have Employee and Department implement the Owner interface, and I'd have Car contain a data member of type (that implements) Owner.
Then Car's Owner can be set to either an Employee or Department.

I think that's possible in Java (I'm assuming we're talking Java here).

Looking at his supplied code it looks like c#
 
A simplie way to do it would be to have a property in the car object say assigned and set it to false. When it gets assigned check that assigned is not equal to true then change it to true otherwise throw an exception.


This would be the simplest way if all 3 objects are singletons
 
Perhaps it's just the way you have worded your reply, but:
+1 on creating an interface for Owner.
However, I would have Employee and Department implement the Owner interface, and I'd have Car contain a data member of type (that implements) Owner.
Then Car's Owner can be set to either an Employee or Department.

I think that's possible in Java (I'm assuming we're talking Java here).
I thought about that, but my Owner interface will be empty because the Employee and Department have no properties in common. So it will be a "marker interface" (empty interface)
So it will be like:
Code:
public interface IOwner
{}

public class Employee : IOwner
{
   public int EmployeeId {get;set;}
   public string FirstName {get;set;}
...
}

public class Department :IOwner
{
  public int DepartmentId {get;set;}
  public string Name {get;set;}
  .....
}
I suppose I can forge some properties on the Owner interface

It's c# code.
 
Last edited:
Here's what I have:

Option 1 - make the property a plain C# object since you mention Employee and Department have no relationship:
Code:
    public class Employee
    { 
    }

    public class Department
    {
    }

    public class Car
    {
        private object _owner;

        public object Owner 
        {
            get { return this._owner; }
            set { _owner = _owner != null ? _owner : value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var car = new Car();
            car.Owner = new Department();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            
            Console.WriteLine("Trying to change owner...");
            car.Owner = new Employee();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            Console.ReadLine();
        }
    }

Option 2 - create an abstract class if you don't want to use the C# object type:

Code:
 public abstract class Owner
    {
    }

    public class Employee : Owner
    { 
    }

    public class Department : Owner
    {
    }

    public class Car
    {
        private Owner _owner;

        public Owner Owner 
        {
            get { return this._owner; }
            set { _owner = _owner != null ? _owner : value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var car = new Car();
            car.Owner = new Employee();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            
            Console.WriteLine("Trying to change owner...");
            car.Owner = new Department();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            Console.ReadLine();
        }
    }

Option 3 - use the interface option mentioned above.

I think all you are looking for is to wrap your property setter to allow the value to be set once only? The above code does that. You could throw an exception in the setter should there already be a value set.
 
You do have something in common when using the interface option.... the Car

AFAIK you can extend that. But I might be jumbling my terms up too much

Both Employee and Department can be owners of a car. So in the interface I'd specify that. But yea, a lot of ways to approach this :D
 
Here's what I have:

Option 1 - make the property a plain C# object since you mention Employee and Department have no relationship:
Code:
    public class Employee
    { 
    }

    public class Department
    {
    }

    public class Car
    {
        private object _owner;

        public object Owner 
        {
            get { return this._owner; }
            set { _owner = _owner != null ? _owner : value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var car = new Car();
            car.Owner = new Department();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            
            Console.WriteLine("Trying to change owner...");
            car.Owner = new Employee();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            Console.ReadLine();
        }
    }

Option 2 - create an abstract class if you don't want to use the C# object type:

Code:
 public abstract class Owner
    {
    }

    public class Employee : Owner
    { 
    }

    public class Department : Owner
    {
    }

    public class Car
    {
        private Owner _owner;

        public Owner Owner 
        {
            get { return this._owner; }
            set { _owner = _owner != null ? _owner : value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var car = new Car();
            car.Owner = new Employee();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            
            Console.WriteLine("Trying to change owner...");
            car.Owner = new Department();

            Console.WriteLine("Car belongs to: " + car.Owner.GetType().Name);

            Console.ReadLine();
        }
    }

Option 3 - use the interface option mentioned above.

I think all you are looking for is to wrap your property setter to allow the value to be set once only? The above code does that. You could throw an exception in the setter should there already be a value set.

You mean abstract implementation? Also abstract classes are expensive to construct, if the entire purpose is to just define a contract use interface's instead.
 
For now I have left the car class with the employee and department data members because dapper doesn't map interfaces or abstract classes - it has to be a concrete class.
 
I have faced problems similar to this as well and struggle to find an answer because there are so many options.
I'm also a self taught hobby programmer btw so keep a pinch of salt handy.

What does Car do with the Owner object? Does it just display it? i.e. you have a list of cars and want to see who owns the cars?
In a real life scenario you would not give a car an owner. Instead you would give a person / department a car.
So it doesn't really make sense to store the employee or department object in the car class (imo)

Instead have a property on the Car that merely stores the details of the owner in a class called OwnerInfo with properties like:

OwnerType (Department / Employee)
OwnerId
OwnerName
etc...

At the point where a car is assigned to an owner, the OwnerInfo class is also created with the relevant data and assigned to the Car's Owner property.

Then take Shautir's suggestion whereby you first check if OwnerInfo is null before assigning an owner.
Unless the value being passed is null Of course - or least you would need some way of clearing the old owner, should the car be reassigned later...


let us know.
 
I have faced problems similar to this as well and struggle to find an answer because there are so many options.
I'm also a self taught hobby programmer btw so keep a pinch of salt handy.

What does Car do with the Owner object? Does it just display it? i.e. you have a list of cars and want to see who owns the cars?
In a real life scenario you would not give a car an owner. Instead you would give a person / department a car.
So it doesn't really make sense to store the employee or department object in the car class (imo)

Instead have a property on the Car that merely stores the details of the owner in a class called OwnerInfo with properties like:

OwnerType (Department / Employee)
OwnerId
OwnerName
etc...

At the point where a car is assigned to an owner, the OwnerInfo class is also created with the relevant data and assigned to the Car's Owner property.

Then take Shautir's suggestion whereby you first check if OwnerInfo is null before assigning an owner.
Unless the value being passed is null Of course - or least you would need some way of clearing the old owner, should the car be reassigned later...


let us know.

I have a bidirectional relationship. The employee and department classes have the car member. So I'm trying to get all the cars with their associated owners indeed.

The database side is different. The car table doesn't have the employee or department foreign key. I have reference tables to link the the car and the employee, the car and the department

Car-->CarEmp-->Employee
Car-->CarDept-->Department

I could have had both the EmployeeId and DepartmentId on the Car table
 
Can you not post an ERD and we can advise better, it sounds like your DB design is off. I mean why would you ever want to link a employee to a car or even to a department.
 
I have a bidirectional relationship. The employee and department classes have the car member. So I'm trying to get all the cars with their associated owners indeed.

The database side is different. The car table doesn't have the employee or department foreign key. I have reference tables to link the the car and the employee, the car and the department

Car-->CarEmp-->Employee
Car-->CarDept-->Department

I could have had both the EmployeeId and DepartmentId on the Car table
Is the DB data correct in that the car ID will only ever be in one or none of the reference tables?
 
Can you not post an ERD and we can advise better, it sounds like your DB design is off. I mean why would you ever want to link a employee to a car or even to a department.




I'm using my phone at the moment.

My car table does not have the employee or department reference. The employee and department tables do not have the car reference since either can have zero or more cars, hence the reference tables
 
I'm using my phone at the moment.
My car table does not have the employee or department reference. The employee and department tables do not have the car reference since either can have zero or more cars.

That is not what i was referring to, you said you had a reference table linking an employee to a car.
 
How would you represent an employee having zero or more cars in this instance?

An employee can belong to a department, and a car can have a link to an employee. If you want a car can have a link to department as well i.e. sports cars. Like i said when you get a chance post the ERD. It just appears your db design is very convoluted.
 
Top
Sign up to the MyBroadband newsletter
X