Java 使用阵列的酒店
好的,基本上我正在创建一个酒店,它可以执行以下选项Java 使用阵列的酒店,java,arrays,Java,Arrays,好的,基本上我正在创建一个酒店,它可以执行以下选项 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:
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");
- 现在,我可以查看、添加客户、显示空房间和删除”
- 然而,我刚刚意识到我不能添加多个客户。代码只会为每个选择的房间分配一个同名的客户
包测试室;
导入java.util.*;
公共课测试室{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
字符串Customer=null;
int roomNum=1;
字符串选项;
字符串[]酒店=新字符串[12];
初始化(酒店);
while(roomNum<11)
{
System.out.println(“酒店预订选项”);
System.out.println(“V:查看所有房间”);
System.out.println(“A:将客户添加到房间中”);
System.out.println(“E:显示空房间”);
System.out.println(“D:从房间中删除客户”);
System.out.println(“F:从客户名称中查找房间”);
System.out.println(“O:按名称字母顺序查看房间”);
Option=input.next();
if(Option.equals(“V”){//查看所有房间
视图(酒店、客户);
}
if(Option.equals(“A”){//将客户添加到房间
System.out.println(“输入房间号(1-10)”);
roomNum=input.nextInt();
System.out.println(“输入房间名称”+roomNum+”:”;
Customer=input.next();
酒店[roomNum]=客户;
添加(酒店、客户);
System.out.println(“”);
}
if(Option.equals(“E”){//查看所有空房间
空闲(酒店、客户);//指向私人静态void的链接为空
}
如果(Option.equals(“D”){//从房间中删除客户
//搜索房间是否已被占用,如果已被占用,则将从该房间中删除客户
视图(酒店、客户);
System.out.println(“输入要从中删除客户的房间:”;
roomNum=input.nextInt();
酒店[roomNum]=“空”;
删除(酒店、客户);
System.out.println(“”);
}
如果(Option.equals(“F”){//查看所有空房间
查找(酒店);//指向私人静态void的链接为空
}
}
}
私有静态void初始化(字符串hotelRef[])
{
对于(int x=1;x<11;x++)
hotelRef[x]=“空”;
System.out.println(“欢迎来到广场”);
}
公共静态无效视图(字符串酒店[],字符串客户){
对于(int x=1;x问题的原因是您没有为每个新条目实例化类。很抱歉,这个问题有太多根本性的错误。您需要做的是定义包含其数据成员和数据访问函数的Customer类,然后根据需要创建新对象,p完全从另一个类中获得/利用它们
您可以尝试以下方法:
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);
}
}
import java.util.*;
类客户
{
私有字符串名称;
私人室内;
公共void集合名(字符串名)
{
this.name=name;
}
公共字符串getName()
{
返回此.name;
}
公共空间设置室(室内)
{
这个房间=房间;
}
公共会议室
{
把这个房间还给我;
}
}
一流酒店
{
公共静态无效初始化(客户房间列表[])
{
对于(int i=0;i您的代码似乎有点不正确。请查看添加客户代码。无论您在何处添加客户,它都会添加到索引0.:)
if(Option.equals(“A”){//将客户添加到房间
System.out.println(“输入房间号(1-10)”);
roomNum=input.nextInt();
System.out.println(“输入房间名称”+roomNum+”:”;
Customer=input.next();
hotel[roomNum]=Customer;//添加到房间号。
add(hotel,Customer);//添加到第0个索引。
System.out.println(“”);
}
私人静态无效添加(字符串酒店[],字符串客户){
对于(int x=1;x你的问题是什么?我刚刚编辑了我的帖子。你能在发帖时更好地格式化你的代码吗?@ShaziaE:你的代码缩进非常严重,所以我花了一段时间才编辑它。在我编辑的时候,你编辑了这个问题,所以它被覆盖了。我已经重新添加了它。我还建议你通读Java代码C命名惯例。(提示:用大写字母开始变量是一个非常糟糕的主意)。此外,您可能需要研究一种面向对象的方法,因为Java毕竟是一种面向对象的语言。请这样想:与其保存上次输入的客户的名称,不如保存另一个customer
对象(你还没有做这个,你应该)查看Roney的答案,在一个简单的Customer
对象上找到一个例子。我明白了。我是Java新手,所以还有很多东西要学。但是谢谢you@ShaziaE:试着理解我刚才添加的新代码。试着按照同样的思路编写一些东西。我希望这会有所帮助。@ShaziaE一篇面向对象的评论在此过程中,ed编程将非常有用。
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);
}
}
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]);
}
}
}