This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
277 lines (256 loc) · 8.28 KB
/
main.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* BUGS
* 1. Add record suddenly NOT WORKING! (after 'open' table, but ok when just created)
* 2. Display - It crashes sometimes, sometimes not (but always works with empty table)
* 3.
*
* TODO
* 1. Add MAIN_MENU_LARGE_TEXT, CREATE_LARGE_TEXT
*
* NOTE
* 1. Display working for 'school_report.table', 'library.table'
*/
// Project by Sohang Chopra and Aman Gill
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype> // toupper()
#include <cstdlib> // exit()
#include <conio.h>
#include "DataTable.hpp"
#include "large_text.hpp"
using namespace std;
void clrscr() {
system("cls");
}
void mainMenu(DataTable& table);
void submenuCreateTable(DataTable& table);
void inputCreateEmptyTable(DataTable& table);
void submenuCreateTableFromTemplate(DataTable& table);
void submenuTableOperations(DataTable& table);
char inputOption();
void pause();
bool confirm(String message);
bool inputFileSearch(DataTable& table, String action); // @return whether record found or not
int main() {
DataTable table;
while (true) {
if (table.isValid()) {
submenuTableOperations(table);
} else {
mainMenu(table);
}
}
return 0;
}
void mainMenu(DataTable& table) {
clrscr();
cout << "======================== DATA TABLE MANAGEMENT SYSTEM ==========================";
cout << "\nMain Menu:"
<< "\nC - Create Table"
<< "\nO - Open Table"
<< "\nD - Delete Table"
<< "\nE - Exit"
<< PROMPT;
char opt = inputOption();
if (opt == '\0') { // error
pause();
return;
}
if (opt == 'E') {
clrscr();
cout << THANK_YOU_LARGE_TEXT;
pause();
exit(SUCCESS);
}
String fname;
if (opt == 'C') {
DataTable::inputFileName("Enter table name", fname);
table.create(fname);
printStatus(table);
if (table.isValid()) {
pause();
submenuCreateTable(table);
}
} else if (opt == 'O') {
DataTable::inputFileName("Enter table name", fname);
table.open(fname);
printStatus(table);
} else if (opt == 'D') {
DataTable::inputFileName("Enter table name", fname);
if (!fileExists(fname)) {
cerr << ERR_MSG << "File '" << fname << "' does not exist";
} else if (confirm("Are you sure you want to permanently delete this table? ")) {
if (remove(fname) == SUCCESS) {
cout << "Table '" << fname << "' successfully deleted";
} else {
cerr << ERR_MSG << "Table '" << fname << "' could not be deleted";
}
}
} else {
cerr << ERR_MSG << "Invalid Option '" << opt << "'";
return;
}
pause();
}
void submenuTableOperations(DataTable& table) {
clrscr();
cout << MENU_LARGE_TEXT << "\nOpen Table - '" << table.getFileName() << "'\n";
cout << "\nTable Submenu:"
<< "\nA - Add Record"
<< "\nD - Display Table"
<< "\nF - Find Record"
<< "\nM - Modify Record"
<< "\nR - Remove Record"
<< "\nC - Close Table"
<< PROMPT;
char opt = inputOption();
if (opt == '\0') { // error
pause();
return;
}
if (opt == 'A') {
table.inputRecord();
} else if (opt == 'C') {
table.close();
} else if (opt == 'D') {
table.display();
} else if (opt == 'F') {
if (inputFileSearch(table, "search"))
table.printCurrentRecord();
} else if (opt == 'M') {
if (inputFileSearch(table, "modify"))
table.modifyRecord();
} else if (opt == 'R') {
if (inputFileSearch(table, "remove"))
table.deleteRecord();
} else {
cerr << ERR_MSG << "Invalid Option '" << opt << "'";
}
pause();
}
void submenuCreateTable(DataTable& table) {
clrscr();
bool correctOptionEntered;
while (!correctOptionEntered) {
correctOptionEntered = true;
cout << "\nCreate Submenu:"
<< "\nE - Empty File"
<< "\nT - Template"
<< PROMPT;
char opt = inputOption();
if (opt == '\0') {} // error
else if (opt == 'E') {
inputCreateEmptyTable(table);
} else if (opt == 'T') {
submenuCreateTableFromTemplate(table);
} else {
cerr << ERR_MSG << "Invalid Option '" << opt << "'";
correctOptionEntered = false;
}
}
}
void inputCreateEmptyTable(DataTable& table) {
int cols;
while (true) {
cout << "Enter number of columns (max " << MAX_COLS << ")" << PROMPT;
cin >> cols;
if (!cin) { /* TODO: Prevent multiple inputs (with whitespace) */
String str;
cin.clear(); // clear fail bit
cin >> str; // extract last input
cerr << ERR_MSG << "Not a number ('" << str << "')\n";
} else if (cols <= 0) {
cerr << ERR_MSG << "Number of columns cannot be 0 or negative ('" << cols << "')\n";
} else if (cols > MAX_COLS) {
cerr << ERR_MSG << "Number of columns exceeds maximum limit ('" << cols << "')\n";
} else {
break;
}
}
cin.ignore(1000, '\n');
table.setNumCols(cols);
table.inputHeader();
}
void submenuCreateTableFromTemplate(DataTable& table) {
clrscr();
// Table Template Definitions
static String report_card_template_headings[] = {"Roll No.", "Name", "Marks", "Percentage"};
static int report_card_template_cols = 4;
static String library_form_template_headings[] = {"Book No.", "Name", "Author", "Price", "Issued On"};
static int library_form_template_cols = 5;
bool valid_input;
while (!valid_input) { // terminates when correct input is provided
valid_input = true;
clrscr();
cout << TEMPLATE_LARGE_TEXT;
cout << "\nTemplate Submenu:"
<< "\nS - School Report Card"
<< "\nL - Library Form"
<< PROMPT;
char opt = inputOption();
if (opt == '\0') {} // error
if (opt == 'S') {
table.setNumCols(report_card_template_cols);
table.addHeader(report_card_template_headings);
} else if (opt == 'L') {
table.setNumCols(library_form_template_cols);
table.addHeader(library_form_template_headings);
} else {
cerr << ERR_MSG << "Invalid Option '" << opt << "'";
valid_input = false;
pause();
}
}
}
char inputOption() { // returns '\0' on error (when more than one character is entered)
String word;
cin >> word;
cin.ignore(1000, '\n');
if (word[1] != '\0' && !isspace(word[1])) {
cerr << ERR_MSG << "Invalid Option '" << word << "'\n";
return '\0';
}
return toupper(word[0]);
}
void pause() {
cout << "\nPress any key to continue...";
getch();
}
bool confirm(String message) {
while (true) {
char opt;
cout << message << "(y|n)" << PROMPT;
cin >> opt;
cin.ignore(1000, '\n');
if (opt == 'y' || opt == 'Y')
return true;
else if (opt == 'n' || opt == 'N')
return false;
cerr << ERR_MSG << "Invalid Option '" << opt << "'\n";
}
}
bool inputFileSearch(DataTable& table, String action) {
int field_no;
String value;
while (true) {
cout << "Enter field name " << PROMPT;
String field_name;
input(field_name);
for (field_no = 0; field_no < table.getNumCols(); ++field_no) {
if (strcmpi(field_name, table.getFieldName(field_no)) == 0)
break;
}
if (field_no < table.getNumCols())
break;
cerr << ERR_MSG << "No field with name '" << field_name << "'\n";
}
cout << "Enter value to " << action << " in field '" << table.getFieldName(field_no) << "'" << PROMPT;
input(value);
if (table.searchRecord(value, field_no)) {
cout << "Record Found...\n";
return true;
} else {
cout << "Record Not Found\n";
return false;
}
}