Ejemplo n.º 1
0
void run_simulation(Pqueue &arrival_queue, int num_checkers, int break_time, ostream &os)
{
  int clock = 0;
  int customer_time = 0;
  int num_customers = arrival_queue.length();           //get number of customers
  Checker *check_array = new Checker[num_checkers];     //create array of checkers
  Cust* temp_customer = NULL;

  Pqueue shopping_q;
  Pqueue checker_q;

  //Initialize all of the checkers
  for(int i = 0; i < num_checkers; i++)
  {
    check_array[i].money = 100;
    check_array[i].break_left = 0;
    check_array[i].time_finished = 0;
    check_array[i].current_customer = NULL;
  }

  //start simulation
  for(clock = 1; num_customers > 0; clock++)
  {
    //----Entering Store----
    while(!arrival_queue.empty() && arrival_queue.get_priority_of_first() == clock)
    {
      temp_customer = arrival_queue.dequeue();                  //get customer to enter store
      temp_customer->print_entered(os, clock);                  //print that customer entered store
      customer_time = clock + temp_customer->get_items()*2;     //calculate when customer will be done
      shopping_q.enqueue(temp_customer, customer_time);         //que up customers by the time they are done shopping
    }

    //----Shopping----
    while(!shopping_q.empty() && shopping_q.get_priority_of_first() == clock)
    {
      temp_customer = shopping_q.dequeue();                     //get customer to enter checker's line
      temp_customer->print_finish_shop(os, clock);              //print that customer finished shopping
      checker_q.enqueue(temp_customer, 0);                      //place customer on checker q with no priority
    }

    //----See if any Checkers are done----
    for(int i = 0; i < num_checkers; i++)
    {
      if(check_array[i].current_customer != NULL && check_array[i].time_finished <= clock)
      {
        if(check_array[i].current_customer->get_alignment())    //if person is a robber
        {
          check_array[i].current_customer->print_finish_cs(os, clock, i, check_array[i].money);
          check_array[i].money = 0;                            //checker loses all money
          check_array[i].break_left = clock + break_time;
        }
        else                                                    //if person is a customer 
        {
          check_array[i].current_customer->print_finish_cs(os, clock, i, 0);
          check_array[i].money += check_array[i].current_customer->get_items()*3;       //customer gets 3*number of items
        }
        
        temp_customer = check_array[i].current_customer; 
        check_array[i].current_customer = NULL;
        delete temp_customer;
        num_customers--;                                        //a customer has been served and leaves the store
      }
    }

    //----Finding Available Checker----
      for(int i = 0; i < num_checkers; i++)
      {
        if(check_array[i].current_customer == NULL && check_array[i].break_left <= clock && !checker_q.empty())    //if checker is available
        {
          temp_customer = checker_q.dequeue();                                //get cusotomer waiting in line
          check_array[i].current_customer = temp_customer;                    //assign that checker the customer
          if(temp_customer->get_alignment())
          {
            customer_time = clock + 4;                                        //robbers take clock + 4
          }
          else
          {
            customer_time = clock + temp_customer->get_items();               //customers take *1 every item
          }
          check_array[i].time_finished = customer_time;                       //set the time the checker will be finished at
          temp_customer->print_start_cs(os, clock, i);
        }
      }
  }
  for(int i = 0; i < num_checkers; i++)
  {
    os << "registers[" << i << "] = $" << check_array[i].money << endl;
  }
  os << "time = " << clock << endl;
}
Ejemplo n.º 2
0
void run_simulation(Pqueue &arrival_queue, int num_checkers, int break_duration, ostream &os)
{
  struct Checker
  { 
    int money;
    int m_available_time;
    Cust *m_cust;
  }; 
  Checker *checkers = new Checker[num_checkers];//array, it is an array, array always is a pointer to the first element, 
  

  int clock;
  int num_customers=arrival_queue.size();     
  Pqueue shop_Q;
  Pqueue checkout_q;
  for(clock=0; num_customers > 0; clock++)
  {
    while(!arrival_queue.empty() &&  (arrival_queue.time_check() == clock))
    {
      Cust *customer = arrival_queue.dequeue();
      customer->print_entered(cout, clock);
      shop_Q.enqueue(customer, customer->time_done_shopping());
    }
    while(!shop_Q.empty() && (shop_Q.time_check() == clock))
    {
      Cust *cust_done = shop_Q.dequeue();
      cust_done->print_done_shopping(cout, clock);
      checkout_q.enqueue(cust_done, 0);
    }
for(int i=0; i<num_checkers; i++)
{
  /*
      if(checkers[i].m_cust->done_checkout(checkers[i].m_available_time))
      {
        cout << "right yeah" << endl;
      }
      */
cout << "break1" << endl;
cout << checkers[i].m_available_time<< endl;
cout << "clock: " << clock<< endl;
if(checkers[i].m_available_time == clock && checkers[i].m_cust == NULL)
//  if((checkers[i].m_cust->done_checkout(checkers[i].m_available_time)) && checkers[i].m_cust == NULL)
  {
      cout << "break2" << endl;
      checkers[i].m_cust->print(cout);
      if(checkers[i].m_cust->m_robber())
      {
    cout << "checker: " << i << endl;
      cout << "break3" << endl;
      cout << "time: " << clock << endl;
cout << "break4" << endl;
      cout << checkers[i].money<< endl;
      cout << "break5" << endl;
      checkers[i].m_cust->print_done_checkout_robber(os,clock,checkers[i].money,i);
      cout << "break6" << endl;
      checkers[i].money = 0;
      }
      else
      {
      checkers[i].money = checkers[i].money + checkers[i].m_cust->money_earned();
      checkers[i].m_cust->print_done_checkout_shopper(os, clock, checkers[i].money, i);
      }
/*
    if(checkers[i].m_cust->m_robber()==false)
    {
      cout << "break3" << endl;
      checkers[i].money = checkers[i].money + checkers[i].m_cust->money_earned();
      checkers[i].m_cust->print_done_checkout_shopper(os, clock, checkers[i].money, i);
    }
    else
    {
      checkers[i].m_cust->print_done_checkout_robber(os,clock,checkers[i].money,i);
      checkers[i].money = 0;
    } 
    */
   

    //Cust *tmp = checkers[i].m_cust;
    //delete [] tmp;
    //delete [] checkers[i].m_cust;
    num_customers--;
//    checkers[i].m_cust=NULL;
}
}

//cout << "num:" << num_customers << endl;

for(int i=0; i < num_checkers && checkout_q.empty() == false; i++)
{
cout << "break3" << endl;
  Cust *checkout_done=checkout_q.dequeue();
  checkers[i].m_cust = checkout_done;
  checkers[i].m_available_time =  checkers[i].m_cust->checkout_time() + checkers[i].m_available_time;
   // checkers[i].m_cust->done_checkout_time(checkers[i].m_available_time);
  checkers[i].m_cust->print_start_checkout(os, clock, i);
  if(checkers[i].m_cust->m_robber())
    checkers[i].m_available_time = checkers[i].m_available_time+break_duration;
}



cout << "break4" << endl;
cout << "clock: " << clock << endl;
if(clock==1000)
  break;
}
cout << "break5" << endl;
}