Reputation: 13
I made a Room Class, (it has length, width, height as integers) but I would like to make 2 different objects (because 1 declared Class is for 1 room, and the program would ask the user "How many rooms do you want to have?" or something like that...)
How can I make 2 or 3 different rooms, from the Room class?
here is what I have:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Feladatok1_20
{
public class Exercise55TEST
{
public static void exercise()
{
System.Console.WriteLine("How many rooms would you like to work in?");
int numberOfRooms = Int32.Parse(Console.ReadLine());
List<int> room = new List<int>();
for (int i = 0; i < numberOfRooms; i++)
{
System.Console.WriteLine("How long is the room? (length)");
int length = Int32.Parse(Console.ReadLine());
//room.Add(length);
System.Console.WriteLine("How high is the room? (height)");
int height = Int32.Parse(Console.ReadLine());
//room.Add(height);
System.Console.WriteLine("How wide is the room? (width)");
int width = Int32.Parse(Console.ReadLine());
//room.Add(width);
int area = width*length;
int wallArea = length*height;
int ceilingArea = width*length;
room.Add(area);
room.Add(wallArea);
room.Add(ceilingArea);
}
}
}
}
here is the Class I made:
namespace Feladatok1_20
{
public class Exercise55
{
public int length;
public int height;
public int width;
}
}
Upvotes: 0
Views: 104
Reputation: 41
Create a List of objects of class "Room" and then add one object whenever you have to add for example:
List<Room> rooms = new List<Room>();
Console.WriteLine("How many rooms do you want to have?");
int count = Console.ReadLine(Convert.ToIn16());
for(int i=0l i<count; i++)
{
rooms.Add(new Room());
}
Upvotes: 3
Reputation: 46005
public class Room
{
public int length { get; set; }
public int height { get; set; }
public int width { get; set; }
// those can be calculated so I would declare readonly properties with get-only
public int area { get { return width * length; } }
public int wallArea { get { return length * height; } }
}
side note: wallArea
should be
public int wallArea { get { return 2 * ((length * height) + (width * height)); } }
and the area of the ceiling equals the area of the floor so one properie area
is enough
List<Room> roomList = new List<Room>();
for (int i = 0; i < numberOfRooms; i++)
{
//Console.ReadLine and int.Parse here
roomList.Add(new Room(){ length = inputLength, width = inputWidth, height = inputHeight});
Upvotes: 5