java 酒店使用数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15178654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:43:37  来源:igfitidea点击:

Hotel using array

javaarrays

提问by Shazia E

Okay basically I am creating a hotel which acts the following options

好的,基本上我正在创建一个酒店,它具有以下选项

System.out.println("V: To View all rooms");
System.out.println("A: To Add customer to a room");
System.out.println("E: To Display empty rooms");
System.out.println("D: To Delete customer from a room");
System.out.println("F: Find room from customer name");
System.out.println("O: View rooms alphabetically by name");
  • NOW, I am able to do the VIEW, ADD CUSTOMER, DISPLAY EMPTY ROOMS AND DELETE".
  • HOWEVER, I just realised I cannot add multiple customers. The code will simply assign a customer with the same name to each room chosen.
  • 现在,我可以查看、添加客户、显示空房间和删除”。
  • 但是,我刚刚意识到我不能添加多个客户。该代码将简单地为每个选择的房间分配一个具有相同名称的客户。

Example: If I add the name TOM to room 2, and then later add DENNIS to room 3. Room 2 and Room 3 which show that it is occupied by Dennis. - I know why, because the String customer can only store one information.

示例:如果我将名称 TOM 添加到房间 2,然后将名称添加到房间 3。房间 2 和房间 3 表明它被丹尼斯占用。- 我知道为什么,因为 String 客户只能存储一个信息。

I have a feeling I need to change Customer to an Array.

我有一种感觉,我需要将 Customer 更改为一个数组。

(please bare in mind, I don't want to use an ArrayList).

(请记住,我不想使用 ArrayList)。

SO: my question is, how do I go about solving this? Because I tried to work with Customer as an array and I ruined the entire coding (thankgod for local history revert!)

SO:我的问题是,我该如何解决这个问题?因为我试图将 Customer 作为一个数组使用,但我破坏了整个编码(感谢本地历史恢复!)

Code:

代码:

package testroom;

import java.util.*;

public class TestRoom{

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String Customer = null;
        int roomNum = 1;
        String Option;

        String[] hotel = new String[12];

        initialise(hotel); 

        while ( roomNum  < 11 )
        {

            System.out.println("Hotel Booking Options");
            System.out.println("V: To View all rooms");
            System.out.println("A: To Add customer to a room");
            System.out.println("E: To Display empty rooms");
            System.out.println("D: To Delete customer from a room");
            System.out.println("F: Find room from customer name");
            System.out.println("O: View rooms alphabetically by name");

            Option = input.next();

            if (Option.equals("V")){ //viewing all rooms
                view(hotel, Customer);  
            }

            if (Option.equals("A")){ // adding a customer to a room

                System.out.println("Enter room number(1-10)");
                roomNum =input.nextInt();
                System.out.println("Enter name for room " + roomNum + " : " ) ;
                Customer = input.next();
                hotel[roomNum] = Customer ;
                add(hotel, Customer);
                System.out.println(" ");

            }

            if (Option.equals("E")){ //view all empty rooms
                vacant(hotel, Customer); //links to private static void empty
            }

            if (Option.equals("D")){ //Deletes a customer from a room

                //Searches if room is occupied, if it is then it will delete customer from that room
                view(hotel, Customer);
                System.out.println("Enter the room which you want to delete a customer from: ");
                roomNum = input.nextInt();
                hotel[roomNum] = "empty";

                delete(hotel, Customer);
                System.out.println("");

            }

            if (Option.equals("F")){ //view all empty rooms
                find(hotel); //links to private static void empty
            }

        }
    }

    private static void initialise( String hotelRef[] )
    {
        for (int x = 1; x < 11; x++ )
            hotelRef[x] = "empty";
        System.out.println( "Welcome to The Plaza");
    }


    public static void view(String hotel[], String Customer){

        for (int x =1; x <11; x++)
        {
            int z=0;
            String Customername = Customer;
            hotel[z]= Customername;

            if (hotel[x].equals("empty"))
                System.out.println("room " + x + " is empty");
            else {
                System.out.println("room " + x + " is occupied by "+ hotel[z]);
            }
        }
    }

    private static void add(String hotel[], String Customer){
        for (int x =1; x <11; x++)
        {
            int z=0;
            String Customername = Customer;
            hotel[z]= Customername;
            if (hotel[x].equals("empty"))
                System.out.println("room " + x + " is empty");
            else {
                System.out.println("room " + x + " is occupied by "+ hotel[z]);
            }

        }
    }

    private static void vacant(String hotel[], String Customer){
        for (int x = 1; x < 11; x++ )
        {
            int z=0;
            String Customername = Customer;
            hotel[z]= Customername;
            if (hotel[x].equals("empty")) //if statement
                System.out.println("room " + x + " is empty"); 
        }

    }


    private static void delete(String hotel[], String Customer){ //link to this when pressed the D key
        //view (hotel);
        for (int x = 1; x < 11; x++ )
        {
            int z=0;
            String Customername = Customer;
            hotel[z]= Customername;
            if (hotel[x].equals("empty"))
            { 
                System.out.println("room " + x + " is empty");
            }
            else {
                System.out.println("room " + x + " occupied by " + hotel[x]);
            }
        }
    }

    private static void find(String hotel[]){

        Scanner input = new Scanner(System.in);   
        System.out.println("Enter customer name for room:" );
        String customersname;
        customersname = input.next();  //stores name they enter as customers name
        for (int x = 0; x < 10; x++ )
        {
            if (hotel[x].equals(customersname))
                System.out.println("room " + x + " is occupied                       by "+hotel[x]);

        }
    }
}

回答by Roney Michael

The reason for your problem is that you are not instantiating the class for each new entry. I'm sorry, but there is just too much fundamentally wrong with this question. What you need to do would be to just define the Customer class with its data members and data access functions in it and then create new objects as necessary and populate/utilize them from another class entirely.

您的问题的原因是您没有为每个新条目实例化该类。对不起,这个问题有太多根本性的错误。您需要做的只是定义 Customer 类及其数据成员和数据访问函数,然后根据需要创建新对象并完全从另一个类填充/利用它们。

You could try something like this:

你可以尝试这样的事情:

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)
    {
        Customer[] RoomList = new Customer[12];
        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);
    }
}

回答by dharam

Your code seems to be slightly incorrect. Look at the add customer code. No matter where you add the customer, it also adds to index zero. :)

您的代码似乎有点不正确。查看添加客户代码。无论您在哪里添加客户,它也会添加到索引零。:)

 if (Option.equals("A")){ // adding a customer to a room
    System.out.println("Enter room number(1-10)");
    roomNum =input.nextInt();
    System.out.println("Enter name for room " + roomNum + " : " ) ;
    Customer = input.next();
    hotel[roomNum] = Customer ; // adds to the room number.
    add(hotel, Customer); // adds to the zero-th index.
    System.out.println(" ");
 }


private static void add(String hotel[], String Customer){
    for (int x =1; x <11; x++)
    {
        int z=0;
        String Customername = Customer;
        hotel[z]= Customername; // this line is the culprit.
        if (hotel[x].equals("empty"))
            System.out.println("room " + x + " is empty");
        else {
            System.out.println("room " + x + " is occupied by "+ hotel[z]);
        }

    }
}