我试图在嵌套的foreach
循环中使用if语句来检查特定条件,但是有一个点,其中列表中的一个对象与第一个条件不匹配。我遇到的问题是如果我把第一个条件放入其他条件,它会使列表中的所有对象都变得虚假,即使每个对象都有一个匹配,除了最后一个。
public void ReserveRoom(List<ReservationType> reservation)
{
List<ReservationType> RequestReservation = new List<ReservationType>();
RequestReservation = reservation; //List that contains the test cases.
string reservationid = "0001";
int number = int.Parse(reservationid);
int count = 0;
foreach (ReservationType requestReservation in RequestReservation)
{
List<String> DateList = CreateDateList(requestReservation.StartDate, requestReservation.numDays);
foreach (Inventory inventory in RoomInventory)
{
if (requestReservation.hotelId == inventory.HotelId)
{
if (requestReservation.roomType == inventory.RoomType)
{
for (int i = 0; i < DateList.Count; i++)
{
if (DateList[i] == inventory.Date && inventory.Quantity > 0)
{
count++;
//Console.Write(requestReservation.customerId + ": " + inventory.Date + ": " + count + Environment.NewLine);
}
}
if (requestReservation.numDays == count)
{
requestReservation.result = ReservationType.ReservationResultType.Success;
count = 0;
}
foreach (String dt in DateList)
{
if (dt.Equals( inventory.Date))
inventory.Quantity--;
}
foreach (Hotels h in LHotels)
{
foreach (Room rm in h.RoomList)
{
if (requestReservation.hotelId == h.HotelId && requestReservation.roomType == rm.RoomType)
requestReservation.cost = (requestReservation.numDays * rm.Rate);
}
}
}
}
else
{
requestReservation.result = ReservationType.ReservationResultType.UnknownHotelId;
}
}
number++;
}
}
我试图实现的输出:
<ReservationType>
<hotelId>2345</hotelId>
<StartDate>20160905</StartDate>
<numDays>1</numDays>
<customerId>00001</customerId>
<roomType>KB</roomType>
<reservationId>0002</reservationId>
<cost>325</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>2345</hotelId>
<StartDate>20160905</StartDate>
<numDays>4</numDays>
<customerId>00002</customerId>
<roomType>KB</roomType>
<reservationId>0003</reservationId>
<cost>1300</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>2345</hotelId>
<StartDate>20160905</StartDate>
<numDays>5</numDays>
<customerId>00003</customerId>
<roomType>KB</roomType>
<reservationId>0004</reservationId>
<cost>1625</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>2345</hotelId>
<StartDate>20160907</StartDate>
<numDays>3</numDays>
<customerId>00004</customerId>
<roomType>KB</roomType>
<reservationId>0005</reservationId>
<cost>975</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>2345</hotelId>
<StartDate>20160909</StartDate>
<numDays>4</numDays>
<customerId>00005</customerId>
<roomType>KB</roomType>
<reservationId>0006</reservationId>
<cost>1300</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>2345</hotelId>
<StartDate>20160906</StartDate>
<numDays>5</numDays>
<customerId>00006</customerId>
<roomType>KB</roomType>
<cost>0</cost>
<result>RoomNotAvailable</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160905</StartDate>
<numDays>1</numDays>
<customerId>00007</customerId>
<roomType>QB</roomType>
<reservationId>00006</reservationId>
<cost>400</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160905</StartDate>
<numDays>1</numDays>
<customerId>00008</customerId>
<roomType>QB</roomType>
<cost>0</cost>
<result>RoomNotAvailable</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160905</StartDate>
<numDays>4</numDays>
<customerId>00009</customerId>
<roomType>QB</roomType>
<reservationId>00007</reservationId>
<cost>400</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20170905</StartDate>
<numDays>1</numDays>
<customerId>00010</customerId>
<roomType>QB</roomType>
<cost>0</cost>
<result>RoomNotAvailable</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160915</StartDate>
<numDays>5</numDays>
<customerId>00011</customerId>
<roomType>QB</roomType>
<reservationId>00007</reservationId>
<cost>500</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160925</StartDate>
<numDays>10</numDays>
<customerId>00012</customerId>
<roomType>QB</roomType>
<cost>0</cost>
<result>RoomNotAvailable</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160907</StartDate>
<numDays>3</numDays>
<customerId>00013</customerId>
<roomType>QB</roomType>
<reservationId>00008</reservationId>
<cost>300</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160909</StartDate>
<numDays>3</numDays>
<customerId>00014</customerId>
<roomType>KB</roomType>
<reservationId>00009</reservationId>
<cost>600</cost>
<result>Success</result>
</ReservationType>
<ReservationType>
<hotelId>7890</hotelId>
<StartDate>20160905</StartDate>
<numDays>1</numDays>
<customerId>00015</customerId>
<roomType>AB</roomType>
<cost>0</cost>
<result>UnknownRoomType</result>
</ReservationType>
<ReservationType>
<hotelId>8888</hotelId>
<StartDate>20160905</StartDate>
<numDays>1</numDays>
<customerId>00016</customerId>
<roomType>DB</roomType>
<cost>0</cost>
<result>UnknownHotelId</result>
</ReservationType>
我得到的结果:这是针对每个对象的。 unknownhotelid
<ReservationType>
<hotelId>2345</hotelId>
<StartDate>20160907</StartDate>
<numDays>3</numDays>
<customerId>00004</customerId>
<roomType>KB</roomType>
<reservationId>0005</reservationId>
<cost>975</cost>
<result>UnknownHotelId</result>