Home > Software design >  Iterating through a list populated by a text file to look for a reference C#
Iterating through a list populated by a text file to look for a reference C#

Time:10-08

I'm new to C# and i'm trying to check whether a guest has checked in on a hotel app. I'm trying to get all bookings in a text file pass them in to a list then read through the list looking for the booking in reference.

The problem i'm having is that it only seems to put the first line of the text file into the list. Could anyone help me solve this?

One way i've tried:

public void CheckBookingReference()
        {

            List<string> BookingList = File.ReadAllLines(BookingFilePath).ToList();

            foreach (var BookingLine in BookingList.ToList())
            {
                string[] bookings = BookingLine.Split(',');

                int _BookingReferenceNumber = int.Parse(bookings[5]);

                if (_BookingReferenceNumber == BookingReferenceNumber)
                {
                    Console.WriteLine("Booking found, check in complete.");

                   break;
                }
                else
                {
                    throw new Exception("BookingNotFoundException");
                                   
                }
            }
        }

Another way i've also tried:

       public void CheckBookingReference()
        {
            List<string> BookingList = new List<string>();
            using (var sr = new StreamReader(BookingFilePath))
            {
                while (sr.Peek() >= 0)
                    BookingList.Add(sr.ReadLine());

                foreach (var BookingLine in BookingList.ToList())
                {
                    string[] bookings = BookingLine.Split(',');

                    int _BookingReferenceNumber = int.Parse(bookings[5]);

                    if (_BookingReferenceNumber == BookingReferenceNumber)
                    {
                        //throw new Exception("GuestAlreadyCheckedInException");
                        Console.WriteLine("booking found");
                        break;

                    }
                    else if (_BookingReferenceNumber != BookingReferenceNumber)
                    {
                        Console.WriteLine("not found");
                        break;
                    }
                }
            }
        }

CodePudding user response:

With the code you have, if the first line doesn't match, you throw the exception.

Try this:

public void CheckBookingReference()
        {

            List<string> BookingList = File.ReadAllLines(BookingFilePath).ToList();

            foreach (var BookingLine in BookingList.ToList())
            {
                string[] bookings = BookingLine.Split(',');

                int _BookingReferenceNumber = int.Parse(bookings[5]);

                if (_BookingReferenceNumber == BookingReferenceNumber)
                {
                    Console.WriteLine("Booking found, check in complete.");
                    return;
                }
            }
            throw new Exception("BookingNotFoundException");
        }
  • Related