Need help with undo and redo functionality for my spec

I need help implementing UNDO / REDO.
The functionality should do something like this:
https://gyazo.com/a82f4384d24b452bad4e596dbf7a995e

It uses my Task class, which has 3 derived classes ShoppingTask, HomeworkTask and EventTask

The commands ADD,COMPLETE,REMOVE is in another class called ToDoListDriver, which has 2 objects called completedand outstanding, which has an implemented SortedVector as its underlying data structure that sorts the tasks by deadline. The commands ADD and REMOVE adds an outstanding task into the vector, while COMPLETE marks a task in outstanding as complete, move it to completed and deletes the task.

Here are some of my files

Task.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Task{

public:
	Task();
	~Task(){}
	void setType(string t){ type = t; }
	string getType(){ return type; }
	void setDeadline(int dl){ deadline = dl; }
	int getDeadline(){ return deadline; }
	void setDescription(string des){ description = des; }
	string getDescription(){ return description; }
	friend bool operator<(Task& lhs, Task& rhs){ return lhs.getDeadline()<rhs.getDeadline(); }
	struct LessThanOperator{
		bool operator()(Task* left, Task* right){ return *left < *right; }
	};
	virtual void print();
	virtual void detailed(){ print(); }
	virtual string save();

protected:
	int deadline;
	string description;
	string type;
};


ToDoListDriver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ToDoListDriver{
	public:
		ToDoListDriver(){};
		~ToDoListDriver(){};
		void removeOutstandingTask(unsigned int num);
		void completeTask(unsigned int num);
		void createOutstandingTask(Task* t);
		void outputOutstandingTask();
		void outputOutstandingTaskDetails();
		void outputCompletedTask();
		bool isOutstandingTaskEmpty();
		bool isCompletedTaskEmpty();
		unsigned int sizeOfList(){ return outstanding.getSize(); }
		Task* outstandingTaskPosition(unsigned int num){ return outstanding.getTask(num); }
		unsigned int setANumber(Task* t);

	private:
		TaskList completed;
		TaskList outstanding;


ToDoListDriver.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

void ToDoListDriver::removeOutstandingTask(unsigned int num){

	if(outstanding.isEmpty() == false){
		outstanding.removeFromList(num);
		cout << "Task removed successfully" << endl;
		return;
	}
	cout << "You have no outstanding tasks!" << endl;
}

void ToDoListDriver::completeTask(unsigned int num){
	
	Task* temp;
	temp = outstanding.getTask(num-1);
	completed.addToList(temp);
	outstanding.removeFromList(num);
}

void ToDoListDriver::createOutstandingTask(Task* t){

	outstanding.addToList(t);
}

void ToDoListDriver::outputOutstandingTask(){

	if(outstanding.isEmpty() == false){
		cout<< "Your outstanding tasks are:" << endl;
		outstanding.printAllTasks();
		return;
	}
	cout << "You have no incomplete tasks!" << endl;
}

void ToDoListDriver::outputCompletedTask(){

	if(completed.isEmpty() == false){
		completed.printAllTasks();
		return;
	}
	cout << "You have no completed tasks!" << endl;
}

void ToDoListDriver::outputOutstandingTaskDetails(){

	if(outstanding.isEmpty() == false){
		cout<< "Your outstanding tasks are:" << endl;
		outstanding.detailAllTasks();
		return;
	}
	cout << "You have no incomplete tasks!" << endl;
}

bool ToDoListDriver::isOutstandingTaskEmpty(){

	if(outstanding.isEmpty() == true){
		cout << "You have no incomplete tasks!" << endl;
		return true;
	}
	return false;
}

bool ToDoListDriver::isCompletedTaskEmpty(){

	if(completed.isEmpty() == true){
		cout << "You have no completed tasks!" << endl;
		return true;
	}
	return false;
}

unsigned int ToDoListDriver::setANumber(Task* t){

	createOutstandingTask(t);
	return 0;
}


This is my command class which deals with the functionality of UNDO/REDO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Command{

	public:
		Command();
		~Command();
		void pushToUndo(string aCommand);
		void pushToRedo();
		bool isUndoEmpty();
		bool isRedoEmpty();
		// Don't think i need this here
		Task* getTheDataForUndo(unsigned int num);


	private:
		stack<string>undo;
		stack<string>redo;
		stack<Task*>deleted;

}


This is my logic for UNDO/REDO. I have stacks of type string for UNDO and REDO, when I undo an ADD, i pop it off and put it into my REDO stack, where I can later pop it out if the user inputs REDO.
I have a stack that stores the Task* for when the user inputs UNDO for a task that they REMOVE. Since my vector sorts by deadline, I was thinking about implementing a way to keep track of the number of times i use ADD. Since it sorts by deadline the vector could come out as something like this
[2|3|1|4|7|5|6] where the numbers indicate the 1st, ADD command, 2nd ADD command, etc.. I don't know how to implement this, and this is my last functionality for my program to work. Any help would be greatly appreciated.
Last edited on
Topic archived. No new replies allowed.