ok here the code
import java.util.ArrayList;
import java.util.List;
public class Hotel {
private static String name;
private static String address;
static List[] floors=new ArrayList[3];
int maxRoomPerFloor=4;
private static int roomPerFloor1;
private static int roomPerFloor2;
private static int roomPerFloor3;
public Hotel(String name, String address){
this.name=name;
this.address=address;
roomPerFloor1=0;
roomPerFloor2=0;
roomPerFloor3=0;
}
public int getroomPerFloor2(){
return roomPerFloor2;
}
public int getroomPerFloor1(){
return roomPerFloor2;
}
public int getroomPerFloor3(){
return roomPerFloor2;
}
public void incroomPerFloor1(){
roomPerFloor1++;
}
public void incroomPerFloor2(){
roomPerFloor2++;
}
public void incroomPerFloor3(){
roomPerFloor3++;
}
public static String getName() {
return name;
}
public static String getAddress() {
return address;
}
public String toString() {
return "Hotel "+getName()+" is at address "+ getAddress() ;
}
public boolean addRoom(int floor, Room r){
for(int i=0;i<floors.length;i++){
if ((floors.equals(floor))&&
(roomPerFloor1<maxRoomPerFloor))
{
floors[i].add(r);
/*
switch (floor) {
case 1: incroomPerFloor1();
break;
case 2: incroomPerFloor2();
break;
case 3: incroomPerFloor3();
break;
default:
break;
}*/
}
}
incroomPerFloor1();
for(int i=0;i<floors.length;i++){
if ((floors.equals(floor))&&
(getroomPerFloor2()<maxRoomPerFloor)){
floors[i].add(r);
}
}
incroomPerFloor2();
for(int i=0;i<floors.length;i++){
if ((floors.equals(floor))&&
(roomPerFloor3<maxRoomPerFloor)){
floors[i].add(r);
}
}
incroomPerFloor3();
if((roomPerFloor1>maxRoomPerFloor) ||
(roomPerFloor2>maxRoomPerFloor) ||
(roomPerFloor3>maxRoomPerFloor)){
System.out.println("full floor!!!");
return false;
}
return true;
}
public static void main(String[]args)throws Exception{
Hotel hotel=new Hotel("Hilton","Brodway 1001");
//hotel.toString();
System.out.println("Hotel "+getName()+" is at address "+getAddress());
Room r=new Room(1,2,"view on beach");
System.out.println("room number: "+r.getRoomNumber()+" has number of beds: "
+r.getNumberOfBeds());
hotel.addRoom(0, r);
hotel.addRoom(0, r);
hotel.addRoom(0, r);
hotel.addRoom(0, r);
hotel.addRoom(0, r);
System.out.println("number of rooms on floor0: "+roomPerFloor1);
System.out.println("number of rooms on floor1: "+roomPerFloor2);
System.out.println("number of rooms on floor2: "+roomPerFloor3);
}
}
and the Room class so u can run it
import java.util.ArrayList;
import java.util.List;
public class Room {
int roomNumber;
int numberOfBeds;
String description;
public Room(int num,int beds, String desc){
roomNumber=num;
numberOfBeds=beds;
description=desc;
}
Question is the if statement if ((floors[i].equals(floor)) was giving me null pointer so i changed it with if ((floors.equals(floor)).
The program is actually compiling and running but it seems somting is wrong he is adding rooms to every floor?