我是java新手并建立酒店系统。我有三个主要课程房间,楼层和酒店。每个楼层都有相同类型的房间,但房间号码除外。所以我只说10个房间,然后将它们全部交给我的5个楼层,然后将房间号分配到相应楼层的每个房间。 房间号码的第一个数字是楼层的数字,其余的数字是1-10。 但是,我在酒店的所有房间都被分配了5楼。
Heres我的代码片段。
class Floor
{
private int floorNo;
private Room[] Rooms;
public Floor()
{
floorNo = 0;
Rooms = null;
}
public Floor(int f, int t)
{
floorNo = f;
Rooms = new Room[t];
}
public void createRooms(Room[] R)
{
for (int i = 0; i < 10; i++)
{
Rooms[i] = new Room();
Rooms[i] = R[i];
}
}
public void setRoom(int i, int f, int r)
{
Rooms[i].setFloorNo(f);
Rooms[i].setRoomno(r);
}
}
public class Main
{
public static void main(String[] args)
{
Room[] Rooms = new Room[10];
for (int n = 0; n < 10; n++)
{
Rooms[n] = new Room();
}
}
Floor[] Floors = new Floor[5];
for (int n = 0; n < 5; n++)
{
Floors[n] = new Floor(n + 1, 10);
Floors[n].createRooms(Rooms);
for (int i = 0; i < 10; i++)
{
Floors[n].setRoom(i, n + 1, i + 1);
}
for (int n = 0; n < 5; n++)
{
Floors[n].print();
}
}
}
答案 0 :(得分:0)
class Floor
{
private int floorNo;
private Room[] Rooms;
public Floor()
{
floorNo=0;
Rooms=null;
}
public Floor(int f, int t)
{
floorNo=f;
Rooms = new Room[t];
}
public void createRooms(Room[] R)
{
for(int i = 0 ; i < 10; i ++)
{
Rooms[i] = new Room();
Rooms[i] = R[i];
}
}
public void setRoom(int i, int f, int r)
{
Rooms[i].setFloorNo(f);
Rooms[i].setRoomno(r,f);
}
}
class Room
{
public void setFloorNo(int i)
{
System.out.println("floor no is "+i);
}
public void setRoomno(int j,int k)
{
System.out.println("room no is "+k+""+j);
}
}
public class main
{
public static void main(String[] args)
{
Room [] Rooms= new Room[10];
for(int n = 0; n < 10; n++)
{
Rooms[n] = new Room ();
}
Floor [] Floors= new Floor[5];
for(int n=0; n<5; n++)
{
Floors[n] = new Floor (n+1,10);
Floors[n].createRooms(Rooms);
}
for(int n=0; n < 5; n++)
{
for(int i=0;i<10;i++)
Floors[n].setRoom(i, n+1, i+1);
enter code here
}
}
}
答案 1 :(得分:-1)
你可以尝试这样的smth:
public class Hotel {
private List<Floor> floors = new ArrayList<>();
public boolean addFloor(Floor floor) {
return floors.add(floor);
}
public static void main(String[] args) {
int totalFloors = 5;
int totalRoomsOnAFloor = 10;
final Hotel hotel = new Hotel();
int roomNumber = 0;
for (int floorNumber = 0; floorNumber < totalFloors; floorNumber++) {
Floor floor = new Floor(floorNumber);
for (int floorRoomNumber = 0; floorRoomNumber < totalRoomsOnAFloor; floorRoomNumber++) {
Room room = new Room(floor, roomNumber++);
floor.addRoom(room);
System.out.println("Added " + room.toString());
}
}
}
public static class Floor {
private List<Room> rooms = new ArrayList<>();
private int number;
public Floor(int number) {
this.number = number;
}
public boolean addRoom(Room room) {
return rooms.add(room);
}
public int getNumber() {
return number;
}
@Override
public String toString() {
return "Floor{" +
"number=" + number +
'}';
}
}
public class Room {
private final Floor floor;
private final int number;
public Room(Floor floor, int number) {
this.floor = floor;
this.number = number;
}
public int getNumber() {
return number;
}
public Floor getFloor() {
return floor;
}
@Override
public String toString() {
return "Room{" +
"number=" + number +
", on the floor=" + floor +
'}';
}
}
}
输出:
Added Room{number=0, on the floor=Floor{number=0}}
Added Room{number=1, on the floor=Floor{number=0}}
Added Room{number=2, on the floor=Floor{number=0}}
...
Added Room{number=47, on the floor=Floor{number=4}}
Added Room{number=48, on the floor=Floor{number=4}}
Added Room{number=49, on the floor=Floor{number=4}}