开发者

3-dimensions different types Map or List: List(ID=integer) of List(ID=integer) of int[]

开发者 https://www.devze.com 2022-12-14 06:49 出处:网络
I\'m going to build a hotel table. But I\'m having problems when trying to implement this in Java. My hotel has Level(int id) x Room(int id) x Field(String status, int counter)

I'm going to build a hotel table. But I'm having problems when trying to implement this in Java.

My hotel has Level(int id) x Room(int id) x Field(String status, int counter)

The same in php would look like:

$level=1; $room=2;
if(isset($hotel[$level][$room])) {
    print("Room: ".$level*100+$room);
    print(", Status: ".$hotel[$level][$room]['status']);
    print(", Total clients:".$hotel[$level][$room]['counter']);
}

And this print returns me(if room exist): "Room: 102, Status: Reserved, Total clients: 8";

Now I want to have the same in JAVA.

But the problem is, that I'm not able to build this:

int[][][] my hotel;

Because, I have the different types in my multi-dimensional array.

I tried to make sth like this:

Map<String, List<String>> myHotel = new HashMap<String, List<String>>();

Or:

List<List<List<String>>> myHotel;

But

out.println(
    myHot开发者_运维百科el.get(1).get(2).get("status") + "\n" +
    out.println(myHotel.get(1).get(2).get("status"));

Or even:

out.println("Status:" +
    myHotel.get(1).get(2).get(0) + "\tClients:" +
    myHotel.get(1).get(2).get(1)
);

Also how to put elements. I'm thinking about sth like: WHEN it's a MAP table:

myHotel.put(1).add(2).add(0, "Reserved")); 
// But better would be:
// myHotel.put(1).add(2).add("status", "Reserved")); 

Or WHEN it's a List<List<List<String>>>:

myHotel.add(1).add(2).add(0, "Reserved"));
// But better would be:
// myHotel.add(1).add(2).add("status", "Reserved")); 

Thanks for helping :)


I'd probably model the hotel as an object Hotel, the room as an object Room etc. rather than stacking everything together in a multi-tiered collection. That becomes very verbose very quickly, and as soon as you change the relationships then that change is reflected throughout your code.

Each object then contains references to its components (Hotel contains a list of Rooms etc.). Once you do this I think everything should become a lot clearer. Furthermore your Hotel object understands how to find Rooms, the Room objects understand how to get its attributes and your calling code becomes a lot less verbose, and a lot less dependent on the Hotel/Room implementation. e.g. you can do this:

Hotel hotel = new Hotel();
Set<Room> rooms = hotel.getFreeRooms(RoomType.NON_SMOKING);

and so your objects do the work for you (the client) rather than you navigating the object hierarchy and doing the work yourself.

That's the ultimate goal of OO. As soon as I find myself putting together collections of collections, that's often an indicator that a new object class is required.


You should create proper classes

import java.util.*;

class Hotel {
    public List<Level> levels = new ArrayList<Level>();
}

class Level {
    public List<Room> rooms = new ArrayList<Room>();
}

class Room {
    public Status status = Status.FREE;
    public int counter = 0;
}

enum Status {
    FREE, OCCUPIED
}

and then you use

Hotel hotel = new Hotel();
hotel.levels.add(new Level());
hotel.levels.get(0).rooms.add(new Room());
Room room = hotel.levels.get(0).rooms.get(0);
room.status = Status.OCCUPIED;
room.counter = 8;

et cetera...

 

NB: of course, OO purists will no come and tell you that all these fields need to be private and only be accessed through accessors. But I'd say it's okay if you start with this most simple design and later, as you learn more Java, evolve it to something more complex.


class HotelRoom{
     int roomnumber;
     int level;
     boolean reserved;
     int clientCount;

     public int getUniqueNumber(){
       return roomnumber + level*100;
     }
}

...
HotelRoom[][] hotel = new HotelRoom[floorCount][roomCount];

HotelRoom myRoom = hotel[floor][room];
System.out.print("room: " + myRoom.getUniqueNumber());
System.out.print(", Status: " myRoom.reserved);
System.out.print(", Total clients: " myRoom.clientCount);

Your design is pretty crazy, by the way.


Since level and room number is the key, I would represent room as a value object like this (at a minimum):

class Room {
    public static enum Status {RESERVED, AVAILABLE}

    private Status status;
    private int numberOfPersons;

    // Getters and setters
}

And the key as:

class RoomKey {
    private int levelNumber;
    private int roomNumber;

    public RoomKey(int levelNumber, int roomNumber) {
         this.leveNumber = levelNumber;
         this.roomNumber = roomNumber;
    }
}

And keep the data in a Map like:

Map<RoomKey, Room> rooms = getRoomMap(); 
Room room = rooms.get(new RoomKey(levelNumber, roomNumber))
0

精彩评论

暂无评论...
验证码 换一张
取 消