0

I want to ask something in object oriented programming: I have built the following software: i have an abstract class: Aapartment

abstract class Aapartment
{
    public int roomNumbers { get; set; }
    public int price { get; set; }  
}

in addition, another two classes:

class Private:Aapartment
{
    public Private() { }
    public Private(int price,int roomN)
    {
        this.price = price;
        this.roomNumbers = roomN;
    }
}

class penthouse:Aapartment
{
    public penthouse() { }
    public penthouse(int price,int roomN)
    {
        this.roomNumbers = roomN;
        this.price = price; 

    }
}

I have also created the following class which derives from ArrayList class:

class AllApartments : ArrayList
{
    public AllApartments() 
    {
    }

    public Aapartment Search(int price, int roomN, string type)
    {
        Aapartment temp = null;
        for(int i=0;i<this.Count;i++)
        {
            if ( (((Aapartment)this[i]).price == price) && (((Aapartment)this[i]).roomNumbers == roomN))
            {
                 if (((Aapartment)this[i]).GetType().Name.ToString().Equals(type))
                 {
                     temp =  (Aapartment)this[i];
                 }
            }   
        }

        return temp;
    }
}

The main program is :

static void Main(string[] args)
{
    AllApartments all = new AllApartments();
    all.Add(new Private(200000000, 5));
    all.Add(new penthouse(125000000, 4));
    all.Add(new penthouse(125000000, 2));
    all.Add(new penthouse(125000000, 7));
    all.Add(new penthouse(125000000, 1));

    int roomN;
    int price;
    string type1;

    type1 = Console.ReadLine() ;
    //Type type = Type.GetType("ConsoleApplication48.Aapartment");
    roomN = Convert.ToInt32(Console.ReadLine());
    price = Convert.ToInt32(Console.ReadLine());

     Aapartment temp =  all.Search(price, roomN, type1);
    Console.WriteLine(temp.GetType().ToString()); 
}

My question is: Is there any option to define Apartment variable instead of string in the search function of AllApartments class, and compare it with the string variable the user enter in the console? I have used the following code: .GetType().Name.ToString().

12
  • 8
    How do I write a good title? Commented Mar 4, 2016 at 13:39
  • 5
    You should not use Arraylists, and even if you did you shouldn't really inherit from them. Look at generics instead. All of that casting would then go away. Commented Mar 4, 2016 at 13:40
  • 2
    This is not to solve your question but more of little nitpicking (if you don't want to call it advice) be very careful when you write your class name your Private (class name) and the private (keyword) is very very similar... Commented Mar 4, 2016 at 13:41
  • 2
    The code has many problems, but the main problem is that you're using inheritance to denote a difference in apartment type. You don't want to recompile and redistribute your application when a user wants to add a new apartment type. Make ApartmentType a string property. Commented Mar 4, 2016 at 13:45
  • I get that this is just an exercise, but using different types for appartments/privates/penthouses really doesn't add anything here, it just makes stuff more complicated. Also, don't use ArrayList; use List<Apartment> instead. Commented Mar 4, 2016 at 13:45

3 Answers 3

2

I would not create AllApartments but use a List and then create a search method :

        private List<Aapartment> _appartements;

    public Aapartment Search(int price, int roomN, Type type)
    {
        var founded = _appartements.SingleOrDefault(x => x.GetType() == type && x.price == price && x.roomN == roomN);
        return founded;

    }

More on lambdas with linq queries : https://msdn.microsoft.com/fr-fr/library/bb397675.aspx

For the program, I would ask to the user 0 for penthouse and 1 for private, it's bad design to map the user input to the type name.

     static void Main(string[] args)
    {
        AllApartments all = new AllApartments();
        all.Add(new Private(200000000, 5));
        all.Add(new penthouse(125000000, 4));
        all.Add(new penthouse(125000000, 2));
        all.Add(new penthouse(125000000, 7));
        all.Add(new penthouse(125000000, 1));

        int roomN;
        int price;
        int type1;

        type1 = int.Parse(Console.ReadLine());
        //Type type = Type.GetType("ConsoleApplication48.Aapartment");
        roomN = Convert.ToInt32(Console.ReadLine());
        price = Convert.ToInt32(Console.ReadLine());
        Type type;
        switch (type1)
        {
            case 0 :
                type = typeof (penthouse);
                break;
            case 1:
                type = typeof (Private);
                break;
            default:
                throw new Exception("Appartment type unknown");
        }

        Aapartment temp = all.Search(price, roomN, type);
        Console.WriteLine(temp.GetType().ToString());
    }

I used an int but you could use an enum;

Sign up to request clarification or add additional context in comments.

1 Comment

and when i call the search function , what do i pass to it ? for example: search ( 100,5,?)
0

You could have a readonly property that returns that.

public string TypeName { get { return GetType().Name; } }

2 Comments

In C#6 this could read: public string TypeName => GetType().Name;
It could solve the problem,but i does not make sense in my abstract class.
0

This isn't Code Review and this looks like an XY problem incited by homework requirements, but here goes.

You're trying to apply inheritance, a strong object-oriented design principle, to a problem where it doesn't fit. You should only use inheritance to add behavior that derives from the base class behavior.

If I may try to extract the original requirements from your question:

I want to create an apartment reservation system, where a user can search for an apartment type.

You don't need inheritance for this. Just as you wouldn't create a Novel : Book class, a ScienceFiction : Book class and a Dictionary : Book class when you're building a library checkout system (not in the first place because a book can have many genres), so shouldn't you when building this apartment reservation system.

The type of an apartment for rent is just a propery of the non-abstract Apartment class. It doesn't add new behavior, it's not something that makes the Apartment class suddenly not fit to hold a "Cardboard Box" nor a "Castle" type of apartment.

So for the sake of simplicity, keep it a string property:

public class Apartment
{
    public int NumberOfRooms { get; set; }
    public int Price { get; set; }  
    public string Type { get; set; }
}

And then the entire need for this question goes away. Of course you could also make Type an enum, for type-safety (or typo-safety).

Furthermore, you don't use ArrayList and you don't inherit from a List (again, you don't add behavior to the list, so don't inherit from it), but just use a List<T>:

var apartmentData = new List<Apartment>
{
    new Apartment { NumberOfRooms = 4, Price = 42000, Type = "Private" },
    new Apartment { NumberOfRooms = 40, Price = 420000, Type = "Penthouse" },
    new Apartment { NumberOfRooms = 400, Price = 4200000, Type = "Castle" },
};

Then you can use a "repository"-like pattern to create your unique logic:

public class ApartmentRepository
{
    private readonly List<Apartment> _apartmentData;

    public ApartmentRepository(List<Apartment> apartmentData)
    {
        _apartmentData = apartmentData;
    }

    public Apartment Search(int price, int numberOfRooms, string type)
    {
        return _apartmentData.FirstOrDefault(a => a.Price == price 
                                               && a.NumberOfRooms == numberOfRooms
                                               && a.Type == type);
    }
}

Besides all that, you should in general favor composition over inheritance. You might say "But my occupants will enter the building in a different way!" (if that's even relevant to the system).

Then you simply add a IWayOfEntry WayOfEntry property to the Apartment (or maybe Residency is a better class name anyway), to which you then assign a DrawBridge, Elevator or FrontDoor instance, while Residency.Enter() simply relays the call to WayOfEntry.Enter().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.