0

我试图自学 Java,并开始浏览 BlueJ 示例。

在手术系统的情况下,我需要将患者及其地址添加到数组中,然后能够添加患者,然后列出患者。

该示例使用向量和硬代码向向量添加名称,但我想使用数组并允许用户向数组添加名称和地址。

谁能提供一些指导?

我有以下但不知道从这里去哪里。

public class Patient
{
    public String name;
    public String address;

   public Patient(String n, String a) {
      name = n;
      address = a;
   }


}
4

2 回答 2

5

此类信息在此处有很好的记录。

在您的情况下,您似乎想要创建一个类型的数组Patient

创建一个可以容纳 10 名患者的数组的语法是:

Patient[] patients = new Patients[10];

阅读文档后,您将能够使用我提供的语法来获得所需的功能:)

于 2013-03-05T13:40:24.930 回答
1

我不会给你确切的答案,因为这看起来很像家庭作业,但这里有一个使用数组的酒店的示例实现。仔细阅读并尝试理解它;如果您这样做,您将能够自己解决患者问题。

import java.util.*;

class Customer
{
    private String name;
    private int room;

    public void setName(String name)
    {
        this.name=name;
    }

    public String getName()
    {
        return this.name;
    }

    public void setRoom(int room)
    {
        this.room=room;
    }

    public int getRoom()
    {
        return this.room;
    }
}

class Hotel
{
    public static void initialize(Customer RoomList[])
    {
        for(int i=0; i<RoomList.length; i++)
        {
            RoomList[i]=new Customer();
            RoomList[i].setName("EMPTY");
            RoomList[i].setRoom(i+1);
        }
    }

    public static void viewList(Customer RoomList[])
    {
        for(int i=0; i<RoomList.length; i++)
        {
            if(RoomList[i].getName()=="EMPTY")
                System.out.println("Room number "+RoomList[i].getRoom()+" is vacant.");
            else
                System.out.println("Room number "+RoomList[i].getRoom()+" is ocupied by "+RoomList[i].getName()+".");
        }
        System.out.println();
    }

    public static boolean addCustomer(Customer RoomList[], String name)
    {
        for(int i=0; i<RoomList.length; i++)
            if(RoomList[i].getName().equals("EMPTY"))
            {
                RoomList[i].setName(name);
                return true;
            }
        return false;
    }

    public static void showEmptyRooms(Customer RoomList[])
    {
        System.out.println("Available rooms are:");
        for(int i=0; i<RoomList.length; i++)
            if(RoomList[i].getName()=="EMPTY")
                System.out.println(RoomList[i].getRoom());
        System.out.println();
    }

    public static boolean deleteCustomer(Customer RoomList[], String name)
    {
        for(int i=0; i<RoomList.length; i++)
            if(RoomList[i].getName().equals(name))
            {
                RoomList[i].setName("EMPTY");
                System.out.println("Deletion successful.\n");
                return true;
            }
        return false;
    }

    public static int getIndex(Customer RoomList[], String name)
    {
        for(int i=0; i<RoomList.length; i++)
            if(RoomList[i].getName().equals(name))
                return i;
        return -1;
    }

    public static void main(String[] args)
    {
        int numOfCustomers=10;
        Customer[] RoomList = new Customer[numOfCustomers];
        String name;
        initialize(RoomList);
        Scanner input = new Scanner(System.in);
        int option=0;

        do
        {
            System.out.println("        Hotel Booking Options");
            System.out.println("=====================================");
            System.out.println("1: To View all rooms");
            System.out.println("2: To Add customer to a room");
            System.out.println("3: To Display empty rooms");
            System.out.println("4: To Delete customer from a room");
            System.out.println("5: Find room from customer name");
            System.out.println("0: Exit");

            System.out.print("\nEnter your choice: ");
            option = input.nextInt();
            System.out.println();

            switch(option)
            {
                case 1:
                {
                    viewList(RoomList);
                    break;
                }
                case 2:
                {
                    System.out.print("Customer's name: ");
                    name=input.next();
                    System.out.println();
                    if(!addCustomer(RoomList, name))
                        System.out.println("No rooms available!");
                    break;
                }
                case 3:
                {
                    showEmptyRooms(RoomList);
                    break;
                }
                case 4:
                {
                    System.out.print("Customer's name: ");
                    name=input.next();
                    System.out.println();
                    deleteCustomer(RoomList, name);
                    break;
                }
                case 5:
                {
                    System.out.print("Customer's name: ");
                    name=input.next();
                    System.out.println();
                    System.out.println("Customer's room: "+RoomList[getIndex(RoomList, name)].getRoom()+"\n");
                    break;
                }
                case 0:
                {
                    System.out.println("\nThank you!\n");
                    break;
                }
                default:
                {
                    System.out.println("Invalid option!\n");
                    break;
                }
            }


        }while(option!=0);
    }
}
于 2013-03-05T13:53:43.607 回答