Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 03-Practice-08.03.2022.cpp
blob: b14815909a39dc08299a50b5cb4d3a27093326e9 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <fstream>
#include <cstring>

const int LINE_BUFFER_SIZE = 1024;
const int MAX_COMPANY_NAME_SIZE = 25;

struct JobOffer {
	char CompanyName[MAX_COMPANY_NAME_SIZE];

	unsigned int ProgrammersInTeam;
	unsigned int PayedTimeOff;
	long long SalaryAfterFinishedProduct;
};

void coutJobOffer(JobOffer& jo) {
	std::cout << jo.CompanyName << " | Programmers: " << jo.ProgrammersInTeam << " | Time Off: " << jo.PayedTimeOff << " | Salary: " << jo.SalaryAfterFinishedProduct << std::endl;
}

struct JobOfferCollection {
	JobOffer* items;
	int count;
	int length;
};

void addJOtoJOC(JobOffer& jo, JobOfferCollection& joc) {
	if (joc.count == joc.length) {
		JobOffer* temp = new JobOffer[joc.length <<= 1];
		for (int i = 0; i < joc.count; i++) temp[i] = joc.items[i];
		delete[] joc.items;
		joc.items = temp;
	}

	joc.items[joc.count++] = jo;
}

void cinJobOffer(JobOffer& jo) {
	char buffer[MAX_COMPANY_NAME_SIZE];
	std::cin.ignore();
	std::cin.getline(buffer, MAX_COMPANY_NAME_SIZE);
	strcpy(jo.CompanyName, buffer);

	std::cin >> jo.ProgrammersInTeam >> jo.PayedTimeOff >> jo.SalaryAfterFinishedProduct;
}

int saveToFile(JobOfferCollection& joc, const char* fileName) {
	std::ofstream outFile(fileName, std::ios::binary | std::ios::app);
	if (!outFile.is_open())
		return -1;

	for (int i = 0; i < joc.length; i++) {
		outFile.write(joc.items[i].CompanyName, MAX_COMPANY_NAME_SIZE);
		outFile.write((char*)&joc.items[i].ProgrammersInTeam, sizeof(joc.items[i].ProgrammersInTeam));
		outFile.write((char*)&joc.items[i].PayedTimeOff, sizeof(joc.items[i].PayedTimeOff));
		outFile.write((char*)&joc.items[i].SalaryAfterFinishedProduct, sizeof(joc.items[i].SalaryAfterFinishedProduct));
	}

	outFile.close();

	return 1;
}

JobOfferCollection readFromFile(const char* fileName) {
	std::ifstream inFile(fileName, std::ios::binary);
	if (!inFile.is_open())
		return {};

	JobOfferCollection joc = { new JobOffer[1], 0, 1 };
	while (!inFile.eof()) {
		JobOffer newJo = {};
		inFile.read(newJo.CompanyName, MAX_COMPANY_NAME_SIZE);
		inFile.read((char*)&newJo.ProgrammersInTeam, sizeof(newJo.ProgrammersInTeam));
		inFile.read((char*)&newJo.PayedTimeOff, sizeof(newJo.PayedTimeOff));
		inFile.read((char*)&newJo.SalaryAfterFinishedProduct, sizeof(newJo.SalaryAfterFinishedProduct));
		addJOtoJOC(newJo, joc);
	}

	inFile.close();

	return joc;
}

void filterOffers(const char* filePath, long long minSalary) {
	JobOfferCollection readJoc = readFromFile(filePath);

	for (int i = 0; i < readJoc.count; i++)
		if (readJoc.items[i].SalaryAfterFinishedProduct >= minSalary)
			coutJobOffer(readJoc.items[i]);

	delete[] readJoc.items;
}

bool existOffer(const char* filePath, const char* name) {
	bool toRet = false;
	JobOfferCollection readJoc = readFromFile(filePath);

	for (int i = 0; i < readJoc.count; i++) {
		if (!strcmp(readJoc.items[i].CompanyName, name)) {
			toRet = true;
			break;
		}
	}

	delete[] readJoc.items;
	return toRet;
}

void taskSaveItems() {
	int n;
	std::cin >> n;

	JobOfferCollection joc = { new JobOffer[n], n, n };

	for (int i = 0; i < joc.length; i++)
		cinJobOffer(joc.items[i]);

	saveToFile(joc, "shefove.joc");

	delete[] joc.items;
}

void taskSalary() {
	int minSalary;
	std::cin >> minSalary;

	filterOffers("shefove.joc", minSalary);
}

void taskExistOffer() {
	char buff[LINE_BUFFER_SIZE];
	std::cin.getline(buff, LINE_BUFFER_SIZE);
	std::cout << existOffer("shefove.joc", buff) << std::endl;
}

int main() {
}