a line of Queue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • APEJMAN
    New Member
    • Feb 2008
    • 33

    a line of Queue

    Hi
    First of all I should say, I dont want to bother any one with my question or long program
    please if you have time, help me.( I dont mean that I want any one to do my program, just please if you are an expert and professional in C++, please help me and guid me how to fix the program)
    I did my best to comment my program.


    I am learning C++ and now I'm working on a program that:
    reads in a file "customers. txt" containing the information for bank customers. The file is formatted with each customer's name, arrival time (in minutes after 9:00), and service time (in minutes) on a separate line. The program moves each customer into the bank line at the arrival time. If the teller counter is available, the customer is moved out of the line to the teller. When the customer has spent the specified amount of time at the counter, they leave the bank and the next customer steps up. The program continues until all customers are serviced.

    I'm trying to make Multiple Tellers, at this time my program assumes the bank has only one teller/counter. I want to modify the program so that it can handle multiple tellers, so that the customers in line will move to any available teller. For example, "Chewbacca steps up to counter #2" or "Vader has left counter #4."
    but I have no idea how I can do that:
    right now my program works, but I dont know how to add multiple tellers.
    my program is:

    [code=cpp]
    /*************** *************** *************** *******
    ** The main routine simulates a line-up at the bank.
    *************** *************** *************** ********/
    int main() {
    //Read in the customer file.
    //Assume file is in format: Name ArrivalTime ServiceTime
    //Assume list is sorted based on distinct arrival times.

    string file_name; // the files name that user input
    ifstream fin;
    cout<<"please Enter your choice:\nChoice 1: (You can type in the name of your file.)\nchoice2 : (You can ask the program to generate a list of random customers.)\n\n ";

    int choice;
    cout<<"what is your choice? (1 or 2):";
    cin>>choice;
    while(choice!=1 && choice!=2){
    cout<<"You should type 1 or 2, try again:\nEnter you choice again:";
    cin>>choice;
    }
    Queue<Customer> customerList;
    Customer newCustomer;

    if(choice==1){ //choice 1 , user type his/her file_name to open
    cout<<"PLease type your file name:";
    cin >> file_name;
    cout<<"\n";

    fin.open( file_name.c_str ( ) );



    while (fin >> newCustomer)
    customerList.en ter(newCustomer );
    fin.close();
    if (customerList.i sEmpty()) {
    cout << "Could not read file customers.txt.\ n";
    return 0;
    }
    }

    if (choice==2){
    cout<<"Please specify the probability that a customer arrives each minute:";
    int probability;
    cin>>probabilit y;
    for (int i=0; i <= 540; i++) {
    if ( 1+rand()%100 <= probability )
    customerList.en ter( Customer("A customer", i, 5+rand()%11) );
    }



    }
    int num_teller; //number of tellers
    cout<<"please Enter the Number of tellers:";
    cin>>num_teller ;
    cout<<"\n";



    //The first customer immediately steps up to the counter, no waiting.
    //We start the clock running at first customer's arrival time.
    Customer atCounter = customerList.le ave();
    bool isCounterEmpty = false;
    int currentTime = atCounter.get_a rrivalTime();
    int startService = atCounter.get_a rrivalTime();
    printTime(curre ntTime);
    cout << " " << atCounter.get_n ame() << " has entered the bank.\n";
    printTime(curre ntTime);
    cout << " " << atCounter.get_n ame() << " stepped up to the counter.\n";

    //Set up our empty line.
    Queue<Customer> line;

    //Repeat while there are customers in bank or yet to arrive.
    while (!customerList. isEmpty() || !line.isEmpty() || !isCounterEmpty ) {

    //Check if someone enters line.
    if (!customerList. isEmpty() && (customerList.p eek()).get_arri valTime() <= currentTime) {
    printTime(curre ntTime);
    cout << " "<< (customerList.p eek()).get_name ()
    << " has entered the bank.\n";
    line.enter( customerList.le ave() );
    }

    //Check if someone leaves the counter.
    if ( !isCounterEmpty && (startService + atCounter.get_s erviceTime() <= currentTime) ) {
    isCounterEmpty = true;
    printTime(curre ntTime);
    cout << " "
    << atCounter.get_n ame()
    << " has left the counter.\n";
    }

    //Check if counter is empty and someone is waiting in line.
    if ( isCounterEmpty && !line.isEmpty() ) {
    atCounter = line.leave();
    startService = currentTime;
    isCounterEmpty = false;
    printTime(curre ntTime);
    cout << " " << atCounter.get_n ame()
    << " has stepped up to the counter.\n";
    }

    currentTime++; //Inefficient. Better to move to next event time.
    }

    //Compute queue statistics.


    return 0;
    }[/code]
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    OK firstly a few site rules

    1. Please do use code tags round your code
    2. We don't allow posting of complete solutions to homework questions (this may not be homework for you but it certainly is for someone else)
    3. Please read our posting guidelines where all this is explained

    Onto your problem

    Your program is limited by this declaration
    Customer atCounter = customerList.le ave();

    This forces the program into only being able to deal with 1 customer at a time, i.e. only 1 teller. You will need to change this declaration in order to handle multiple tellers and then your will need to change you code to handle this variables new type.

    Also why did you go to the bother of doing all that stuff creating your own Queue<class T> type when the C++ STD (Standard Template Library) already has a queue class in it?

    Comment

    • APEJMAN
      New Member
      • Feb 2008
      • 33

      #3
      thanks alot for your help
      sure, I'll read the guideline.
      I am trying to learn C++, that's why I dont use the library.

      take care

      Comment

      • APEJMAN
        New Member
        • Feb 2008
        • 33

        #4
        should I work on the leave() function?
        thanks

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by APEJMAN
          should I work on the leave() function?
          thanks
          There should be no need to work on the leave function as it works on a single entity (the line).

          You may wish to google for the "sleeping barber problem" which this is a variation on.

          Comment

          • APEJMAN
            New Member
            • Feb 2008
            • 33

            #6
            Atually I think that its better to have a vector like
            vector<Customer > AtTeller(num_te ller); // a vector including number of tellers

            the num_teller is the number of tellers. btu how can I make a loop , so that the loop checkes if teller[i] is empty?

            thanks

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by APEJMAN
              Atually I think that its better to have a vector like
              vector<Customer > AtTeller(num_te ller); // a vector including number of tellers

              the num_teller is the number of tellers. btu how can I make a loop , so that the loop checkes if teller[i] is empty?
              vector sounds right for your tellers, in fact when using the STL a common school of thought is that you should use vector unless you have a specific reason to use one of the other classes. vector is the class with the least overhead.

              You have several options in making a loop, you could try using one of the Standard Library Algorithms, for_each or find_if look promising or you can just write a standard for loop

              [code=cpp]
              for(int ix=0; ix<AtTeller.siz e(); ix++)
              {
              // Here perform operations on AtTeller[ix]
              }
              [/code]

              Comment

              • APEJMAN
                New Member
                • Feb 2008
                • 33

                #8
                I wrote this , but it dosent work
                would you take a look at it, thanks.

                Code:
                for( int j=0; j<AtTeller.size(); j++){
                if (!line.isEmpty() /*&& isCounterEmpty*/){
                	if (AtTeller[i].get_name()=="DEFAULT"){
                		AtTeller[i]=line.leave();
                		startTime[i]=currentTime;
                		printTime(startTime[i]);
                		cout<<"  "<<AtTeller[i].get_name()<<" has sptepped up to the Counter #"<<i<<"\n";
                
                	}
                
                }
                
                }

                Comment

                Working...