好吧,因为标题说我不明白为什么我的地图没有更新。
我有一个“酒店”,有房间。每个房间都有客户。酒店房间一开始就填充,给他们Key(1-10)和Customer(最初为空)
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(""));
}
}
答案 0 :(得分:2)
在你的房间方法中,你需要使参数小写。这可能是个问题。
public Room(String customer) {
setCustomer(customer);
}