英文:
Enter an array of objects into an Arraylist of objects and change attribute
问题
我希望有人能帮助我,我从昨天开始就在努力解决这个问题...
我正在开发一个酒店管理系统项目。
我正在尝试做的是,将一个数组[] Clients放入一个Rooms的ArrayList中,然后将此房间属性设置为'occupied = true',并保存,这样如果我试图为其他客户使用该房间,它就不会让我使用。
房间类
```java
public class Room {
private int number;
private float price = 30.5f;
private boolean occupied = false;
private Client[] hosts;
public Room(int number, Client[] hosts) {
this.number = number;
this.hosts = hosts;
}
public void setOccupied() {
this.occupied = true;
}
}
客户类
public class Client {
private String id;
private String name;
private String lastName;
public Client(String id, String name, String lastName) {
this.id = id;
this.name = name;
this.lastName = lastName;
}
}
这是我在主函数中的部分内容... 我正在调用下一个函数
public void checkIn(ArrayList<Room> myRooms) {
int roomNumber;
String id;
String name;
String lastName;
Scanner input = new Scanner(System.in);
int people = input.nextInt();
Client[] array = new Client[people];
for (int i = 0; i < people; i++) {
System.out.println("Enter ID" + (i + 1));
id = input.next();
System.out.println("Enter name " + (i + 1));
name = input.next();
System.out.println("Enter last name " + (i + 1));
lastName = input.next();
array[i] = new Client(id, name, lastName);
}
System.out.print("Assign to room number... : ");
roomNumber = input.nextInt();
myRooms.add(new Room(roomNumber, array));
// 这里我尝试过:
// room.set().setOccupied();
// room.set(roomNumber).setOccupied();
// 但 .set() 需要一个索引...
}
一旦我完成这个,我想要创建一个函数来显示已占用的房间列表。
请原谅我的英语,因为我是西班牙人。
<details>
<summary>英文:</summary>
i hope someone can help me, i'm struggling with this since yesterday...
I'm working right now on a Hotel Management System project.
What i'm trying to do is, to put an array[] Clients into an ArrayList of Rooms and then set this room attribute to 'occupied = true' and save that so if i try to use that room for other client it doesn't let me.
Room class
public class Room {
private int number;
private float price= 30.5f;
private boolean occupied = false;
private Client[] hosts;
public Room(int number, Client[] hosts) {
this.number= number;
this.hosts= hosts;
}
public void setOccupied() {
this.occupied = true;
}
}
Client class
public class Client {
private String id;
private String name;
private String lastName;
public Client(String id, String name, String lastName) {
this.id = id;
this.name= name;
this.lastName= lastName;
}
}
This is what i've got on my Main so far... i'm calling the next function
public void checkIn(ArrayList<Room> myRooms){
int roomNumber;
String id;
String name;
String lastName;
Scanner input = new Scanner(System.in);
int people = input.nextInt();
Client[] array = new Client[people];
for (int i = 0; i < people; i++) {
System.out.println("Enter ID" + (i+1));
id = input.next();
System.out.println("Enter name " + (i+1));
name= input.next();
System.out.println("Enter last name " + (i+1));
lastName= input.next();
array[i] = new Client(id,name,lastName);
}
System.out.print("Assign to room number... : ");
roomNumber = input.nextInt();
myRooms.add(new Room(roomNumber, array));
//here i tried doing:
//room.set().setOccupied();
//room.set(roomNumber).setOccupied();
//but .set() expects an index...
}
Once i have this, i want to create a function that shows a list of rooms that are occupied
Excuse my english since i'm Spanish
</details>
# 答案1
**得分**: 0
有几种方法可以解决这个问题,可能我会这样做,重载我的房间构造函数,这样在实例化时就可以设置是否被占用,就像这样:
```java
public Room(int number, Client[] hosts, boolean occupied)
{
this.number = number;
this.hosts = hosts;
this.occupied = true;
}
然后在你的checkIn()
方法中,在列表中实例化房间时添加true
:
myRooms.add(new Room(roomNumber, array, true));
英文:
Several ways to tackle this, probably what i would do is overload my room constructor so that you can set if it's occupied or not once you instantiate it like:
public Room(int number, Client[] hosts, boolean occupied)
{
this.number = number;
this.hosts = hosts;
this.occupied = true;
}
and in your checkIn()
method just add true when instantiating your room inside the list:
myRooms.add(new Room(roomNumber, array, true));
答案2
得分: 0
由于在调用myRooms.add(new Room(roomNumber, array));
时没有创建一个可引用的 Room 实例,因此接下来不能使用room.setOccupied();
。你可以通过两种方法来解决这个问题:首先,如Bahij.Mik所说,你可以在 Room 构造函数中调用this.occupied = true;
。或者,你可以在你的 checkIn 方法中简单地创建一个新的 Room 实例。代码如下所示:
Room newRoom = new Room(roomNumber, array);
newRoom.setOccupied();
至于创建一个用于检查哪些房间已被占用的函数,可以创建一个房间号的数组,然后尝试以下代码:
public String[][] findOccupiedRooms(int[] roomNumbers) {
String[][] roomList = new String[roomNumbers.length][2];
for(int i = 0; i < roomNumbers.length; i++) {
if(roomNumbers[i].isOccupied() == true) {
roomList[i][0] = "" + roomNumbers[i];
roomList[i][1] = "occupied";
} else {
roomList[i][0] = "" + roomNumbers[i];
roomList[i][1] = "vacant";
}
}
return roomList;
}
这将返回一个二维数组,其中存储了房间号以及它们是否已占用或空置。不确定是否完全符合你的要求,但希望能有所帮助。
英文:
Since you aren't creating a reference-able instance of Room when you call
myRooms.add(new Room(roomNumber, array));
you can't then say
room.setOccupied();
You can fix this two ways: First, as Bahij.Mik said, you could call this.occupied = true;
in the Room constructor. Or, you could simply create a new instance of a Room in your checkIn method. This would look like:
Room newRoom = new Room(roomNumber, array);
newRoom.setOccupied();
whenever someone checks in.
As for creating a function to check which rooms are occupied, make an array of room numbers and then try:
public String[][] findOccupiedRooms(int[] roomNumbers) {
String[][] roomList = new String[roomNumbers.length][1];
for(int i = 0; i < roomNumbers.length; i++) {
if(roomNumbers[i].isOccupied() == true) {
roomList[i][0] = "" + roomNumbers[i];
roomList[i][1] = "occupied";
} else {
roomList[i][0] = "" + roomNumbers[i];
roomList[i][1] = "vacant";
}
}
return roomList[][];
}
This returns a 2-dimensional array that stores the room number and whether it is occupied or vacant. Not sure if this is exactly what you are looking for, but I hope it helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论