This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2022-2023DSA.cpp
More file actions
149 lines (137 loc) · 3.87 KB
/
2022-2023DSA.cpp
File metadata and controls
149 lines (137 loc) · 3.87 KB
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
#include <iostream>
#include "fstream"
#include "sstream"
#include "vector"
#include "algorithm"
#define FILE_NAME "items.csv"
using namespace std;
struct Item
{
int item_id;
string item_name;
int item_quantity;
string item_registration_date;
};
bool compareAlphabetically(const Item &a, const Item &b)
{
return a.item_name < b.item_name;
}
void listItems()
{
ifstream file(FILE_NAME);
vector<Item> items;
string line;
string item_id, item_name, item_quantity, item_registration_date;
if (file.is_open())
{
while (getline(file, line))
{
// convert to string stream
istringstream ss(line);
// store stream values to corresponding variables
if (ss >> item_id >> item_name >> item_quantity >> item_registration_date)
{
Item itm = {
stoi(item_id),
item_name,
stoi(item_quantity),
item_registration_date};
items.push_back(itm);
}
else
{
cerr << "Invalid record : " << line << endl;
}
}
if (!items.empty())
{
sort(items.begin(), items.end(), compareAlphabetically);
for (auto &item : items)
{
cout << "Item ID: " << item.item_id << "\t" << "Item Name :" << item.item_name << "\t"
<< "Quantity :" << item.item_quantity << "\t" << "Reg Date :"
<< item.item_registration_date << endl;
}
}
else
{
cout << "No items found in " << FILE_NAME << endl;
}
file.close();
}
else
{
cerr << "failed to open the file : " << FILE_NAME << endl;
}
}
void addItem(const Item &itm)
{
ofstream file(FILE_NAME, ios::app);
if (file.is_open())
{
file << itm.item_id << " " << itm.item_name << " " << itm.item_quantity << " " << itm.item_registration_date
<< endl;
cout << "Item saved successfully!" << endl;
file.close();
}
else
{
cerr << "failed to open the file : " << FILE_NAME << endl;
}
}
string convertToLowercase(const string &str)
{
string lowercase_str;
for (auto character : str)
{
lowercase_str.push_back(character);
}
return lowercase_str;
}
int main()
{
string command, user_input, item_id, item_name, item_quantity, item_registration_date;
while (true)
{
cout << "Enter command" << endl;
getline(cin, user_input);
istringstream ss(user_input);
ss >> command;
command = convertToLowercase(command);
if (command == "itemadd")
{
if (ss >> item_id >> item_name >> item_quantity >> item_registration_date)
{
Item item = {
stoi(item_id),
item_name,
stoi(item_quantity),
item_registration_date};
addItem(item);
}
else
{
cerr << "Invalid input format" << endl;
}
}
else if (command == "itemslist")
{
listItems();
}
else if (command == "help")
{
cout << "----------------------" << endl;
cout << "*\t Commands syntaxes \t *" << endl;
cout << "\t itemadd <item_id> <item_name> <quantity> <registration_date>\n \t itemslist" << endl;
}
else if (command == "exit")
{
cout << "Quitting the program..." << endl;
exit(0);
}
else
{
cerr << "Invalid Command" << endl;
}
}
}