printer spooling

i am given an assignment with the following requirements

Assume that a major printer company have outsourced their printer spooling software to your group. You are tasked to write software that spools the print requests from users and send it to the printer. Assume that there are only one printer and few users sharing it, and printing is based on First Come First Serve basis. The software has a menu to simulate users sending the print job to the printer and the printer finish the print job randomly, ranging from 1 second to 10 seconds. Assume that the printer has a limited buffer and any subsequent print job added will be returned back to the sender and a message printer buffer full will be prompt. At anytime, the printer will display the current printing job spool. Any print job that have remained in the print queue that have exceed the maximum holding time need to be discarded. You are free to assume STL Queue as the data structure, and any other parameter declared. State any assumptions in the report.


how do i implement the program using class and queues and ctime ?
Last edited on
For now, forget about class, queue or ctime. How would you do it using any means at your disposal?
erm this is wad i have done

#include <iostream>
#include <ctime>
#include <queue>


using namespace std;


void set_time ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}


int main()
{

int max=5,choice,Qnumber=0,Q=0;
char ans,x;
srand((unsigned)time(0));
queue<char>spool;
queue<char>spool1;

do
{
cout<<"********Queue Program**********\n";
cout<<"1. Add\n";
cout<<"2. Delete\n";
cout<<"3. Current job\n";
cout<<"4. Quit\n";
cout<<"Enter Your choice : ";
cin>>choice;
if(choice==1)
{
do
{
do
{
if(Qnumber<max)
{
cout<<"\nEnter name of print job to Be Added : ";
cin>>x;
spool.push(x);
Qnumber++;

}
else
cout<<"Queue Full";
cout<<"\nDo you want to print more?[y/n]: ";
cin>>ans;
}while(ans=='y'||ans=='Y');

}while(choice=='y'||choice == 'Y');
}
else if(choice==2)
{
do
{
do
{
if(Qnumber!=0)
{
cout<<"Print Job "<<spool.front()<<" has been deleted"<<endl;
spool.pop();
Qnumber--;
}
else
{
cout<<"Queue Empty"<<endl;
}
cout<<"Want to Delete More[y/n]? ";
cin>>ans;


}while(ans=='y'||ans=='Y');

}while(choice=='y'||choice=='Y');
}
else if(choice==3)
{
if(Qnumber==0)
cout<<"\nQueue Empty!!";
else
{
cout<<"\nDisplaying Queue\n";
while(!spool.empty())
{
for(int d =1;d<=Qnumber;d++)
{
cout<<d<<". "<<spool.front()<<endl;
spool1.push(spool.front());
spool.pop();
}
}
while(!spool1.empty())
{
spool.push(spool1.front());
spool1.pop();
}
for(int d =1;d<=Qnumber;d++)
cout<<d<<". "<<spool.front()<<endl;
}
}
else if (choice==4)
exit(1);

cout<<"\nWant To Go To The Main Menu[y/n]? ";
cin>>ans;

/*srand((unsigned)time(0));
int n=(rand()%10)+1;
for(int j=n;j>0;j--)
{
set_time(1);
}*/
}while(ans == 'y'|| ans == 'Y');

return 0;
}


i was wondering how i can change it into class and how to put the set_time function tgt with the program.
I have a few comments:
1. A queue knows if it's empty. You can call spool.empty() rather than using a seperate variable, Qnumber to track the number of items in a queue. You can find out how many items there are in a queue by calling spool.size().

See http://www.sgi.com/tech/stl/queue.html for a description of the standard queue.

2. Rather than holding a queue of just job names, a single char in this case, you can define a class that has a name and other details like time and so on, and have a queue of those.

could you show me how to write the code for the class for all the details?
If you wanted to use a name rather than just a char, you'd use a string. But if you wanted to add other information, you'd just add the fields as necessary.

Also, you don't have to use a class to represent a record, you can use struct. For example, if you wanted a record that contained an id, a name and the job time in seconds you might have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <queue>  // defines std::queue
#include <string> // defines std::string
#include <time.h> // defines time_t

struct CustomerJob
{
    int id;
    std::string name;
    time_t submit_time;
};

typedef std::queue<CustomerJob> CustomerJobQueue;

// Add an entry to the queue
void Add(CustomerJobQueue& q, CustomerJob job)
{
    job.submit_time = time();  // update the job submit time
    q.push(job);
}

// Create a dummy job and add it to the queue
int main()
{
    CustomerJobQueue job_queue;

    CustomerJob job;
    job.id = 7;
    job.name = "Times";

    Add(job_queue, job);

    return 0;
}


I've not tried to compiler the above code, but it illustrates my point.
Topic archived. No new replies allowed.