- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
以下程序旨在重现基本的酒店客房预订方案。创建酒店号码的实例后,通过调用 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/
我在使用 Room 或 idk 时遇到问题,实际上问题出在哪里,我需要帮助找出问题出在哪里,我正在使用 Hilt DI,创建数据库实例的那一刻它崩溃了这是我的代码 错误 E/AndroidRuntim
我有一个关于数据结构和类设计的问题(抱歉太长了)。为了简单起见,假设这是一个游戏,我想在房间之间导航(想象一系列 2D 非滚动屏幕,例如早期的银河战士/恶魔城)。每个房间可以有许多导出(例如上、下、左
我使用当前日期(1-25)作为父ID并使用房间(08-00_11-00_karpet1-)作为 child ID。该数据库中包含在该日期(父 ID)订购该房间(子 id)的用户信息。 问题1 使用此布
在我的 Android 项目中,我使用 Room 库来处理 SQLite 数据库。我使用我的数据库来保存国家电话代码。我的数据库预装了两个国家(观看 populateDatabaseWithCount
我正在尝试将 Room 持久性库添加到 Android 应用程序项目中。在 build.gradle 文件中,我添加了以下依赖项: implementation 'android.arch.persi
人们可以提前从 25 场讲座中选择最多 5 场。所有这些讲座都在五个房间的五个时间段内在一天内进行。听众可以参加的每个(首选)讲座都让她更快乐,他选择但不能参加的每个讲座(因为另一个首选讲座在同一时间
我在 Android 上使用 OrmLite 而不是 SQLite 和 SQLCipher 来加密数据库。有没有办法加密 Room 数据库? 最佳答案 默认情况下,Room 将数据存储在应用程序的内部
使用 Room ORM,我使用 @Entity 注释声明了一个实体 EQPreset。该实体包含一个数组 int[]。它给出以下错误: 错误:无法确定如何将此字段 (int[] arr) 保存到数据库
我正在尝试构建一个管理 child 托儿所的应用程序,特别是管理哪个 child 在哪个时间点在托儿所的哪个房间里。 Nursery 链式店有多个分支机构。每个分店有几个房间,每个房间对应一个年龄段,
我在生产环境中遇到了“android.database.sqlite.SQLiteDatabaseLockedException”异常。错误分析时出现异常。我的项目数据库有空间。项目中没有使用多进程。
我想实现 Android Room 持久性。 这是我的 DAO 界面。 @Dao interface FoodDao { /** * Returns all data in tabl
我正在尝试使用 Room 数据库和 LiveData。我有 ViewModels,它保存从 dao 获得的 LiveData。如果我更新Transaction ,然后LiveData>观察正常,但是
在 Firebase ,创建“房间”(例如用于聊天)很容易,正如其各种示例中所记录的那样。 对于聊天的数据结构,我会使用这样的东西: rooms room1 member_co
我试图从 Activity 中将一行插入 SQLITE 数据库,然后返回要存储在 Activity 中的变量中的 rowId。请参阅下面我使用的方法和逻辑。 private void insert
我正在使用 XMPPFramework 开发聊天应用程序 加入现有房间后如何接收消息历史记录? 现在我像这样加入房间: XMPPJID *roomJid = [XMPPJID jidWithStrin
我在我的应用程序中使用 Room 并将数据插入到我的数据库中时 ConcurrentModificationException有时会被抛出。为什么会这样? 我使用分页 api,在每次 api 调用后,
我想为 pb 添加值,由于将 pb_value 包含到实体中,应用程序崩溃了。我是学习室的新手,我不确定将额外项目合并到数据库中的正确方法。 E/AndroidRuntime: FATAL EXCEP
我想在当前 pb 中添加值、日期和详细信息。我在 pbInfo 的数据库中收到错误“冲突声明”。我应该如何修复此错误? @Entity(tableName = "pb_table") data cla
我正在尝试制作一个聊天应用程序,用户可以在其中聊天。我想将两个用户 uid 字符串插入函数并返回一个连接的字符串。但我希望以某种方式组织 uid,以便返回的值始终相同。 func (id1, id2)
我不断收到以下错误: Cannot figure out how to save this field into database. You can consider adding a type co
我是一名优秀的程序员,十分优秀!