有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中为房间分配床位

我有一个作业问题,我需要一些帮助。这是一个养老院的模型,我们需要把病人分到床上,然后把床分到一个房间,再分到一个病房。这些房间可以容纳不同数量的床,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);
        
    }
}

问题是试图将床分配给不同大小的房间

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;
    }

问题是,我一直无法弄清楚如何将卧床课的床位分配给客房课,我已经在几个小时内循环编码了


共 (1) 个答案

  1. # 1 楼答案

    你为什么不把班级的床位移走

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

    教室有一个属性:arraylist of Bed

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

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

    然后你举例说明你的课程:房间1,病人1,床1。。。 然后用对象填充属性:Room_1。添加(患者1、病房1)或患者1。房间=房间1

    这能回答你的问题吗