-1

i am creating a console app that makes use of inheritance to access methods in an interface which i am also creating. This will be the interface with the methods. (Code still to be added.) EDIT: I am creating an application that captures transport booking info for aircraft and trains. CAircraftBookings, CTrainBookings are booking classes that will contain and manage bookings made for Aicraft/Trains.

namespace Vehicle_Bookings
{
public interface IVehichlesBookings : CAircraftBookings, CTrainBookings
{
    public int Add
    {
    }

    public int Update
    {
    }

    public int Delete
    {
    }

    public int Retrieve
    {
    }
}

}

Now in my console app, the user selects the appropriate choice which will be Adding, Updating, Deleting and Retrieving. The choices will then go as follows:

switch (choice)
        {
            case "1": 



                break;

            case "2":



                break;

            case "3":



                break;

            case "4":



                break;
        }
        while (choice != "5")

How would i go about implementing specific methods from that interface. If the user presses 1 for choice 1, the method Add will be used. If they press 2 the update method will be used etc.

zen_1991
  • 599
  • 1
  • 10
  • 27
  • 3
    Your `interface` is not valid C# syntax. – Jonathon Reinhart Sep 11 '13 at 08:31
  • You should not derive from CAircraftBookings (looks like it's a class). This looks like someone needs to do some programming on school. :) – Jeroen van Langen Sep 11 '13 at 08:32
  • What are `CAircraftBookings, CTrainBookings`? – Hamlet Hakobyan Sep 11 '13 at 08:32
  • I will be deriving and implementing booking classes for aircraft and trains from the IVehicleBookings class – zen_1991 Sep 11 '13 at 08:33
  • Since you are already writing code to do hardcoded differentiation between the possible options, simply call the actual method you want to call, in each of the `case` sections... – Eduard Dumitru Sep 11 '13 at 08:34
  • I agree with Jeroen. Also, you refer to a console app. An interface should be implemented by one or more classes. Which is your class that will implement your interface? – Christos Sep 11 '13 at 08:35
  • I didn't want to make the assumption the other guys did. I simply thought you are not aware of the C# naming conventions. It is a fact that an interface cannot extend a class. It's like saying that pigs fly. Just because it sounds like words which do not violate grammar (we're not saying "pigs flyes" which would be incorrect from a grammar point of view) it doesn't mean it can happen. – Eduard Dumitru Sep 11 '13 at 08:37
  • An interface can't have implementations for the methods. Also CAircraftBookings, CTrainBookings will need to be interfaces themselves. You should try this in the compiler first. – acarlon Sep 11 '13 at 08:37

3 Answers3

2

"I will be deriving and implementing booking classes for aircraft and trains from the IVehicleBookings class"

I think this is what you want:

public interface IVehichlesBookings
{
    int Add();
    int Update();
    int Delete();
    int Retrieve();
}

public class CAircraftBookings : IVehichlesBookings
{
    public int Add()
    {
        throw new NotImplementedException();
    }

    public int Update()
    {
        throw new NotImplementedException();
    }

    public int Delete()
    {
        throw new NotImplementedException();
    }

    public int Retrieve()
    {
        throw new NotImplementedException();
    }
}

public class CTrainBookings : IVehichlesBookings
{
    public int Add()
    {
        throw new NotImplementedException();
    }

    public int Update()
    {
        throw new NotImplementedException();
    }

    public int Delete()
    {
        throw new NotImplementedException();
    }

    public int Retrieve()
    {
        throw new NotImplementedException();
    }
}

An interface IVehicleBookings that defines all kinds of things you can do with "vehicle bookings", and CAircraftBookings and CTrainBookings will decide how those things are done.

dcastro
  • 66,540
  • 21
  • 145
  • 155
1

Assuming VehiclesBooking is a class that implements IVehiclesBooking:

IVehiclesBooking vehiclesBooking = new VehiclesBooking();

switch (choice)
        {
            case "1": 
            vehiclesBooking.Add();
            break;

            case "2":
            vehiclesBooking.Update();
            break;

...
Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
1

I think i'm doing someones homework :)

As I can advise you, you should read about interfaces and how to use them. Look here: Why do we use Interface? Is it only for Standardization?

I think the definition of the interface is right, except it should not derive from a class. You should derive from the interface.

example: PSEUDO

public interface IVehichlesBookings
{
    // members.
}

public class CAircraftBookings : IVehichlesBookings
{
}

public class CTrainBookings : IVehichlesBookings
{
}

This means, that a CAircraftBookings supports the interface IVehichlesBookings, so it should have implement those IVehichlesBookings members. Next you can handle an instance of CTrainBookings or CAircraftBookings as IVehichlesBookings.

And you need to construct it like this:

IVehichlesBookings booking = new CTrainBookings();

char key = Console.ReadKey();

switch(key)
{
    case '1':
        booking.Add(...);
        break;
}

Your turn..

Community
  • 1
  • 1
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57