Java 如何将主程序中的HashMap中的数据读入另一个类
这是我的主要代码。我正在尝试设计一个房间预订系统,所以我使用HashMap来获取我的房间列表Java 如何将主程序中的HashMap中的数据读入另一个类,java,Java,这是我的主要代码。我正在尝试设计一个房间预订系统,所以我使用HashMap来获取我的房间列表 import java.util.*; public class MiniProject { private static Scanner inp = null; public static void main(String[] args) { Reservation person = null; inp = new Scanner( System.in );
import java.util.*;
public class MiniProject {
private static Scanner inp = null;
public static void main(String[] args) {
Reservation person = null;
inp = new Scanner( System.in ); //input
System.out.println( "Welcome to the Room Reservation System" );
System.out.print( "Username: " );
String name = inp.nextLine();
String admin = "admin";
if(name.equals(admin))
{
Admin a = new Admin(name);
System.out.print("Enter password: ");
String password=inp.nextLine();
a.checkPassword(password);
}
else
{
person = new Reservation(name);
System.out.println();
}
HashMap<String, Integer> room = new HashMap<>();
room.put("Lecture Room 1", 1);
room.put("Lecture Room 2", 2);
room.put("Lecture Room 3", 3);
room.put("Lecture Room 4", 4);
room.put("Lecture Room 5", 5);
room.put("Lecture Room 6", 6);
room.put("Lecture Room 7", 7);
room.put("Lecture Room 8", 8);
room.put("Lecture Room 9", 9);
room.put("Lecture Room 10", 10);
room.put("Lecture Hall 1", 11);
room.put("Lecture Hall 2", 12);
room.put("Lecture Hall 3", 13);
room.put("Lecture Hall 4", 14);
room.put("Lecture Hall 5", 15);
room.put("Lecture Hall 6", 16);
room.put("Lecture Hall 7", 17);
room.put("Lecture Hall 8", 18);
room.put("Lecture Hall 9", 19);
room.put("Lecture Hall 10", 20);
room.put("Computer Training Lab 1", 21);
room.put("Computer Training Lab 2", 22);
room.put("Computer Training Lab 3", 23);
import java.util.*;
公共类小型项目{
专用静态扫描仪inp=null;
公共静态void main(字符串[]args){
预订人=空;
inp=新扫描仪(System.in);//输入
System.out.println(“欢迎使用客房预订系统”);
系统输出打印(“用户名:”);
字符串名称=inp.nextLine();
字符串admin=“admin”;
if(name.equals(admin))
{
管理员a=新管理员(姓名);
系统输出打印(“输入密码:”);
字符串密码=inp.nextLine();
a、 检查密码(密码);
}
其他的
{
person=新预订(姓名);
System.out.println();
}
HashMap房间=新建HashMap();
会议室(1号会议室);
会议室(2号会议室);
会议室。会议室(“3号演讲室”,3号);
会议室(演讲室4,4);
会议室(5号会议室,5号会议室);
会议室(6号会议室),6号会议室;
会议室(7号演讲室,7号);
会议室(8号演讲室,8号);
会议室(9号会议室,9号会议室);
会议室(10号会议室),10号会议室;
会议室(1号演讲厅,11号);
会议室(2号演讲厅,12);
会议室(第3演讲厅,13);
会议室(4号演讲厅,14号);
会议室(第5、15号演讲厅);
会议室(第6、16号演讲厅);
会议室(第7、17号演讲厅);
会议室(8号演讲厅,18号);
会议室(第9演讲厅,19);
会议室(10号、20号演讲厅);
put室(“计算机培训实验室1”,21);
put室(“计算机培训实验室2”,22);
put室(“计算机培训实验室3”,23);
我是否正确创建了可用的房间
我想在另一个类中使用房间呼叫RoomChecker,以便检查房间的可用性。所有房间最初都是空的。INTEGER值意味着一个id。因此,如果系统预订了号码,房间将不可用。但是我如何在RoomChecker类中阅读房间列表?确保您理解局部变量和实例变量(或字段)以及Java中的对象和数据封装。web上有很多很好的示例。最好不要将所有内容都写在主方法中,而是写在几个按它们所做的事命名的方法中(例如fillRoomsWithData())
针对您的具体问题:在类迷你项目中声明Map rooms(注意名称中的复数形式)。创建RoomChecker实例后,您可以将房间传递给它(首先阅读关于Setter的内容)。从那里,您可以访问以前传入的值。如果您想在另一个类中使用
HashMap
,只需将其提供给该类即可。例如,使用HashMap
初始化RoomChecker
类,以便它可以使用其中的所有变量
public class RoomChecker {
HashMap<String,Integer> rooms;
public RoomChecker(HashMap<String,Integer> rooms)
{
this.rooms = rooms;
}
//... Other methods to perform stuff on the rooms
public boolean hasRoom(String roomName)
{
return rooms.containsKey(roomName);
}
public boolean hasRoom(Integer roomNumber)
{
return rooms.containsValue(roomNumber);
}
//... etc
}
公共类RoomChecker{
地图室;
公共RoomChecker(HashMap房间)
{
这个房间=房间;
}
//…在房间里表演的其他方法
公共布尔hasRoom(字符串roomName)
{
返回房间。containsKey(房间名称);
}
公共布尔hasRoom(整数roomNumber)
{
返回房间。包含值(房间号);
}
//…等等
}
我认为你想要完成的最好的方法是有一个单独的类
Room
,它有几个字段,比如boolean-occumped
,int-roomNumber
,String-roomName
等等,并制作一个房间的数组列表,而不是HashMap
,我想,你应该uld可以从更简单的方式开始。但是如果您需要创建预订系统,您应该创建带有字段的classRoom
int-id;
字符串名;
布尔保留
getter、setter和方法
bool isReserved();
在SystemReservation
类中,您应该创建arraylist
,其中包含类Room
的对象