I'm currently doing an assignment, where I have two classes Room
and House
. As a part of the assignment, I need to enable a House
class to store the Room
s in ArrayList
:
Implement a method to read in room information in the
House
class from the keyboard — this should call theRoom
constructor as required. Create a zero-argument House constructor to call this new method.
I apologize if this is vague or has been answered elsewhere, but having tried to understand the explanations on other similar queries, I don't know how to apply them to my situation.
How do I apply ArrayList
of a Room
to the House
class?
House
:
import java.util.ArrayList;
import java.util.Scanner;
public class House {
private int idNum;
private static int internalCount = 0;
private ArrayList rooms = new ArrayList();
private String address;
private int numRooms;
private String houseType;
public House (String address, int numRooms, String houseType) {
idNum = internalCount++;
this.address = address;
this.numRooms = numRooms;
this.houseType = houseType;
}
public House () {
int i = 0;
idNum = ++internalCount;
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");
System.out.println("Enter address of house:");
address = scan.next();
System.out.println("Enter number of rooms:"); //Number of rooms in the House
numRooms = scan.nextInt();
while (i < numRooms) //This will loop until all rooms have been described
{
add.room = new Room[100]; //I understand this is incorrect but I don't know what I should have in here
i++;
}
System.out.println("Enter type of house:");
houseType = scan.next();
}
public void addroom(String description, double length, double width)
{
}
int getIdNum() {
return idNum;
}
@Override
public String toString()
{
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
report += "Address: " + address + "\n";
report += "No. of Rooms: " + numRooms + "\n";
report += "House Type: " + houseType+ "\n";
report += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
return report;
}
}
Room
:
import java.util.Scanner;
public class Room {
private String description;
private double length;
private double width;
public Room (String description, double length, double width) {
this.description = description;
this.length = length;
this.width = width;
}
public Room () {
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");
System.out.println("Enter description of room:");
description = scan.next();
System.out.println("Enter length of room:");
length = scan.nextDouble();
System.out.println("Enter width of room:");
width = scan.nextDouble();
}
/*
* Calculates and returns area of Room
*/
public double getArea () {
return length*width;
}
@Override
public String toString() {
return description + "- Length: " + length + "m; Width: " + width + 'm';
}
}
Since this is homework I wont solve it in entirety but give you a foundation to work from...
public class House {
private List<Room> rooms;
public House() {
rooms = new ArrayList<Room>();
}
public void addRoom(...) {
Room room = new Room(...);
rooms.add(room);
}
public int numRooms() {
return rooms.size();
}
public Room getRoom(int roomNumber) {
return rooms.get(roomNumber);
}
}
public class Room {
public Room(...) {
...
}
}
Questions?