CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2011
    Posts
    2

    Tired. Need Help. Please. :(

    I'm dumb. Theres probably a much more efficient way of doing this, but I'm a few weeks behind in this class and I don't know of one, so I'm using what I know. This is the Airline Seating Programming that I'm sure lots of people here have encountered at some point.

    ( Airline Reservations System ) A small airline has just purchased a computer for its new automated reservations system. You have been asked to develop the new system. You are to write an application to assign seats on each flight of the airline's only plane (capacity: 10 seats).

    Your application should display the following alternatives: Please type 1 for First Class and Please type 2 for Economy . If the user types 1 , your application should assign a seat in the first-class section (seats 1-5). If the user types 2 , your application should assign a seat in the economy section (seats 6-10).

    Use a one-dimensional array of simple type bool to represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that all the seats are empty. As each seat is assigned, set the corresponding element of the array to TRue to indicate that the seat is no longer available.

    Your application should never assign a seat that has already been assigned. When the economy section is full, your application should ask the person if it is acceptable to be placed in the first-class section (and vice versa). If yes, make the appropriate seat assignment. If no, display the message " Next flight leaves in 3 hours."

    This is what I've got.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
        class Airline2
        {
            string classChoice;
    
             const int ARRAY_LENGTH = 10;
             bool[] seatList = new bool[ARRAY_LENGTH];
    
            public void PromptForSeat()
            {
                Console.WriteLine("Please Choose Class: Type '1' for First Class or '2' for Economy Class: ");
            }
    
            public void AssignSeat() 
            {
                classChoice = Console.ReadLine();
                string answer;
    
                if (classChoice == "1") 
                {
                    if (seatList[0] == false)
                    {
                    seatList[0] = true;
                    AssignSeat();
                    }
                    else if (seatList[1] == false)
                    {
                        seatList[1] = true;
                        AssignSeat();
                    }
                    else if (seatList[2] == false)
                    {
                        seatList[2] = true;
                        AssignSeat();
                    }
                    else if (seatList[3] == false)
                    {
                        seatList[3] = true;
                        AssignSeat();
                    }
                    else if (seatList[4] == false)
                    {
                        seatList[4] = true;
                        AssignSeat();
                    }
                    else if (seatList[4] == true)
                    {
                        Console.WriteLine("No more seats available. Downgrade to Economy? Y/N?");
                        answer = Console.ReadLine();
    
                        if (answer == "y")
                        {
                            classChoice = "2";
                        }
                        else
                            Console.WriteLine("Sorry, the next flight is in 3 hours.");
                    } 
    
    
                } else
                    if (classChoice == "2")
                    {
                        if (seatList[5] == false)
                        {
                            seatList[5] = true;
                            AssignSeat();
                        }
                        else if (seatList[6] == false)
                        {
                            seatList[6] = true;
                            AssignSeat();
                        }
                        else if (seatList[7] == false)
                        {
                            seatList[7] = true;
                            AssignSeat();
                        }
                        else if (seatList[8] == false)
                        {
                            seatList[8] = true;
                            AssignSeat();
                        }
                        else if (seatList[9] == false)
                        {
                            seatList[9] = true;
                            AssignSeat();
                        }
                        else if (seatList[9] == true)
                        {
                            Console.WriteLine("No more seats available. Upgrade to First? Y/N?");
                            answer = Console.ReadLine();
    
                            if (answer == "y")
                            {
                                classChoice = "1";
                            }
                            else
                                Console.WriteLine("Sorry, the next flight is in 3 hours.");
                        }
                    }
            }
        }
    It breaks down after all the seats in a given class have been taken and it prompts the user to see if they'd like to upgrade/downgrade. I've been playing catch up for this class all night and I'm dog tired, so this could be blaringly obvious and I'm just not seeing it. Thanks in advance to anyone that can help.
    Last edited by elbumpy; December 3rd, 2011 at 03:21 PM.

  2. #2
    Join Date
    Dec 2011
    Posts
    2

    Re: Tired. Need Help. Please. :(

    Nobody? Am I even on the right path?

    This is what I've gotten since then. What do you guys think?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
        class Airline2
        {
            string classChoice;
    
             const int ARRAY_LENGTH = 10;
             bool[] seatList = new bool[ARRAY_LENGTH];
    
            public void PromptForSeat()
            {
                Console.WriteLine("Please Choose Class: Type '1' for First Class or '2' for Economy Class: ");
            }
    
            public void AssignSeat() 
            {
                classChoice = Console.ReadLine();
                string answer;
    
                if (classChoice == "1") 
                {
                    if (seatList[0] == false)
                    {
                    seatList[0] = true;
                    AssignSeat();
                    }
                    else if (seatList[1] == false)
                    {
                        seatList[1] = true;
                        AssignSeat();
                    }
                    else if (seatList[2] == false)
                    {
                        seatList[2] = true;
                        AssignSeat();
                    }
                    else if (seatList[3] == false)
                    {
                        seatList[3] = true;
                        AssignSeat();
                    }
                    else if (seatList[4] == false)
                    {
                        seatList[4] = true;
                        AssignSeat();
                    }
                    else if (seatList[4] == true)
                    {
                        Console.WriteLine("No more seats available. Downgrade to Economy? Y/N?");
                        answer = Console.ReadLine();
    
                        if (answer == "y")
                        {
                            classChoice = "2";
                        }
                        else if (answer == "n")
                        {
                            Console.WriteLine("Sorry, the next flight is in 3 hours.");
                            AssignSeat();
                        }
                    } 
    
    
                }
                    
                if (classChoice == "2")
                    {
                        if (seatList[5] == false)
                        {
                            seatList[5] = true;
                            AssignSeat();
                        }
                        else if (seatList[6] == false)
                        {
                            seatList[6] = true;
                            AssignSeat();
                        }
                        else if (seatList[7] == false)
                        {
                            seatList[7] = true;
                            AssignSeat();
                        }
                        else if (seatList[8] == false)
                        {
                            seatList[8] = true;
                            AssignSeat();
                        }
                        else if (seatList[9] == false)
                        {
                            seatList[9] = true;
                            AssignSeat();
                        }
                        else if (seatList[9] == true)
                        {
                            Console.WriteLine("No more seats available. Upgrade to First? Y/N?");
                            answer = Console.ReadLine();
    
                            if (answer == "y")
                            {
                                classChoice = "1";
                            }
                            else if (answer =="n")
                            {
                                Console.WriteLine("Sorry, the next flight is in 3 hours.");
                                AssignSeat();
                            }
                        }
                    }
            }
    
            public void NoSeats()
            {
                if (seatList[4] && seatList[9] == true)
                {
                    Console.WriteLine("Sorry No More Seats Available.");
                }
                else
                    AssignSeat();
    
            }
    
    
        }
    Last edited by elbumpy; December 3rd, 2011 at 04:02 PM.

  3. #3
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Tired. Need Help. Please. :(

    Quote Originally Posted by elbumpy View Post
    Nobody? Am I even on the right path?

    This is what I've gotten since then. What do you guys think?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
        class Airline2
        {
            string classChoice;
    
             const int ARRAY_LENGTH = 10;
             bool[] seatList = new bool[ARRAY_LENGTH];
    
            public void PromptForSeat()
            {
                Console.WriteLine("Please Choose Class: Type '1' for First Class or '2' for Economy Class: ");
            }
    
            public void AssignSeat() 
            {
                classChoice = Console.ReadLine();
                string answer;
    
                if (classChoice == "1") 
                {
                    if (seatList[0] == false)
                    {
                    seatList[0] = true;
                    AssignSeat();
                    }
                    else if (seatList[1] == false)
                    {
                        seatList[1] = true;
                        AssignSeat();
                    }
                    else if (seatList[2] == false)
                    {
                        seatList[2] = true;
                        AssignSeat();
                    }
                    else if (seatList[3] == false)
                    {
                        seatList[3] = true;
                        AssignSeat();
                    }
                    else if (seatList[4] == false)
                    {
                        seatList[4] = true;
                        AssignSeat();
                    }
                    else if (seatList[4] == true)
                    {
                        Console.WriteLine("No more seats available. Downgrade to Economy? Y/N?");
                        answer = Console.ReadLine();
    
                        if (answer == "y")
                        {
                            classChoice = "2";
                        }
                        else if (answer == "n")
                        {
                            Console.WriteLine("Sorry, the next flight is in 3 hours.");
                            AssignSeat();
                        }
                    } 
    
    
                }
                    
                if (classChoice == "2")
                    {
                        if (seatList[5] == false)
                        {
                            seatList[5] = true;
                            AssignSeat();
                        }
                        else if (seatList[6] == false)
                        {
                            seatList[6] = true;
                            AssignSeat();
                        }
                        else if (seatList[7] == false)
                        {
                            seatList[7] = true;
                            AssignSeat();
                        }
                        else if (seatList[8] == false)
                        {
                            seatList[8] = true;
                            AssignSeat();
                        }
                        else if (seatList[9] == false)
                        {
                            seatList[9] = true;
                            AssignSeat();
                        }
                        else if (seatList[9] == true)
                        {
                            Console.WriteLine("No more seats available. Upgrade to First? Y/N?");
                            answer = Console.ReadLine();
    
                            if (answer == "y")
                            {
                                classChoice = "1";
                            }
                            else if (answer =="n")
                            {
                                Console.WriteLine("Sorry, the next flight is in 3 hours.");
                                AssignSeat();
                            }
                        }
                    }
            }
    
            public void NoSeats()
            {
                if (seatList[4] && seatList[9] == true)
                {
                    Console.WriteLine("Sorry No More Seats Available.");
                }
                else
                    AssignSeat();
    
            }
    
    
        }

    You code could be shortened by adding some for loops into your code. Also I am not sure why you are calling AssignSeat(); from within itself or where in your code you call the NoSeats()

    Example of for loop
    Code:
    // Create variables
    bool[] IsAssigned = new bool[10];
    
    // Check if seat is available
    for (int i = 0; i < IsAssigned.Length; i++)
    {
       if (IsAssigned[i] == false)
       {
          // A seat has been found and filled so break out of the loop to prevent filling all seats in a single choice.
          IsAssigned[i] = true;
          break;
       }
    }
    That code example loops through the list of seats until it finds one that is false. Once the false one has been found it then sets the flag to true and breaks out of the loop.

    You could modify the example to work out for you. I would make two separate methods for each class and then prompt the user if they would like to upgrade/downgrade if seats are full in a specific class.

    For example:
    Code:
    public void FirstClass()
    {
       // Insert code here to assign a seat in first class
    
      // If all seats are assigned in first class then prompt user if they want to downgrade to economy class if yes then run EconomyClass();
       if (userResponse.ToLower() == "y")
          EconomyClass();
    }
    
    public void EconomyClass()
    {
       // Insert code here to assign a seat in first class
    
       // If all seats are assigned prompt user for upgrade to first class
       if (userResponse.ToLower() == "y")
          FirstClass();
    }
    That is just a rough outline that will hopefully help you out. I'm sure the more experienced users will chime in and provide you with better solutions to your issue.

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Tired. Need Help. Please. :(

    Mmm.. youre using recursion to stay in your program, you should perhaps use a loop in main. Perhaps the biggest problem is that you hvent written your logic out in plain english first, either pen and paper or in comments. Comments are better because when you write the code under the comment, you end up with a nicely commented program without having to write comments after (programmers rarely do that, they should, they get more points for it)

    Your method names should always be verbs (doing-words or phrases) because methods do things.. and they should return suitable values for their verb i.e. your PromptUserForSeat() should both print the message AND do the readline() and store the input into a string and return the string

    Code:
    //write a method to prompt user for seat, and return the choice
    
    //write a method to assign a seat in first class and return a boolean true if it succeeded or false if not
    
    
    //write a method to assign a seat in economy and return a boolean
    
    
    //in the main method, do this logic
    
      //start a loop
    
      //prompt the user for a seat choice
      //if first class requested
        //if assign first class seat worked
          //print message user is in first class
        //else
          //print all seats in first are full, would you like economy?
          //if yes (downgrade)
            //if assign economy works
              //print message
            //else plane is full
              //print message
          //else (no downgrade)
            //next plane in 3 hrs
      //else(second class requested)
        //if assign economy class seat worked
          //print message user is in economy class
        //else
          //print all seats in economy are full, would you like first?
          //if yes (upgrade)
            //if assign first works
              //print message
            //else plane is full
              //print message
          //else (no upgrade)
            //next plane in 3 hrs
    
      //end of loop
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured