-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stock.c
446 lines (388 loc) · 13.5 KB
/
Stock.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#else
#include <unistd.h>
#include <termios.h>
#endif
#define MAX_USERS 100
#define MAX_TRANSACTIONS 100
#define MAX_STOCKS 10
#define MAX_WATCHLIST 10
typedef struct {
char username[50];
char password[50];
float balance;
} User;
typedef struct {
char symbol[10];
float price;
} Stock;
typedef struct {
char type[10];
char stock[10];
float amount;
float price;
float quantity;
} Transaction;
typedef struct {
char stock[10];
float totalInvested;
float totalEarned;
float totalProfitOrLoss;
} StockPerformance;
typedef struct {
char symbol[10];
} Watchlist;
void registerUser(User users[], int *numUsers);
void authenticate(User users[], int numUsers, User **authenticatedUser);
void displayPortfolio(const User *user);
void buyStock(User *user, const Stock stocks[], int numStocks, Transaction transactions[], int *numTransactions);
void sellStock(User *user, const Stock stocks[], int numStocks, Transaction transactions[], int *numTransactions);
void displayMenu();
void viewStock(const Stock stocks[], int numStocks);
void viewTransactionHistory(const Transaction transactions[], int numTransactions);
void manageWatchlist(Watchlist watchlist[], int *numWatchlist);
void setColor(int color);
void viewPerformanceAnalytics(Transaction transactions[], int numTransactions, const char *stockSymbol);
void getPassword(char *password, int maxLen);
int main() {
User users[MAX_USERS];
int numUsers = 0, numTransactions, numWatchlist;
User *authenticatedUser = NULL;
Transaction transactions[MAX_TRANSACTIONS] = {
{"Buy", "AAPL", 700, 700, 1},
{"Buy", "AAPL", 300, 300, 1},
{"Sell", "AAPL", 1000, 1000, 1}
};
numTransactions = 3;
Watchlist watchlist[MAX_WATCHLIST];
numWatchlist = 0;
Stock stocks[MAX_STOCKS] = {
{"AAPL", 150.0},
{"GOOGL", 2800.0},
{"AMZN", 3500.0},
{"MSFT", 300.0},
{"TSLA", 700.0}
};
int numStocks = 5;
int choice;
do {
printf("\n1. Register\n2. Login\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
registerUser(users, &numUsers);
break;
case 2:
authenticate(users, numUsers, &authenticatedUser);
if (authenticatedUser != NULL) {
int userChoice;
do {
displayMenu();
scanf("%d", &userChoice);
switch (userChoice) {
case 1:
displayPortfolio(authenticatedUser);
break;
case 2:
viewStock(stocks, numStocks);
break;
case 3:
buyStock(authenticatedUser, stocks, numStocks, transactions, &numTransactions);
break;
case 4:
sellStock(authenticatedUser, stocks, numStocks, transactions, &numTransactions);
break;
case 5:
viewTransactionHistory(transactions, numTransactions);
break;
case 6:
manageWatchlist(watchlist, &numWatchlist);
break;
case 7:
{
char stockSymbol[10];
printf("Enter stock symbol to view performance analytics: ");
scanf("%s", stockSymbol);
viewPerformanceAnalytics(transactions, numTransactions, stockSymbol);
break;
}
case 8:
printf("Logging out...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (userChoice != 8);
}
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 3);
return 0;
}
void registerUser(User users[], int *numUsers) {
if (*numUsers >= MAX_USERS) {
printf("User limit reached. Cannot register more users.\n");
return;
}
char username[50];
char password[50];
printf("Enter new username: ");
scanf("%s", username);
printf("Enter new password: ");
getPassword(password, 50);
strcpy(users[*numUsers].username, username);
strcpy(users[*numUsers].password, password);
users[*numUsers].balance = 1000.0;
(*numUsers)++;
printf("\nUser registered successfully!\n");
}
void authenticate(User users[], int numUsers, User **authenticatedUser) {
char username[50];
char password[50];
int i;
printf("Username: ");
scanf("%s", username);
printf("Password: ");
getPassword(password, 50);
for (i = 0; i < numUsers; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
printf("\nAuthentication successful!\n");
*authenticatedUser = &users[i];
return;
}
}
printf("\nAuthentication failed. Invalid username or password.\n");
*authenticatedUser = NULL;
}
void displayPortfolio(const User *user) {
setColor(14);
printf("\n---- Portfolio ----\n");
setColor(7);
printf("Username: %s\n", user->username);
printf("Balance: $%.2f\n", user->balance);
}
void buyStock(User *user, const Stock stocks[], int numStocks, Transaction transactions[], int *numTransactions) {
char symbol[10];
float amount;
int i;
printf("Enter the stock symbol to buy: ");
scanf("%s", symbol);
int stockIndex = -1;
for (i = 0; i < numStocks; i++) {
if (strcmp(stocks[i].symbol, symbol) == 0) {
stockIndex = i;
break;
}
}
if (stockIndex == -1) {
printf("Invalid stock symbol.\n");
return;
}
printf("Enter the amount to invest: ");
scanf("%f", &amount);
if (amount > user->balance) {
printf("Insufficient balance!\n");
return;
}
user->balance -= amount;
float quantity = amount / stocks[stockIndex].price;
if (*numTransactions < MAX_TRANSACTIONS) {
strcpy(transactions[*numTransactions].type, "Buy");
strcpy(transactions[*numTransactions].stock, stocks[stockIndex].symbol);
transactions[*numTransactions].amount = amount;
transactions[*numTransactions].price = stocks[stockIndex].price;
transactions[*numTransactions].quantity = quantity;
(*numTransactions)++;
}
printf("You bought %.2f worth of %s shares.\n", amount, stocks[stockIndex].symbol);
}
void sellStock(User *user, const Stock stocks[], int numStocks, Transaction transactions[], int *numTransactions) {
char symbol[10];
float amount;
int i;
printf("Enter the stock symbol to sell: ");
scanf("%s", symbol);
int stockIndex = -1;
for (i = 0; i < numStocks; i++) {
if (strcmp(stocks[i].symbol, symbol) == 0) {
stockIndex = i;
break;
}
}
if (stockIndex == -1) {
printf("Invalid stock symbol.\n");
return;
}
printf("Enter the amount to sell: ");
scanf("%f", &amount);
user->balance += amount;
float quantity = amount / stocks[stockIndex].price;
if (*numTransactions < MAX_TRANSACTIONS) {
strcpy(transactions[*numTransactions].type, "Sell");
strcpy(transactions[*numTransactions].stock, stocks[stockIndex].symbol);
transactions[*numTransactions].amount = amount;
transactions[*numTransactions].price = stocks[stockIndex].price;
transactions[*numTransactions].quantity = quantity;
(*numTransactions)++;
}
printf("You sold %.2f worth of %s shares.\n", amount, stocks[stockIndex].symbol);
}
void displayMenu() {
setColor(11);
printf("\n---- Menu ----\n");
setColor(7);
printf("1. View Portfolio\n");
printf("2. View Stock\n");
printf("3. Buy Stock\n");
printf("4. Sell Stock\n");
printf("5. View Transaction History\n");
printf("6. Manage Watchlist\n");
printf("7. View Performance Analytics\n");
printf("8. Logout\n");
printf("Enter your choice: ");
}
void viewStock(const Stock stocks[], int numStocks) {
int i;
setColor(10);
printf("\n---- Available Stocks ----\n");
setColor(7);
printf("Symbol\tPrice\n");
for (i = 0; i < numStocks; i++) {
printf("%s\t$%.2f\n", stocks[i].symbol, stocks[i].price);
}
}
void viewTransactionHistory(const Transaction transactions[], int numTransactions) {
int i;
setColor(13);
printf("\n---- Transaction History ----\n");
setColor(7);
printf("Type\tStock\tAmount\tPrice\tQuantity\n");
for (i = 0; i < numTransactions; i++) {
printf("%s\t%s\t$%.2f\t$%.2f\t%.2f\n", transactions[i].type, transactions[i].stock, transactions[i].amount, transactions[i].price, transactions[i].quantity);
}
}
void manageWatchlist(Watchlist watchlist[], int *numWatchlist) {
int i, j;
setColor(12);
printf("\n---- Manage Watchlist ----\n");
setColor(7);
int choice;
do {
printf("1. Add stock to watchlist\n");
printf("2. Remove stock from watchlist\n");
printf("3. View watchlist\n");
printf("4. Back to main menu\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (*numWatchlist < MAX_WATCHLIST) {
printf("Enter stock symbol to add to watchlist: ");
scanf("%s", watchlist[*numWatchlist].symbol);
(*numWatchlist)++;
} else {
printf("Watchlist is full. Cannot add more stocks.\n");
}
break;
case 2:
if (*numWatchlist > 0) {
char symbol[10];
printf("Enter stock symbol to remove from watchlist: ");
scanf("%s", symbol);
int i, found = 0;
for (i = 0; i < *numWatchlist; i++) {
if (strcmp(watchlist[i].symbol, symbol) == 0) {
found = 1;
for (j = i; j < *numWatchlist - 1; j++) {
strcpy(watchlist[j].symbol, watchlist[j + 1].symbol);
}
(*numWatchlist)--;
printf("Stock %s removed from watchlist.\n", symbol);
break;
}
}
if (!found) {
printf("Stock symbol not found in watchlist.\n");
}
} else {
printf("Watchlist is empty. No stocks to remove.\n");
}
break;
case 3:
printf("\n---- Watchlist ----\n");
for (i = 0; i < *numWatchlist; i++) {
printf("%s\n", watchlist[i].symbol);
}
break;
case 4:
printf("Returning to main menu...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
}
void viewPerformanceAnalytics(Transaction transactions[], int numTransactions, const char *stockSymbol) {
float totalInvested = 0.0;
float totalEarned = 0.0;
int i;
for (i = 0; i < numTransactions; i++) {
if (strcmp(transactions[i].stock, stockSymbol) == 0) {
if (strcmp(transactions[i].type, "Buy") == 0) {
totalInvested += transactions[i].amount;
} else if (strcmp(transactions[i].type, "Sell") == 0) {
totalEarned += transactions[i].amount;
}
}
}
float totalProfitOrLoss = totalEarned - totalInvested;
printf("\nStock: %s\n", stockSymbol);
printf("Total Invested: $%.2f\n", totalInvested);
printf("Total Earned: $%.2f\n", totalEarned);
printf("Profit/Loss: $%.2f\n", totalProfitOrLoss);
}
void getPassword(char *password, int maxLen) {
#ifdef _WIN32
int i = 0;
while (1) {
int ch = _getch();
if (ch == '\r' || ch == '\n') {
break;
}
if (ch == '\b' && i > 0) {
printf("\b \b");
i--;
} else if (ch != '\b') {
password[i++] = ch;
printf("*");
}
}
password[i] = '\0';
#else
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
fgets(password, maxLen, stdin);
password[strcspn(password, "\n")] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#endif
}
void setColor(int color) {
#ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
#else
printf("\033[1;%dm", color);
#endif
}