gpt4 book ai didi

c# - C#房间管理程序

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:19 25 4
gpt4 key购买 nike

以下程序旨在重现基本的酒店客房预订方案。创建酒店号码的实例后,通过调用 checkIn() 方法,程序检查是否有可用房间,如果有,则保留房间。

但是,如果在分配完所有房间后,有人从一个房间退房(通过 checkOut() 方法),例如1 号房间,然后尝试登记入住该房间。当 checkIn() 调用 hasRoomsAvailable() 方法时会出现此问题,在这种情况下返回 false 而不是 true,因为 currentRoomNumber 是 5。

在不改变每种方法的时间复杂度的情况下,有人可以建议如何解决这个问题以及理想情况下如何改进设计吗?

using System;

namespace HotelManagement
{
//Hotel class
public class Hotel
{
private bool[] available;
private int totalNumberOfRooms;
private int currentRoomNumber;

// constructor to set number of rooms
private Hotel(int totalNumberOfRooms)
{
this.totalNumberOfRooms = totalNumberOfRooms;
available = new bool[totalNumberOfRooms];

for (int i = 0; i < totalNumberOfRooms; i++)
available[i] = true;
}

//Returns true if room is available
private bool hasRoomsAvailable()
{
if (currentRoomNumber < totalNumberOfRooms &&
available[currentRoomNumber])
return available[currentRoomNumber];
else
return false;
}

//Time Complexity: O(1)

//Checks if there's at least one room available and it reserves it
private int checkIn()
{
if (hasRoomsAvailable())
{
available[currentRoomNumber] = false;
return ++currentRoomNumber;
}
else
return -1;
}
//Time Complexity: O(1)

//Check out method
private void checkOut(int roomNumber)
{
if (roomNumber <= totalNumberOfRooms && roomNumber != -1)
{
if (available[roomNumber - 1] == false)
{
available[roomNumber - 1] = true;
Console.WriteLine("Check out room : {0}", roomNumber);
}
else
Console.WriteLine("Invalid Check Out : {0}", roomNumber);
}
else
Console.WriteLine("Incorrect room number : {0}", roomNumber);
}
//Time Complexity: O(1)

public static void Main(string[] args)
{
//Create an instance of Hotel with 5 rooms
Hotel hotel = new Hotel(5);
int roomNum = -1;

if (hotel.hasRoomsAvailable())
{
roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);
}

hotel.checkOut(1);
roomNum = hotel.checkIn();

Console.WriteLine("Room Allocated is: {0}", roomNum);

//pause program output on console
Console.ReadLine();
}
}
}

最佳答案

bool[] available 足以找到可以入住的房间。维护 currentRoomNumber 会带来比其值(value)更多的困难。

public class Hotel
{
private bool[] available;
private int totalNumberOfRooms;

// constructor to set number of rooms
public Hotel(int totalNumberOfRooms)
{
this.totalNumberOfRooms = totalNumberOfRooms;
available = new bool[totalNumberOfRooms];

for (int i = 0; i < totalNumberOfRooms; i++)
available[i] = true;
}

//Returns true if room is available
public bool hasRoomsAvailable()
{
return available.Any(room => room);
}

//Time Complexity: O(1)

//Checks if there's at least one room available and it reserves it
public int checkIn()
{
for(int room = 0; room < totalNumberOfRooms; room++)
{
if (available[room])
{
available[room] = false;
return room + 1;
}
}

return -1;
}
//Time Complexity: O(1)

//Check out method
public void checkOut(int roomNumber)
{
if (roomNumber <= totalNumberOfRooms && roomNumber > 0)
{
if (available[roomNumber - 1] == false)
{
available[roomNumber - 1] = true;
Console.WriteLine("Check out room : {0}", roomNumber);
}
else
Console.WriteLine("Invalid Check Out : {0}", roomNumber);
}
else
Console.WriteLine("Incorrect room number : {0}", roomNumber);
}
}
public static void Main(string[] args)
{
//Create an instance of Hotel with 5 rooms
Hotel hotel = new Hotel(5);
int roomNum = -1;

if (hotel.hasRoomsAvailable())
{
roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);
}

hotel.checkOut(1);
roomNum = hotel.checkIn();

Console.WriteLine("Room Allocated is: {0}", roomNum);

Console.ReadLine();
}


复杂度为 O(1) 的解决方案,它使用队列来存储可用房间:

public class Hotel
{
private Queue<int> rooms;
private int totalNumberOfRooms;

// constructor to set number of rooms
public Hotel(int totalNumberOfRooms)
{
this.totalNumberOfRooms = totalNumberOfRooms;
rooms = new Queue<int>();

for (int i = 1; i <= totalNumberOfRooms; i++)
rooms.Enqueue(i);
}

//Returns true if room is available
public bool hasRoomsAvailable()
{
return rooms.Count > 0;
}

//Time Complexity: O(1)

//Checks if there's at least one room available and it reserves it
public int checkIn()
{
if (rooms.Count > 0)
return rooms.Dequeue();

return -1;
}
//Time Complexity: O(1)

//Check out method
public void checkOut(int roomNumber)
{
if (roomNumber <= totalNumberOfRooms && roomNumber > 0)
{
Console.WriteLine("Check out room : {0}", roomNumber);
rooms.Enqueue(roomNumber);
}
else
Console.WriteLine("Incorrect room number : {0}", roomNumber);
}
}

public static void Main(string[] args)
{
//Create an instance of Hotel with 5 rooms
Hotel hotel = new Hotel(5);
int roomNum = -1;

if (hotel.hasRoomsAvailable())
{
roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);
}

hotel.checkOut(4);
hotel.checkOut(2);
roomNum = hotel.checkIn();
Console.WriteLine("Room Allocated is: {0}", roomNum);

roomNum = hotel.checkIn();

Console.WriteLine("Room Allocated is: {0}", roomNum);

//pause program output on console
Console.ReadLine();
}

关于c# - C#房间管理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50075283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com