when i put different key into hashmap after that i use get(key), different key shows the same result, i have no idea why this happen
I have two class, Room class and Office class
This is my Room Class
import java.util.HashMap;
import java.util.Scanner;
public class Room {
private static int roomNumber;
private static String description;
public int getRoomNum(){return roomNumber;}
public String getDescription(){return description;}
public void setDescription(String description){
this.description=description;
}
public void setRoomNum(int roomNumber){
this.roomNumber=roomNumber;
}
public Scanner sc = new Scanner(System.in);
public static HashMap<Integer, Room> room = new HashMap<>();
public void askData(){
System.out.println("ENTER ROOM NUMBER:");
this.roomNumber=sc.nextInt();
sc.nextLine();
System.out.println("Enter description:");
this.description = sc.nextLine();
}
public String toString(){
return this.getRoomNum()+": "+this.getDescription();
}
public static void main(String[] args) {
Room r = new Office();
r.room.put(1,new Office(1,"java","asdfw"));
r.room.put(2,new Office(2,"jafasdfva","ghasdfasow"));
//----------------------My problem is here
Room s = r.room.get(1);
Room s2 = r.room.get(2);
System.out.println(s.getRoomNum()+" . "+s.getDescription());
System.out.println(s2.getRoomNum()+" . "+s2.getDescription());
for(Integer keey: room.keySet()){
System.out.println(r.room.get(roomNumber));
}
}
// following are the result
//2 . jafasdfva
//2 . jafasdfva
//2: jafasdfva. ghasdfasow
//2: jafasdfva. ghasdfasow
//-------------------------------------
}
}
i have int key 1 and 2, but when i get key 1 and 2, result will show key 2 two times, i dont know why this happend
//------------------------------------
This is my Office class
import java.util.ArrayList;
import java.util.HashMap;
public class Office extends Room {
private String staffMembers;
public String enter,check;
public Office(int roomNumber,String description, String staffMembers){
this.setRoomNum(roomNumber);
this.setDescription(description);
this.staffMembers=staffMembers;
}
public Office(){}
public void askData(){
super.askData();
StringBuilder sb = new StringBuilder();
do{
System.out.println("Enter members in this room: ");
enter =sc.nextLine();
sb.append(enter+", ");
System.out.println("Enter Y to continule.");
check = sc.nextLine();
}while(check.equalsIgnoreCase("Y"));
this.staffMembers=sb+"";
this.room.put(this.getRoomNum(), new Office(this.getRoomNum(),this.getDescription(),staffMembers));
}
public String toString(){
return this.getRoomNum()+": "+this.getDescription()+". "+
this.staffMembers;
}
}