将一组对象输入到对象的Arraylist中并更改属性。

huangapple go评论53阅读模式
英文:

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&#39;m struggling with this since yesterday...

I&#39;m working right now on a Hotel Management System project.
What i&#39;m trying to do is, to put an array[] Clients into an ArrayList of Rooms and then set this room attribute to &#39;occupied = true&#39; and save that so if i try to use that room for other client it doesn&#39;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&#39;ve got on my Main so far... i&#39;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 &lt; people; i++) {
        System.out.println(&quot;Enter ID&quot; + (i+1));
        id = input.next();

        System.out.println(&quot;Enter name &quot; + (i+1));
        name= input.next();

        System.out.println(&quot;Enter last name &quot; + (i+1));
        lastName= input.next();

        array[i] = new Client(id,name,lastName);

    }

    System.out.print(&quot;Assign to room number... : &quot;);
    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&#39;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 &lt; roomNumbers.length; i++) {
           if(roomNumbers[i].isOccupied() == true) {
                 roomList[i][0] = &quot;&quot; + roomNumbers[i];
                 roomList[i][1] = &quot;occupied&quot;;
           } else {
                 roomList[i][0] = &quot;&quot; + roomNumbers[i];
                 roomList[i][1] = &quot;vacant&quot;;
           }
        }
        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.

huangapple
  • 本文由 发表于 2020年4月11日 04:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定