在java中为房间分配床位

在java中为房间分配床位,java,Java,我有一个作业问题,我需要一些帮助。这是一个养老院的模型,我们需要把病人分到床上,然后把床分到一个房间,再分到一个病房。这些房间可以容纳不同数量的床,1张、2张或4张 我已将患者及其数据分配到以下班级的病床 public class Beds protected int bed; protected Patients patient; protected boolean occupied; protected int totalBeds;

我有一个作业问题,我需要一些帮助。这是一个养老院的模型,我们需要把病人分到床上,然后把床分到一个房间,再分到一个病房。这些房间可以容纳不同数量的床,1张、2张或4张

我已将患者及其数据分配到以下班级的病床

public class Beds 

    protected int bed;
    protected Patients patient;
    protected boolean occupied;
    
    protected int totalBeds;
    
    protected static ArrayList<Beds> beds = new ArrayList<Beds>();
    
    protected Beds(int Bed, Patients Patient, boolean Occupied)
    {
        this.bed = Bed;
        this.setPatient(Patient);
        this.occupied = Occupied;

    }

    public int getBed()
    {
        return bed;
    }
    
    public boolean getOccupied()
    {
        return occupied;
    }
    
    public Patients getPatient() 
    {
        return patient;
    }

    public void setPatient(Patients patient) 
    {
        this.patient = patient;
        occupied = true;
    }
    
    public void assignBed(int oldBed, int newBed)
    {
        if ( !beds.get(newBed).getOccupied())
        {   
            beds.get(newBed).setPatient(patient);
            beds.get(oldBed).setPatient(null);
        }
        else
            System.out.println("Sorry that bed is taken. Please try again");
    }       
    
    public String toString()
    {
        return new String( "bed = " + bed + "patient = " + patient + "occuplied = " + occupied);
        
    }
}
公共类床位
保护床;
受保护病人;
受保护的布尔值被占用;
保护床;
受保护的静态ArrayList beds=新ArrayList();
受保护床位(内部床位、患者、有人占用)
{
这张床;
这个病人(病人);
有人占了;
}
公共int getBed()
{
返回床;
}
公共布尔GetOccupled()
{
返回已占用;
}
公共病人
{
返回病人;
}
公立医院病人(病人)
{
这个病人=病人;
占领=真;
}
公共床(内部旧床、内部新床)
{
如果(!beds.get(newBed.getocculation())
{   
床位。获取(新生儿)。设置患者(患者);
beds.get(oldBed).setPatient(null);
}
其他的
System.out.println(“对不起,床上有人,请再试一次”);
}       
公共字符串toString()
{
返回新字符串(“bed=“+bed+”patient=“+patient+”occuplied=“+occuplied”);
}
}
问题是试图将床分配给不同大小的房间

public class Rooms 
{
    protected int roomNo;
    protected int totalBeds; 
    protected boolean full;

    protected static ArrayList<Rooms> rooms = new ArrayList<>(); 
    
    protected ArrayList<Beds> beds = new ArrayList<>();
    
    protected static Beds beds;
    
    public Rooms(int RoomNo, int totalBeds, ArrayList<Beds> Beds, boolean Full) 
    {
        this.totalBeds = totalBeds;
        this.roomNo = RoomNo;
        this.full = Full;
        this.beds = Beds;
    }
    
    int getRoomNo()
    {
        return roomNo;
    }

    int getTBeds()
    {
        return totalBeds;
    }
    
    boolean getFull()
    {
        return full;
    }
    
    ArrayList<Beds> getBeds()
    {
        return beds;
    }
公共教室
{
受保护的国际房间号;
保护床;
受保护布尔满;
受保护的静态ArrayList房间=新建ArrayList();
受保护的ArrayList床位=新建ArrayList();
保护静态床;
公共房间(int RoomNo、int totalBeds、ArrayList Beds、boolean Full)
{
this.totalBeds=totalBeds;
this.roomNo=roomNo;
this.full=full;
这个。床=床;
}
int getRoomNo()
{
返回房间号;
}
int gettbeeds()
{
返回总床位;
}
布尔getFull()
{
全额退还;
}
ArrayList getBeds()
{
返回床;
}

问题是,我一直不知道如何从bed类中分配床位,以便与room类一起工作,我已经在圈中编码了几个小时。

为什么不删除班级床位

你会有一张班级病床,一间教室和一个班级病人

教室有一个属性:arraylist of Bed

类Patient有一个属性:Bed(也可以添加房间属性)

班级床位有一个属性:Patient(您还可以添加房间属性)

然后你举例说明你的课程:房间1,病人1,床1。。。 然后用对象填充属性:Room_1.add(Patient_1,Room_1)或Patient_1.Room=Room_1


它回答了你的问题吗?

我想让你困惑的是,你在类中使用复数,比如床和房间。重新编写代码,让一个类来表示一件事,比如床、房间和病人。房间包含一张床的列表。床可能包含病人,也可能不包含病人。我认为这可能更容易推理。你的alg是什么在一个房间里给病人安排床位的方法?我可以想到两种方法:首先安排病人把房间挤得很近(护士最容易),或者把病人一个安排到一个房间,直到病人比房间多(最好是病人)。患者被分配到1号床,然后是2号床,依此类推。然后,1号床被分配到1号房间。2号床和3号床被分配到2号房间。4号床、5号床、6号床和7号床被分配到3号房间,依此类推,但护士可以随时将患者转移到任何房间的任何床上