3 Replies - 8335 Views - Last Post: 12 April 2010 - 04:07 PM Rate Topic: -----

#1 Guest_Tammy69*


Reputation:

Help with Airline reservation code

Posted 11 April 2010 - 05:52 PM

Hello All,

I have this much code however I can not get the bool to work so that is does not assign the same seat twice Please help I have been at it for hours.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _8._19
{
    class Program
    {
        static void Main(string[] args)
        // Description: main entry point of program
        //  Parameters: none
        //     Returns: nothing
        {
            Random rand = new Random();
            bool[] seats = new bool[10];
            int inputI = 0;
            char inputC = ' ';
            bool quit = false;
            do
            {
                Console.Clear();
                int seatAssignF = rand.Next(0, 5);
                int seatAssignE = rand.Next(5, 10);
                Console.WriteLine("Please type [1] for First Class
                                  \nPlease type [2] for Economy
                                  \nPlease type [3] to exit the order system");
                inputI = Int32.Parse(Console.ReadLine());
                switch (inputI)
                {
                    case 1:
                        seats[seatAssignF] = true;
                        Console.WriteLine("Your seat: {0}", seatAssignF + 1);
                        Console.WriteLine("Press enter to continue...");
                        Console.ReadLine();
                        break;
                    case 2:
                        seats[seatAssignE] = true;
                        Console.WriteLine("Your seat: {0}", seatAssignE + 1);
                        Console.WriteLine("Press enter to continue...");
                        Console.ReadLine();
                        break;
                    case 3:
                        quit = true;
                        break;
                    default:
                        Console.WriteLine("ERROR::INVALID SELECTION");
                        quit = true;
                        break;
                }
            } while (!quit);
        }
    }
}



Thanks Tammy

Is This A Good Question/Topic? 0

Replies To: Help with Airline reservation code

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Help with Airline reservation code

Posted 11 April 2010 - 06:02 PM

Moved to C#, as there's nothing ASP.NET about this code.

You don't seem to ever check that the seat at array location seatAssignF or seatAssign is already taken? That would appear to be a requirement here.
Was This Post Helpful? 0
  • +
  • -

#3 ParagonDevx  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 17
  • Joined: 09-April 10

Re: Help with Airline reservation code

Posted 11 April 2010 - 09:13 PM

As JackOfAllTrades said, you need to keep track of which seats are taken.

Here's a solution for handling that, I added a list to keep track of the seats taken and all the logic to make sure you don't assign the same seat twice. It even tells you when a particular class of seats is booked:





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _8._19
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            bool[] seats = new bool[10];  
            //To keep a separate list of seats taken          
            List<int> seatsBooked = new List<int>(); 
            int inputI = 0;
            char inputC = ' ';
            bool quit = false;
            do
            {
                Console.Clear();
                //int seatAssignF = rand.Next(0, 5);-> Moved to switch-case 1: block
                int seatAssignE = rand.Next(5, 10);
                Console.WriteLine("Please type [1] for First Class" +
                                 " \nPlease type [2] for Economy" +
                                  "\nPlease type [3] to exit the order system");
                inputI = Int32.Parse(Console.ReadLine());
                switch (inputI)
                {
                    case 1:                                                 
                        int seatAssignF; //Variable moved from main loop
                        //Are there any seats booked already or this is the first?
                        if (seatsBooked.Count == 0) //if this is the first seat to be booked...
                        {
                            seatAssignF = rand.Next(0, 5);
                            seats[seatAssignF] = true;
                            seatsBooked.Add(seatAssignF);  //Add seat to the list of booked seats.                            
                        }
                        else
                        {

                            do //while there are available seats and current seat has not being assigned before.
                            {
                                seatAssignF = rand.Next(0, 5);
                                if (!seatsBooked.Contains(seatAssignF)) //if seatAssingF is not booked.
                                {
                                    seats[seatAssignF] = true;                                    

                                }
                                //repeat while the random seat number is already booked and there are  avaialable seats
                            } while (seatsBooked.Contains(seatAssignF) && seatsBooked.Count < 5);
                            
                            if (seatsBooked.Count < 5) //if seatsBooked list is not full for First Class
                            {
                                seatsBooked.Add(seatAssignF); //Add current random-generated seat to the list.
                            }
                           
                        }
                      
                        if (seatsBooked.Count >= 5) 
                        {
                            Console.WriteLine("All seats for First Class are booked");
                            Console.WriteLine("Press enter to continue...");
                            
                        }
                        else
                        {
                            Console.WriteLine("Your seat: {0}", seatAssignF + 1);
                            Console.WriteLine("Press enter to continue...");
                            
                        }
                        Console.ReadLine();
                        break;
                    case 2:
                        seats[seatAssignE] = true;
                        Console.WriteLine("Your seat: {0}", seatAssignE + 1);
                        Console.WriteLine("Press enter to continue...");
                        Console.ReadLine();
                        break;
                    case 3:
                        quit = true;
                        break;
                    default:
                        Console.WriteLine("ERROR::INVALID SELECTION");
                        quit = true;
                        break;
                }
            } while (!quit);
        
    
        }
    }
}




The same logic can be applied to take care of the Economy class code section...

This post has been edited by ParagonDevx: 11 April 2010 - 09:18 PM

Was This Post Helpful? 1
  • +
  • -

#4 Guest_Tammy69*


Reputation:

Re: Help with Airline reservation code

Posted 12 April 2010 - 04:07 PM

View PostParagonDevx, on 11 April 2010 - 08:13 PM, said:

As JackOfAllTrades said, you need to keep track of which seats are taken.

Here's a solution for handling that, I added a list to keep track of the seats taken and all the logic to make sure you don't assign the same seat twice. It even tells you when a particular class of seats is booked:





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _8._19
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            bool[] seats = new bool[10];  
            //To keep a separate list of seats taken          
            List<int> seatsBooked = new List<int>(); 
            int inputI = 0;
            char inputC = ' ';
            bool quit = false;
            do
            {
                Console.Clear();
                //int seatAssignF = rand.Next(0, 5);-> Moved to switch-case 1: block
                int seatAssignE = rand.Next(5, 10);
                Console.WriteLine("Please type [1] for First Class" +
                                 " \nPlease type [2] for Economy" +
                                  "\nPlease type [3] to exit the order system");
                inputI = Int32.Parse(Console.ReadLine());
                switch (inputI)
                {
                    case 1:                                                 
                        int seatAssignF; //Variable moved from main loop
                        //Are there any seats booked already or this is the first?
                        if (seatsBooked.Count == 0) //if this is the first seat to be booked...
                        {
                            seatAssignF = rand.Next(0, 5);
                            seats[seatAssignF] = true;
                            seatsBooked.Add(seatAssignF);  //Add seat to the list of booked seats.                            
                        }
                        else
                        {

                            do //while there are available seats and current seat has not being assigned before.
                            {
                                seatAssignF = rand.Next(0, 5);
                                if (!seatsBooked.Contains(seatAssignF)) //if seatAssingF is not booked.
                                {
                                    seats[seatAssignF] = true;                                    

                                }
                                //repeat while the random seat number is already booked and there are  avaialable seats
                            } while (seatsBooked.Contains(seatAssignF) && seatsBooked.Count < 5);
                            
                            if (seatsBooked.Count < 5) //if seatsBooked list is not full for First Class
                            {
                                seatsBooked.Add(seatAssignF); //Add current random-generated seat to the list.
                            }
                           
                        }
                      
                        if (seatsBooked.Count >= 5) 
                        {
                            Console.WriteLine("All seats for First Class are booked");
                            Console.WriteLine("Press enter to continue...");
                            
                        }
                        else
                        {
                            Console.WriteLine("Your seat: {0}", seatAssignF + 1);
                            Console.WriteLine("Press enter to continue...");
                            
                        }
                        Console.ReadLine();
                        break;
                    case 2:
                        seats[seatAssignE] = true;
                        Console.WriteLine("Your seat: {0}", seatAssignE + 1);
                        Console.WriteLine("Press enter to continue...");
                        Console.ReadLine();
                        break;
                    case 3:
                        quit = true;
                        break;
                    default:
                        Console.WriteLine("ERROR::INVALID SELECTION");
                        quit = true;
                        break;
                }
            } while (!quit);
        
    
        }
    }
}




The same logic can be applied to take care of the Economy class code section...

Was This Post Helpful? 0

Page 1 of 1