首页 > 解决方案 > 如何调用数组患者姓名以按名称库进行比较?

问题描述

找不到 getName();

您必须在医院的急诊病房实施候诊室管理系统。您的程序将按照先到先得的原则为患者分配一个 ID 号。id 越低,越早为患者提供服务。

ShowAllPatient():此方法按 SORTED 顺序显示等待患者的所有 id。(提示:使用课堂上学到的排序方法,为每个任务使用适当的数据结构)[按名称排序]

**/ import java.util.Scanner;
    //import static java.lang.System.*;
    public class Tester {
        public static void main(String [] args){
            String input;
            WatingRoomManagment b= new WatingRoomManagment();
            System.out.println("1 Array Based \n2 Linkedlist Based");
            String type=new Scanner(System.in).next();
            if(type.equals("1")){
                b.arrayQueue();
            }
            else if(type.equals("2")){
                b.ListQueue();
            }
                do
            {
                System.out.println("Hospital Wating Room Managment Service");  
                System.out.println("1. Add a new Patient");
                System.out.println("2. Serve a patient");
                System.out.println("3. Can Doctor go Home?");
                System.out.println("4. Cancel All appointment");
                System.out.println("5. Display all Patient Information");
                System.out.println("6. Quit");

                System.out.println("Please enter either 1 to 6: "); 
                input =(new Scanner(System.in).next());

                if (input.equals("1")){
                    System.out.println("Name: ");
                    String nam=new Scanner(System.in).next();
                    System.out.println("Age: ");
                    String age=new Scanner(System.in).next();
                    System.out.println("blood: ");
                    String blood=new Scanner(System.in).next();
                    paitent a = new paitent(nam,age,blood);
                    b.RegisterPatient(a);
                }
                else if (input.equals("2")){
                    b.serveapatient();
                }
                else if(input.equals("3")){
                    b.CanDoctorgoHome();
                }
                else if(input.equals("4")){
                    b.cancelAll();
                }
                else if (input.equals("5")){
                    b.showAllpatient();
                }
            }while(!input.equals("6"));
        }
    }
    class paitent {
        String name;
        int id;
        String age;
        String blood;
        paitent(String name,String age,String blood){
            this.name=name;
            this.age=age;
            this.blood=blood;
            this.id=idGen();
        }
        public String getName(){
            return name;
        }
        public String toString(){
            String s= id+" "+name+" age "+age+" Blood "+blood;
            return s;
        }


        public int idGen() {

            int id = this.name.hashCode() + this.blood.hashCode();
            int length = String.valueOf(id).length();
            int Max_Length = 5;
            if(String.valueOf(id).length()>Max_Length) 
            {
                id = (int) (id /Math.pow(10.0,length - Max_Length ));
            }
            return  id;
        }
    }
    class WatingRoomManagment {
        static Queue q1;
        void arrayQueue(){
            q1= new ArrayQueue();
        }
        void ListQueue(){
            q1= new ListQueue();
        }
        void RegisterPatient(paitent k){
            try{
                q1.enqueue(k);
            }
            catch(QueueOverflowException e){
                System.err.println("Queue Full! Cannot Enqueue\n"+e);
            }
        }
        void serveapatient(){

            try{
                q1.dequeue();
            }
            catch(QueueUnderflowException e){
                System.err.println("Queue Empty\n"+e);
            }
        }
        void showAllpatient(){
            Object [] names=q1.toArray();
            Object temp;
            for (int i = 0; i < names.length; i++){
                for (int j = i + 1; j < names.length-1; j++){
                    if(names[i].getName().compareTo(names[j].getName())>0){
                        temp = names[i];
                        names[i] = names[j];
                        names[j] = temp;
                   }
                }
            }
            System.out.println(q1.toString());
        }
        void CanDoctorgoHome(){
            if(q1.isEmpty())
                System.out.println("Yes. He can go now");
            else
                System.out.println("No.");
        }
        void cancelAll(){
            q1.cancel();
        }
    }
    class ArrayQueue implements Queue {
        int size=0;
        int rear=-1;
        int front=-1;

        Object[] a = new Object[10];

        public void enqueue(paitent k)throws QueueOverflowException{
            if(size==a.length){
                throw new QueueOverflowException();
            }
            if(size==0){
                a[0]=k;
                a[(rear+1)%a.length]=k;
                rear=(rear+1)%a.length;
                front=(front+1)%a.length;
                size++;
            }
            else{      
                a[(rear+1)%a.length]=k;
                rear=(rear+1)%a.length;
                size++;
            }
        }
        public void dequeue() throws QueueUnderflowException{
            if(size==0)
                throw new QueueUnderflowException();
            a[front%a.length]=null;
            front=(front+1)%a.length;
            size--;
        }
        public boolean isEmpty(){
            if(size==0)
                return true;
            else
                return false;
        }
        public Object[] toArray(){
            Object[] temp = new Object[size];
            int start=front;
            for(int i=0; i<temp.length ; i++){
                temp[i]=a[start];
                start=(start+1)%a.length;
            }
            return temp;
        }
        public String toString(){
            String p="";
            if(size==0){
                p=p+"No is here";
            }
            else{
                int st=front;
                for(int i=0 ; i<size ; i++){
                    p=p+" "+a[st]+"\n";
                    st=(st+1)%a.length;
                } 
            }
            return p;
        }
        public void cancel(){
            int st=front;
            for(int i=0 ; i<size ; i++){
                a[st]=null;
                st=(st+1)%a.length;
            }
            size=0;
        }
        public int size(){
            return size;
        }
    }
    class ListQueue implements Queue{
        int size;
        Node front;
        Node rear;


        public ListQueue(){
            size = 0;
            front = null;
            rear = null;
        }
        public void enqueue(paitent o) throws QueueOverflowException{
            Node temp=new Node(o,null);
            if(size==0){
                front=temp;
                rear=front;
                size++;
            }
            else if(size>0){
                rear.next=temp;
                rear=temp;
                size++;
            }
        }
         public void dequeue() throws QueueUnderflowException {
            if(size==0){
                throw new QueueUnderflowException();
            }
            Node mn = front;
            if(size==1){
                rear=null;
                front=null;
                size--;
            }
            if(size>0){
                front = front.next;
                size--;
            }
            mn.val= null;
            mn.next= null;
        }
         public boolean isEmpty(){
            if(size==0)
                return true;
            else
                return false;
        }
         public String toString(){
            if(size==0)
                return "Empty Queue";
            else{
                String s = "[ ";
                for(Node n = front; n!=null; n=n.next){
                    s = s + n.val+ " ";
                }
                return s + "]";
            }
        }
         public Object[] toArray() {
            Object a [] = new Object [size];
            int i =0;
            for(Node n = front; n!=null; n=n.next){
                a[i] = n.val;i++;
            }
            return a; 
        }
         public void cancel(){
             front=null;
             rear=null;
             size=0;
         }
         public int size(){
             return size;
         }
    }
    class Node{
        paitent val;
        Node next;

        public Node(paitent v, Node n){
            val = v;
            next = n;
        }
    }
    interface Queue { 
        public int size(); 
        public boolean isEmpty(); 
        public void enqueue(paitent k) throws QueueOverflowException; 
        public void dequeue() throws QueueUnderflowException;
        public String toString();
        public Object[] toArray();
        public void cancel();
    }
    class QueueOverflowException extends Exception{

    }
    class QueueUnderflowException extends Exception{
    }**/

标签: javaqueue

解决方案


尝试用此行替换调用“getName”的行

if(((patient)names[i]).getName().compareTo(((patient)names[j]).getName())>0)

因为您的数组“名称”被定义为对象数组而不是特定类型的数组,所以在编译器能够查看哪些方法可用之前,您可能需要将其元素转换为“患者”。


推荐阅读