| 124 | } |
| 125 | |
| 126 | void demonstrateTextTable() |
| 127 | { |
| 128 | std::cout << "\n=== Text Table Demo ===\n"; |
| 129 | |
| 130 | // Create sample employee data |
| 131 | std::vector<Employee> employees = { |
| 132 | {"Alice Johnson", "Engineering", 28, 75000.0, "alice@company.com"}, |
| 133 | {"Bob Smith", "Marketing", 35, 65000.0, "bob@company.com"}, |
| 134 | {"Carol Williams", "Engineering", 31, 82000.0, "carol@company.com"}, |
| 135 | {"David Brown", "Sales", 29, 58000.0, "david@company.com"}, |
| 136 | {"Eve Davis", "HR", 42, 70000.0, "eve@company.com"} |
| 137 | }; |
| 138 | |
| 139 | choc::text::TextTable table; |
| 140 | |
| 141 | // Add header row |
| 142 | table << "Name" << "Department" << "Age" << "Salary" << "Email"; |
| 143 | table.newRow(); |
| 144 | |
| 145 | // Add data rows |
| 146 | for (const auto& emp : employees) |
| 147 | { |
| 148 | table << emp.name |
| 149 | << emp.department |
| 150 | << std::to_string (emp.age) |
| 151 | << ("$" + choc::text::floatToString (emp.salary, 0)) |
| 152 | << emp.email; |
| 153 | |
| 154 | table.newRow(); |
| 155 | } |
| 156 | |
| 157 | std::cout << "Employee Table:\n"; |
| 158 | std::cout << table.toString ("| ", " | ", " |\n"); |
| 159 | |
| 160 | // Create a summary table |
| 161 | choc::text::TextTable summary; |
| 162 | std::map<std::string, std::vector<double>> deptSalaries; |
| 163 | |
| 164 | // Group salaries by department |
| 165 | for (const auto& emp : employees) |
| 166 | deptSalaries[emp.department].push_back (emp.salary); |
| 167 | |
| 168 | summary << "Department" << "Employees" << "Avg Salary" << "Total Salary"; |
| 169 | summary.newRow(); |
| 170 | |
| 171 | for (const auto& [dept, salaries] : deptSalaries) |
| 172 | { |
| 173 | double total = 0.0; |
| 174 | |
| 175 | for (double salary : salaries) |
| 176 | total += salary; |
| 177 | |
| 178 | double average = total / salaries.size(); |
| 179 | |
| 180 | summary << dept |
| 181 | << std::to_string (salaries.size()) |
| 182 | << ("$" + choc::text::floatToString (average, 0)) |
| 183 | << ("$" + choc::text::floatToString (total, 0)); |