Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 Overview In this project, you will develop code for a scheduling application. Specifically, you will need to implement two classes, Scheduler and ScheduleNode. Scheduler

1 Overview

In this project, you will develop code for a scheduling application. Specifically, you will need to implement two classes, Scheduler and ScheduleNode. Scheduler is the main class, and it maintains the schedule as a doubly-linked collection of ScheduleNodes. Each ScheduleNode has a name and duration and represents a single event on the schedule. A header for both classes has been provided, and you are not allowed to modify this file.

The program starts with an empty schedule of length 100, to which the user may add (and later cancel) events. As user adds and removes events to the schedule, there are five main challenges your code should overcome:

All valid changes (e.g., scheduling a new event during a free period or cancelling an existing event) should be reflected when the schedule is printed.

Scheduling or cancelling events should not change the total length of the schedule.

The schedule should never show two consecutive events (or free periods) with the same name. Instead, consecutive events with the same name should be merged together into a single event with a duration equal to the sum of the original events.

Every event must have a positive (i.e., non-zero) duration.

All allocated ScheduleNodes must be deallocated.

2 Member function descriptions

The member functions you will need to implement are as follows:

bool Scheduler::isAvailable(unsigned start, unsigned duration) const: returns whether the time period from time start to start + duration is free; times are considered free if they occur during a ScheduleNode with name == "FREE". Valid times are 099; the head SchedulerNode is considered to start at time 0, and the next node starts immediately after the previous node ends. Times beyond the Schedulers length are not considered free.

void Scheduler::schedule(const string& name, unsigned start, unsigned duration): schedule a new event with the given name and duration at a given time, making sure not to create an invalid schedule (e.g., length 6= 100, consecutive nodes with same name, or 0- length node). You may assume that the user has already called isAvailable on the start and duration.

void Scheduler::free(unsigned start): frees up the time associated with the event that starts at the given time (i.e., sets that time back to FREE), without creating an invalid schedule. Has no effect if the time is already free or no event starts at that time.

void Scheduler::printSchedule() const: prints information for all of the events in the schedule, in the following format: TIME: NAME (DURATION) where TIME represents the start time of the event, NAME the events name, and DURATION the length of the event. Note that there are two spaces after the colon and one before the open parenthesis.

Scheduler::~Scheduler(): destroys the Scheduler, freeing all ScheduleNodes. You are not required to implement a copy constructor or copy assignment operator for the Scheduler.

void ScheduleNode::merge(ScheduleNode* other): (Optional) compares this ScheduleNode to its argument (which should be the next or previous node), and combines the two nodes into one with the same name where the length is the sum of the two original nodes. This function is not called by the driver and is totally optional, but you may find it helpful to implement this so that you can use it in other functions.

3 Driver file

You have been provided with a simple driver file that parses commands from cin and calls relevant Scheduler functions. The first command it recognizes is print, which just calls printSchedule.

The second command is add, which will add events to the schedule with schedule. The format for this command is:

add EVENT from TIME1 to TIME2

where EVENT should be replaced by the name of the event, TIME1 with the start time, and TIME2 with the ending time. If the indicated time is not available (!isAvailable), the driver will print an error message (Scheduling conflict: not added), or it will print a notification that the change was successful (Added to schedule).

The third command is cancel, which will remove events from the schedule with free. The format for this command is:

cancel TIME

where TIME should be replaced with the start time of the event to cancel. Both the add and cancel will print the modified schedule afterwards.

The last command recognized by the driver is quit, which ends the program. The driver can also be exited with Ctrl-C. All commands (and event names) are case-insensitive.

Scheduler-driver.cpp:

#include

#include

#include

using namespace std;

#include "scheduler.h"

void makeUppercase(string& str)

{

for (char& c : str)

c = toupper(c);

}

int main()

{

Scheduler schedule;

string cmd, opt;

unsigned time, duration;

schedule.printSchedule();

cin >> cmd;

makeUppercase(cmd);

while (cmd != "QUIT")

{

if (cmd == "CANCEL")

{

cin >> time;

schedule.free(time);

schedule.printSchedule();

}

else if (cmd == "ADD")

{

time = duration = -1;

string temp;

//add EVENT from X to Y

cin >> opt >> temp >> time >> temp >> duration;

makeUppercase(opt);

duration -= time;

if (opt == FREE)

cout << "Cannot add event " << FREE << ": use cancel instead" << endl;

else if (schedule.isAvailable(time, duration))

{

schedule.schedule(opt, time, duration);

cout << "Added to schedule" << endl;

}

else

cout << "Scheduling conflict: not added" << endl;

schedule.printSchedule();

}

else if (cmd == "PRINT")

schedule.printSchedule();

cin.clear();

cin.ignore(10000,' ');

cin >> cmd;

makeUppercase(cmd);

}

return 0;

}

Schedular.h:

#ifndef __SCHEDULER_H

#define __SCHEDULER_H

#include

#include

using namespace std;

const unsigned DEFAULT_LENGTH = 100;

const string FREE = "FREE";

class Scheduler;

class ScheduleNode;

class ScheduleNode

{

friend Scheduler;

private:

string name;

unsigned length;

ScheduleNode* prev;

ScheduleNode* next;

public:

ScheduleNode(string name, unsigned len = 0) : name(name)

{ length = len; next = prev = nullptr; cout << "CREATE" << endl; }

~ScheduleNode() { cout << "DESTROY" << endl; }

void merge(ScheduleNode* other);

};

class Scheduler

{

private:

ScheduleNode* head;

unsigned length;

public:

Scheduler(unsigned len = DEFAULT_LENGTH)

{ length = len; head = new ScheduleNode(FREE, len); }

bool isAvailable(unsigned start, unsigned duration) const;

void schedule(const string& name, unsigned start, unsigned duration);

void free(unsigned start);

void printSchedule() const;

~Scheduler();

};

#endif //__SCHEDULER_H

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions