【问题标题】:Simulating hospital in Java (Priority Queue)用Java模拟医院(优先队列)
【发布时间】:2013-03-06 08:11:57
【问题描述】:

我目前在学校学习 ADT,为了完成一项任务,我必须在医院模拟急诊室。我目前有我的病人类如下:

public class Patient implements Comparable<Patient>{

    private String name;
    private int condition;

    public Patient( String n, int c ){
        this.name = name;
        this.condition = condition;
    }

    public String toString(){
        return name;
    }

    public int boundary(int condition) {
        if (condition > 17){
            return 17;
        }
        else if (condition < 1) {
            return 1;
        }
        return condition;
    }

    public int compareTo( Patient other ) {
        if( this.condition < that.condition ) {
            return -1;
        }
        else if( this.condition > that.condition ) {
            return +1;
        }
        else {
            return this.name.compareTo(that.name);
        }    
    }
}

我现在需要创建一个名为 ER() 的类...我必须实现的许多方法之一具有以下条件:

public void addPatient(String name, int severity, Date time)
// Purpose: adds a person to the waiting list in the emergency 
//          room.  
// Preconditions: name is not null
//                severity is an integer in the range [1,17]
//                time is the current time
// Postconditions: the person is added to the emergency room
//                 waiting list.  The "priority" in the list is 
//                 based on severity (1 being least important and
//                 17 being most important) first and for patients
//                 with equal severity, based on time (FIFO).

我的问题是,我将在哪里创建每个患者(指定姓名和病情严重程度)并且有人可以帮助我(请解释因为我想学习,我不要求直接代码或答案)优先方面以及如何按到达时间优先考虑相同严重程度的患者?

提前感谢大家的帮助或输入!

【问题讨论】:

  • patient 类是作为模板提供给您的还是您编写的?
  • en.wikipedia.org/wiki/Priority_queue 将为您提供有关优先级队列是什么、作用以及如何实现它的想法。您应该在 main() 方法中创建患者并将其添加到您的 ER。
  • @TheocharisK。我对其进行了编码......所以设计很可能不合适或效率低下。我希望我设计正确?!?!?!随时让我知道我是怎么做的! PS:我的 compareTo 方法中的“那个”确实有一个错误,尽管我稍后会解决这个错误!
  • 您在that 关键字上出现错误,因为Java 中根本没有这样的关键字。您应该将其替换为other。还有一个很好的提示是将Date 变量添加到您的Patient 类中,您可以在其中保存每位患者的到达时间,以便以后进行比较。
  • 感谢您的帮助@TheocharisK。

标签: java oop queue priority-queue


【解决方案1】:

首先创建特定的控制器,例如FrontDeskController,然后在此类中创建方法,例如register/checkIncheckOut。您将在此处插入/删除所有患者数据,并在单个 Collection 中收集您认为适合您的情况的所有数据。

要对队列进行优先排序,如果可以将要处理的Collection 分开,则必须使用简单的排序算法进行排序,例如快速排序,并将排序数据传递给另一个集合,例如QueueStack。我认为这种方法很适合在ER 类中使用。

【讨论】:

  • 所以基本上你是说创建一个单独的类来处理到达时间和日期类信息?
  • 日期类信息是什么意思?当患者到达时,获取她/他的到达时间,将其保存在患者属性中,假设患者类有 Date timeArrival。然后在 ER 类中,您必须按日期或条件值对所有患者数据进行排序
  • 这个类会扩展 Patient 吗?
  • ER 不应扩展 PatientPatient 应视为 POJO 模型,ER 应视为 Controller。在Patient类中添加private java.util.Date timeArrival,应该够了,在register时赋值
猜你喜欢
  • 2013-08-21
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多