Singly Linked List, Employee Data

I need help with storing data in a singly-linked list where the nodes in the list correspond to each day in a 31-day month. An employee passes in between 1 and 5 slips per day every time they sell a product. These slips must include Product ID, staffID, and total dollar amount of products sold in a day. So I need a list of 31 different slips essentially.

The problem is, my output keeps doing the same slip every day for 31 days because I know it's working off the attributes of one employee. I believe my problem is in my Employee constructor, considering this would only randomize the values once upon object intialization. I need it to randomize ONE employee's attributes for each node in the singly linked list.



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
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
using namespace std;

class Slip;

class Employee {

	private:
	int prodID, prodCost, total;
	int NUMSLIPS;
	Employee* next;

	public:
	Employee();
	virtual ~Employee();

	friend class Slip;
};


#endif

Employee::Employee() {

	srand(time(0));
	NUMSLIPS = 1;
	prodID = 0;
	
	//for (int j = 1; j < 31; j++) {
		NUMSLIPS = rand() % 5 + 1;
		total = 0;
		for (int i = 1; i < NUMSLIPS; i++) {
			prodID = rand() % 5 + 1;

			if (prodID == 1)
				prodCost = 5;
			else if (prodID == 2)
				prodCost = 10;
			else if (prodID == 3)
				prodCost = 15;
			else if (prodID == 4)
				prodCost = 20;
			else
				prodCost = 50;

			total += prodCost;
			cout << "Slip " << i << ": ID" << prodID << " Cost: $" << prodCost << endl;
		}
	cout << "Total for day: $" << total << endl;
	//}
	
	next = 0;

}

Employee::~Employee() {
}


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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef SLIP_H
#define SLIP_H

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

using namespace std;

class Slip {
	private:
	Employee* topNode;

	public:
	Slip();
	Slip(const Slip &);
	virtual ~Slip();
	
	void push();
	void pop();
	Employee* top();
	
	bool isEmpty();
	bool isFull();

	Slip& operator=(const Slip &);
	
	class StackEmptyException { };
};

#endif

Slip::Slip() {
	topNode = 0;
}

Slip::Slip(const Slip& st) {
	if (st.topNode == NULL) {
		topNode == NULL;
	} else {
		topNode = new Employee();
		Employee* current = topNode;
		Employee* stTop = st.topNode;
		Employee* currentSt = stTop;
		while (currentSt->next != NULL) {
			current->next = new Employee();
			currentSt = currentSt->next;
			current = current->next;
		}
	}
}

Slip::~Slip() {
	while (!isEmpty())
		pop();
}

void Slip::push() {
	try {
		Employee* newNode = new Employee();
		newNode->next = topNode;
		topNode = newNode;
	} catch (bad_alloc &e) {
		cout << "memory allocation exception: " << e.what() << endl;
		exit(1);
	}
}

void Slip::pop() {
	if (isEmpty())
		throw StackEmptyException();

	Employee* discard = topNode;
	topNode = topNode->next;
	delete discard;
}

Employee* Slip::top() {
	if (isEmpty())
		throw StackEmptyException();
	
	return topNode;

}

bool Slip::isEmpty() {
	return (topNode == 0);
}

bool Slip::isFull() {
	return false;
}

Slip& Slip::operator=(const Slip& slip) {
	Slip temp(slip);


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
#include "Employee.h"
#include "Slip.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
	
	//Employee e1;

	Slip slip;

	for (int i = 1; i < 31; i++)
		slip.push();

	for (int i = 1; i < 31; i++) {
		slip.top();
		slip.pop();
	}
	
	return 0;

}
You design is wrong. Line 11 should be moved to the Slip class. The Employee needs an array of 5 slips and a count for the available slips.

The class Slip shouldn't know anything about Employee. So move most if not all functions to Employee.

The constructs should initialize the members not much more. So I would suggest that you move the generation of the random data to main.

So:
1
2
3
4
5
6
7
8
9
10
11
class Slip {
	private:
	int prodID = 0, prodCost = 0, total = 0; // total must be calculated by Employee

...

class Employee {

	private:
	Slip slip_array[5];
	int NUMSLIPS = 0;
I have since figured out my problem. Sometimes, I have a hard time viewing classes and objects as actual datatypes. I was trying to integrate a singly-linked list into my SalesPerson class rather than using Node class and List class as templated classes that simply form the architecture, if you will, of a singly-linked list. I then passed objects of types SalesPerson into the templated list. Thank you for you help though!
Topic archived. No new replies allowed.