locked
Decrementing Variable When Condition Is Met. RRS feed

  • Question

  • User1343990551 posted

    What im trying to do , if the condition is met, deduct 1 from that variable. but the problem, if there are three items in the list, that needs to be deduct, only the last one gets deducted, which then next iteration check, its not accurate. putting in a forloop to iterate through the list does not do it, so i dont know what else Im missing.

     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.Equals (inventory.HotelId))
                        {
                                                                         
                          if (requestReservation.roomType == inventory.RoomType)
                            {
                                
                                   for (int i = 0; i < DateList.Count; i++)
                                   {
                                    if (DateList[i].Equals(inventory.Date) && inventory.Quantity > 0)
                                    {
                                        
                                        count++;
    
                                        if (requestReservation.numDays == count)
                                        {
                                            for (int j = 0; j < DateList.Count; j++)
                                            {
                                               if (DateList[j].Equals(inventory.Date))
                                                    {
                                                      inventory.Quantity--;
    
                                                    }
                                              
                                            }
    
                                            requestReservation.reservationId = "000" + (number + 1).ToString();
                                            requestReservation.result = ReservationType.ReservationResultType.Success;
                                            count = 0;
    
                                            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);
                                                    }
    
                                                }
                                            }
                                        } // end of if num == counts
                                        else {
                                            requestReservation.reservationId = null;
                                            requestReservation.result = ReservationType.ReservationResultType.RoomNotAvailable;
                                            count = 0;
                                        }
    
                                    } //end of if 
    
                                   
                                }//end of forloop
                                                                                                
                            }//end of if statement (Roomtypes)
    
                        }//end of If statement( inventory.hotelID)
    
                    } // end of foreach inventory
                    number++;
                }// end of Foreach
    
                serial = new XmlSerializer(RequestReservation.GetType());
                sw = new StreamWriter(ReservationFilename);
                serial.Serialize(sw, RequestReservation);
                sw.Close();
    
    
            }// End of ReserveRoom Method

     

    Saturday, October 29, 2016 8:09 PM

All replies

  • User347430248 posted

    Hi ajs1351,

    we are not able to run the code mentioned by you because there are so many objects in that code that we don't know about how you declare in your code.

    you had mentioned that ," if there are three items in the list, that needs to be deduct, only the last one gets deducted, which then next iteration check, its not accurate. "

    do you mean you want to remove all the items from the list?

    if possible please try to create a simple demo of your problem that we can run on our side and reproduce the issue.

    so that we can try to look in to that and try to help you.

    then you can implement that solution in your original code.

    Regards

    Deepak

    Monday, October 31, 2016 7:50 AM