0

这与我之前的问题有些相关,但我意识到我需要在问题的早期处理嵌套问题,所以这就是我所在的位置。(我也离成为 Java 初学者不远了,所以请多多包涵)。

我正在为 5 个房间创建一个简单的预订系统,其中包含姓名、时间、房间号和日期。这必须使用嵌套的 TreeMaps 来完成。这是我看到的数据布局,其中括号代表 TreeMap 的边界:

(天,(房间号,(时间,名称)))

据我所知,我需要一张用于时间和名称的 TreeMap,每个房间一张,然后每天一张。这意味着每天每个房间一个时间/名称树图,这意味着 1 x 5 x 7 = 35 个树图。像这样:

{Mon,  [Room 1,  (0600, NameA
                  0630, NameB
                  0700, NameC)
        Room 2,  (0600, NameD
                  0630, NameE)
        Room 3,  (0600, NameF
                  0630, NameG)]
Tues,  [Room 1,  (0600, Name1
                  0630, Name2)
        Room 2,  (0600, Name3
                  0630, Name4
                  0700, Name5)]}

(不同的括号类型代表嵌套 TreeMaps 的边界)

得出这个结论后,我的下一个问题是遍历一个循环来创建所有这些 TreeMap。我似乎无法使用 for 循环动态生成 TreeMap,因为我无法将计数器的变量编号粘贴到新创建的 TreeMap 的名称上。

我确实有这个:

TreeMap keyDay = new TreeMap();
TreeMap keyRoom = new TreeMap();
TreeMap keyTime = new TreeMap();

但它只有三个,这显然不足以允许重复密钥 - 例如“0900”(时间密钥)或“房间 1”(房间密钥)的任何新条目都将覆盖旧条目。

有没有人有什么建议?将不胜感激:)

4

3 回答 3

3

我同意这真的超级糟糕。但是如果你想实现这个功能,我推荐 HashMaps。这是一个实现您想要的功能的类:

public class Reservations {

static final int  DAY_SUN = 0, DAY_MON = 1, DAY_TUE = 2, DAY_WED = 3, DAY_THU = 4, DAY_FRI = 5, DAY_SAT = 6;
static final int ROOM_1 = 0, ROOM_2 = 1, ROOM_3 = 2, ROOM_4 = 3, ROOM_5 = 4;
private static HashMap<Integer[], String[]> hMap = new HashMap<Integer[], String[]>();


static String [] getStringForValue(Integer[] i){
    return hMap.get(i);
}

static TreeSet<String> getOrderedOutputStrings(){
    TreeSet<String> set = new TreeSet<String>();
    for(Entry<Integer[],String[]> e : hMap.entrySet()){
        int day_int = Reservations.getDay(e.getKey());
        int room_int = Reservations.getRoom(e.getKey());
        int time = Reservations.getTime(e.getValue());
        String name = Reservations.getGuestName(e.getValue());
        String day = Reservations.dayToString(day_int);
        String room = Reservations.roomToString(room_int);
        if(time > 0)
        set.add("DAY: " + "(" + day_int + ")" + day + " (" + room_int + ")"+ "ROOM: " + room + " :: " + name + " @ " + time);
    }
    return set;
}



static void setupMap() {
    for (int day = 0; day < 7; day++) {
        for (int room = 0; room < 5; room++) {
            addGuest(day, room, (int)(Math.random()*1000), "Bob TestCase");
        }
    }
}

static void addGuest(int day, int room, int time, String name) {
    Integer[] ref = new Integer[2];
    ref[0] = day;
    ref[1] = room;
    String[] s = new String[2];
    s[0] = Integer.toString(time);
    s[1] = name;
    hMap.put(ref, s);
}

static String[] lookupRoom(int day, int room) {
    Integer[] i = new Integer[2];
    i[0] = day;
    i[1] = room;
    return hMap.get(i);
}

static int getDay(Integer[] i){
    return i[0];
}

static int getRoom(Integer[] i ){
    return i[1];
}

static int getTime(String[] s) {
    return Integer.parseInt(s[0]);
}

public static String getGuestName(String[] s) {
    return s[1];
}

public static String dayToString(int i){
    switch(i){
        case 0:
            return "SUNDAY";
        case 1:
            return "MONDAY";
        case 2:
            return "TUESDAY";
        case 3:
            return "WEDNESDAY";
        case 4:
            return "THURSDAY";
        case 5:
            return "FRIDAY";
        case 6:
            return "SATURDAY";
        default:
            return null;
    }
}

public static String roomToString(int i){
    switch(i){
        case 0:
            return "ROOM ONE";
        case 1:
            return "ROOM TWO";
        case 2:
            return "ROOM THREE";
        case 3:
            return "ROOM FOUR";
        case 4:
            return "ROOM FIVE";
        default:
            return null;
    }
}

}

这是一个运行 Reservations 类的 main:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Reservations.setupMap(); //Run to test TODO: remove setupMap() from Reservation class 
    Reservations.addGuest(Reservations.DAY_MON, Reservations.ROOM_2, 1230, "John Doe");

    TreeSet<String> set = new TreeSet<String>();
    for(String s: Reservations.getOrderedOutputStrings()){
        System.out.println(s + "\n");
    }


}
}

最后,使用 SetupMap 和单个条目,它产生:

DAY: (0)SUNDAY (0)ROOM: ROOM ONE :: Bob TestCase @ 423

DAY: (0)SUNDAY (1)ROOM: ROOM TWO :: Bob TestCase @ 637

DAY: (0)SUNDAY (2)ROOM: ROOM THREE :: Bob TestCase @ 731

DAY: (0)SUNDAY (3)ROOM: ROOM FOUR :: Bob TestCase @ 424

DAY: (0)SUNDAY (4)ROOM: ROOM FIVE :: Bob TestCase @ 427

DAY: (1)MONDAY (0)ROOM: ROOM ONE :: Bob TestCase @ 335

DAY: (1)MONDAY (1)ROOM: ROOM TWO :: Bob TestCase @ 580

DAY: (1)MONDAY (1)ROOM: ROOM TWO :: John Doe @ 1230

DAY: (1)MONDAY (2)ROOM: ROOM THREE :: Bob TestCase @ 700

DAY: (1)MONDAY (3)ROOM: ROOM FOUR :: Bob TestCase @ 556

DAY: (1)MONDAY (4)ROOM: ROOM FIVE :: Bob TestCase @ 92

DAY: (2)TUESDAY (0)ROOM: ROOM ONE :: Bob TestCase @ 84

DAY: (2)TUESDAY (1)ROOM: ROOM TWO :: Bob TestCase @ 964

DAY: (2)TUESDAY (2)ROOM: ROOM THREE :: Bob TestCase @ 981

DAY: (2)TUESDAY (3)ROOM: ROOM FOUR :: Bob TestCase @ 294

DAY: (2)TUESDAY (4)ROOM: ROOM FIVE :: Bob TestCase @ 22

DAY: (3)WEDNESDAY (0)ROOM: ROOM ONE :: Bob TestCase @ 216

DAY: (3)WEDNESDAY (1)ROOM: ROOM TWO :: Bob TestCase @ 838

DAY: (3)WEDNESDAY (2)ROOM: ROOM THREE :: Bob TestCase @ 198

DAY: (3)WEDNESDAY (3)ROOM: ROOM FOUR :: Bob TestCase @ 494

DAY: (3)WEDNESDAY (4)ROOM: ROOM FIVE :: Bob TestCase @ 796

DAY: (4)THURSDAY (0)ROOM: ROOM ONE :: Bob TestCase @ 617

DAY: (4)THURSDAY (1)ROOM: ROOM TWO :: Bob TestCase @ 866

DAY: (4)THURSDAY (2)ROOM: ROOM THREE :: Bob TestCase @ 799

DAY: (4)THURSDAY (3)ROOM: ROOM FOUR :: Bob TestCase @ 242

DAY: (4)THURSDAY (4)ROOM: ROOM FIVE :: Bob TestCase @ 178

DAY: (5)FRIDAY (0)ROOM: ROOM ONE :: Bob TestCase @ 491

DAY: (5)FRIDAY (1)ROOM: ROOM TWO :: Bob TestCase @ 16

DAY: (5)FRIDAY (2)ROOM: ROOM THREE :: Bob TestCase @ 838

DAY: (5)FRIDAY (3)ROOM: ROOM FOUR :: Bob TestCase @ 63

DAY: (5)FRIDAY (4)ROOM: ROOM FIVE :: Bob TestCase @ 860

DAY: (6)SATURDAY (0)ROOM: ROOM ONE :: Bob TestCase @ 73

DAY: (6)SATURDAY (1)ROOM: ROOM TWO :: Bob TestCase @ 759

DAY: (6)SATURDAY (2)ROOM: ROOM THREE :: Bob TestCase @ 15

DAY: (6)SATURDAY (3)ROOM: ROOM FOUR :: Bob TestCase @ 115

DAY: (6)SATURDAY (4)ROOM: ROOM FIVE :: Bob TestCase @ 21

该结果是在不到一秒钟的时间内产生的。我保证这比嵌套的 TreeMaps 效率更高。祝你好运!

于 2011-03-27T20:32:54.313 回答
0

不是真正的答案,而是一个想法;)

事实上,正如 xappymah 在评论中指出的那样,更自然的方法是为您的任务发明特定领域的类型。喜欢:

interface Room {
    String getName();
    public void bookRoom(Booking booking) throws AlreadyBookedException;
}

interface Person {
    String getName();
}

class Interval {
    Date startTime;
    long duration;
}

interface Booking {
    Interval getInterval();
    Room getBookedRoom() throws NotBookedYetException;
    Set<Person> getParticipants();
}

interface BookingUtils {
    Set<Booking> getBookingsForInterval(Interval interval);
    Set<Booking> getBookingsOfRoom(Room room);
}
于 2011-03-27T18:50:03.777 回答
0

有人会假设这一天实际上是一个枚举,所以顶级地图应该是一个 EnumMap

Map<Day, Map<String, Map<Integer, String>>> bookings = new EnumMap<Day, Map<String, Map<Integer, String>>>(Day.class);
于 2011-03-27T18:45:17.983 回答