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()
.