put 语句后地图未更新
Map is not being updated after put statement
好的,正如标题所说,我真的不明白为什么我的地图没有更新。
我有一个 "hotel" 有房间。每个房间可以有一个顾客。酒店房间在一开始就被填充,并给他们 Key (1-10) 和 Customer (最初是 null)
public class Hotel {
public Map<Integer, Room> map = new HashMap<>();
public static void main(String[] args) {
new Hotel().run();
}
public void run() {
// create rooms
createRooms();
// add customer
addCustmer(2, "Plastman");
showRooms();
}
public void showRooms() {
for (Map.Entry<Integer, Room> entry : map.entrySet()) {
Integer roomNumber = entry.getKey();
Room room = entry.getValue();
System.out.println("Room: " + roomNumber + " Customer " + room.getCutomer() );
}
}
public void addCustmer(Integer roomNumber, String customer) {
// check if room is empty.
Room room = map.get(roomNumber);
if(room.getCutomer() == null || room.getCutomer().equals("")) {
// room is vacant.
System.out.println("Room is vacant. Adding customer: " + customer);
map.put(roomNumber, new Room(customer));
} else {
// room is NOT vacant.
System.out.println("Room is NOT vacant");
}
}
private void createRooms() {
for(int i = 1; i < 11; i ++) {
map.put(i,new Room(""));
}
}
在您的 room 方法中,您需要将参数设为小写。这可能是问题所在。
public Room(String customer) {
setCustomer(customer);
}
好的,正如标题所说,我真的不明白为什么我的地图没有更新。
我有一个 "hotel" 有房间。每个房间可以有一个顾客。酒店房间在一开始就被填充,并给他们 Key (1-10) 和 Customer (最初是 null)
public class Hotel {
public Map<Integer, Room> map = new HashMap<>();
public static void main(String[] args) {
new Hotel().run();
}
public void run() {
// create rooms
createRooms();
// add customer
addCustmer(2, "Plastman");
showRooms();
}
public void showRooms() {
for (Map.Entry<Integer, Room> entry : map.entrySet()) {
Integer roomNumber = entry.getKey();
Room room = entry.getValue();
System.out.println("Room: " + roomNumber + " Customer " + room.getCutomer() );
}
}
public void addCustmer(Integer roomNumber, String customer) {
// check if room is empty.
Room room = map.get(roomNumber);
if(room.getCutomer() == null || room.getCutomer().equals("")) {
// room is vacant.
System.out.println("Room is vacant. Adding customer: " + customer);
map.put(roomNumber, new Room(customer));
} else {
// room is NOT vacant.
System.out.println("Room is NOT vacant");
}
}
private void createRooms() {
for(int i = 1; i < 11; i ++) {
map.put(i,new Room(""));
}
}
在您的 room 方法中,您需要将参数设为小写。这可能是问题所在。
public Room(String customer) {
setCustomer(customer);
}