[Classes and Objects]Job Manager

Description

In this assignment, you are required to finish two classes’ definition according to their declaration respectly.
.1. Two basic concepts are needed before you begin to code: 1. Singleton, 2. linked Queue.

SINGLETON: Singleton is a kind of frequently-used Design Pattern(but often be prone to misuse).
Ensure a class only has one instance, and provide a global point of access to it.
Use the Singleton pattern when

there must be exactly one instance of a class, and it must be accessible to clients from a wellknown access point.
when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
2. Class Job has:
(1)one static variable “number”, which indicates current object’s id. you shold increment it when you create an new object. Attention: when the object was destoyed, this variable do not subtract by 1;
(2)three memeber variables:

id means this object’s id. you need to set the current number to id when create an object.
priority, means the importance among all jobs.
nextJob, a Job pointer, which points to the next Job.
(3)function toString return a string, its format is like: [id:priority], for example: [0:234]…

  1. Class JobManager only has one instance, JobManager can manage the jobs in the job queue.
    JobManager manages the job queue according to the principle FIFO(First In, First Out), no matter what the priority is…
    This Class has a member variable named “jobFrontPointer”(Job *), which points to the first Job in the queue(first add).
    static variable instance is the only instance of this class. You need to implement two static function getInstance and deleteInstance. “getInstance” will return the only instance of this class; “deleteInstance” will delete the only instance, if delete successfully, return true; otherwise, return false.
  void addJob(int pri = 0); which adds one job(its priority is pri, default: 0) to the end of queue.  
  void finishOneJob(); which finish the job in the begin and pop it out of the queue.  
  void sortJob(); which sorts the current queue according to their priority (if same, then the smaller id is in front of the larger one).  
  void printJob(); print the queue's job, like "[2:23432]->[3:1]->[4:5]->[5:0]->[6:6666]" with endl.  
  int getNumOfJob(); return the number of jobs in the queue.  
  void clear(); clear the queue.

Hint

singleton and linked queue…

Attention:

the declaration of Job has changed by younglee, add method getter and setter for member variable “id”….

Job.cpp
#include <string>
#include <sstream>
#include "Job.h"

using std::string;
using std::stringstream;

int Job::number = 0;

Job::Job(int priority)
    :id(number++),priority(priority),nextJob(NULL){}
void Job::setId(int id)
{
    this->id = id;
}
int Job::getId() const
{
    return id;
}
void Job::setPriority(int priority)
{
    this->priority = priority;
}
int Job::getPriority() const
{
    return priority;
}
void Job::setNext(Job *job)
{
    nextJob = job;
}
Job *Job::getNext() const
{
    return nextJob;
}
string Job::toString()
{
    stringstream ss;
    ss << "[" << id << ":" << priority << "]";
    return ss.str();
}
JobManager.cpp
#include <iostream>
#include "JobManager.h"

using std::cout;
using std::endl;

JobManager *JobManager::instance = NULL;

JobManager *JobManager::getInstance()
{
    if (instance == NULL)
    {
        instance = new JobManager;
    }
    return instance;
}
bool JobManager::deleteInstance()
{
    if (instance == NULL)
        return false;
    if (instance != NULL)
    {
        delete instance;
        instance = NULL;
        return true;
    }
}
void JobManager::addJob(int priority)
{
    if (jobFrontPointer == NULL)
    {
        jobFrontPointer = new Job(priority);
    }
    else
    {
        Job *pArr = jobFrontPointer;
        while (pArr->getNext() != NULL)
        {
            pArr = pArr->getNext();
        }
        Job *pArr2 = new Job(priority);
        pArr->setNext(pArr2);
    }
}
void JobManager::finishOneJob()
{
    if (jobFrontPointer != NULL)
    {
        Job *pArr = jobFrontPointer->getNext();
        delete jobFrontPointer;
        jobFrontPointer = pArr;
    }
}
void JobManager::sortJob()
{
    if (jobFrontPointer == NULL)
        return;
    Job *pArr = jobFrontPointer;
    if (pArr->getNext() == NULL)
        return;
    for (int i = 0; i < getNumOfJob() - 1; i++)
    {
        for (int j = 0; j < getNumOfJob() - 1 - i; j++)
        {
            if (pArr->getPriority() < pArr->getNext()->getPriority())
            {
                int temp1 = pArr->getId();
                int temp2 = pArr->getPriority();
                pArr->setId(pArr->getNext()->getId());
                pArr->getNext()->setId(temp1);
                pArr->setPriority(pArr->getNext()->getPriority());
                pArr->getNext()->setPriority(temp2);
            }
            if (pArr->getPriority() == pArr->getNext()->getPriority())
            {
                if (pArr->getId() > pArr->getNext()->getId())
                {
                    int temp1 = pArr->getId();
                    int temp2 = pArr->getPriority();
                    pArr->setId(pArr->getNext()->getId());
                    pArr->getNext()->setId(temp1);
                    pArr->setPriority(pArr->getNext()->getPriority());
                    pArr->getNext()->setPriority(temp2);
                }
            }
            pArr = pArr->getNext();
        }
        pArr = jobFrontPointer;
    }
}
void JobManager::printJob()
{
    if (jobFrontPointer == NULL)
    {
        cout << "empty!" << endl;
        return;
    }
    Job *pArr = jobFrontPointer;
    while (pArr != NULL)
    {
        cout << pArr->toString();
        pArr = pArr->getNext();
        if (pArr != NULL)
        {
            cout << "->";
        }
    }
    cout << endl;
}
int JobManager::getNumOfJob()
{
    int count = 0;
    Job *pArr = jobFrontPointer;
    while (pArr != NULL)
    {
        count++;
        pArr = pArr->getNext();
    }
    return count;
}
void JobManager::clear()
{
    Job *pArr = jobFrontPointer;
    while (pArr != NULL)
    {
        pArr = pArr->getNext();
        jobFrontPointer->setNext(NULL);
        delete jobFrontPointer;
        jobFrontPointer = pArr;
    }
}
JobManager::~JobManager()
{
    if (jobFrontPointer != NULL)
        clear();
}
JobManager::JobManager()
    : jobFrontPointer(NULL) {}

Job.h
/*
 * declaration for class Job..
 */
#ifndef JOB_H

#include<string>
using namespace std;

class Job {
    public:
        explicit Job(int priority = 0);
        void setId(int id);
        int getId() const;
        void setPriority(int priority);
        int getPriority() const;
        void setNext(Job *job);
        Job *getNext() const;
        string toString();
    private:
        static int number;
        int id;
        int priority;
        Job *nextJob;
};

#endif

JobManager.h
#ifndef JOBMANAGER_H
#define JOBMANAGER_H

#include <iostream>
#include "Job.h"

// disallow copy constructor and assignment operator
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&);               \
  void operator=(const TypeName&)

class JobManager {
    public:
        // get the unique instance
        static JobManager* getInstance();
        static bool deleteInstance();

        void addJob(int priority = 0);
        void finishOneJob();
        void sortJob();
        void printJob();
        int getNumOfJob();
        void clear();

    private:
        Job * jobFrontPointer;
        ~JobManager();
        JobManager();
        static JobManager* instance;
        DISALLOW_COPY_AND_ASSIGN(JobManager);
};

#endif

main.cpp
#include "JobManager.h"
using namespace std;

int main() {
    JobManager *manager1 = JobManager::getInstance();
    JobManager *manager2 = JobManager::getInstance();
    int pris[5] = {234, 23, 23432, 1, 5};
    for (int i = 0; i < 5; i++) {
        manager1->addJob(pris[i]);
    }
    manager2->printJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;
    manager1->finishOneJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;
    manager2->finishOneJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;
    manager1->addJob();
    manager1->addJob(6666);
    manager1->printJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;
    manager2->clear();
    manager1->printJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;

    int jobNum, jobPriority, deleteNum;
    cin >> jobNum;
    for (int i = 0; i < jobNum; i++) {
        cin >> jobPriority;
        manager2->addJob(jobPriority);
    }
    manager1->sortJob();
    manager2->printJob();
    cin >> deleteNum;
    while (deleteNum--) {
        manager1->finishOneJob();
    }
    manager1->printJob();
    cout << "The number of job is: " << manager2->getNumOfJob() << endl;

    if (JobManager::deleteInstance()) cout << "Delete successfully!\n";
    else cout << "Delete failure!\n";

    if (JobManager::deleteInstance()) cout << "Delete successfully!\n";
    else cout << "Delete failure!\n";
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值