-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.cpp
More file actions
173 lines (160 loc) · 5.27 KB
/
Copy path8.cpp
File metadata and controls
173 lines (160 loc) · 5.27 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class Customer {
public :
char Name[20] ;
int Age ;
char Category[20] ;
char Product[20] ;
float Price ;
public :
void GetCustomerInfo( char StrMode[ 50 ] ) ;
} ;
class FileHandler {
public :
FILE *fp ;
char FilePath[ 100 ] ;
Customer C[ 500 ] ;
int MaxCustomer ;
public :
FileHandler( char FilePath[ 100 ] ) ;
void GetFilePath() ;
void ReadFile() ;
void SelectMode() ;
} ;
int main() {
FileHandler *F = new FileHandler( "output/customer.csv" ) ;
while( 1 ) F->SelectMode() ;
return 0;
}
void FileHandler :: SelectMode() {
int Mode = 0 ;
printf( "Select Mode : " ) ;
scanf( "%d", &Mode ) ;
if( Mode == 0 ) { //Exit
printf("Thank you!.\n");
exit(0);
} else if( Mode == 1 ) { //Who bought higest price
float maxPrice = 0;
int index = -1;
for (int i = 1; i <= this->MaxCustomer; i++) {
if (this->C[i].Price > maxPrice) {
maxPrice = this->C[i].Price;
index = i;
}
}
if (index != -1) {
this->C[index].GetCustomerInfo("Who bought highest price");
} else {
printf("No data found.\n");
}
} else if( Mode == 2 ) { //Lines of file
printf( "File data = %d records.\n", this->MaxCustomer) ;
} else if( Mode == 3 ) { //Average Price
float totalPrice = 0;
for (int i = 0; i < this->MaxCustomer; i++) {
totalPrice += this->C[i].Price;
}
float averagePrice = totalPrice / this->MaxCustomer;
printf("Average price = %.2f\n", averagePrice);
} else if( Mode == 4 ) { //Count People who age above average.
int totalAge = 0;
for (int i = 0; i < this->MaxCustomer; i++) {
totalAge += this->C[i].Age;
}
float averageAge = (float)totalAge / this->MaxCustomer;
int count = 0;
for (int i = 0; i < this->MaxCustomer; i++) {
if (this->C[i].Age > averageAge) {
count++;
}
}
printf("Average age = %.2f.\n", averageAge);
printf("People who aged above average = %d.\n", count);
} else if( Mode == 5 ) { //Most Popular Product
int maxCount = 0;
char popularProduct[20] = "";
for (int i = 0; i < this->MaxCustomer; i++) {
int count = 0;
for (int j = 0; j < this->MaxCustomer; j++) {
if (strcmp(this->C[i].Product, this->C[j].Product) == 0) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
strcpy(popularProduct, this->C[i].Product);
}
}
printf("Most popular product = %s (sold %d times).\n", popularProduct, maxCount);
} else if( Mode == 6 ) { //Least Popular Product
int minCount = this->MaxCustomer;
char leastPopularCategory[20] = "";
for (int i = 0; i < this->MaxCustomer; i++) {
int count = 0;
for (int j = 0; j < this->MaxCustomer; j++) {
if (strcmp(this->C[i].Category, this->C[j].Category) == 0) {
count++;
}
}
if (count < minCount) {
minCount = count;
strcpy(leastPopularCategory, this->C[i].Category);
}
}
printf("Least popular category = %s (sold %d times).\n", leastPopularCategory, minCount);
} else {
printf("Thank you!.\n");
exit(0);
}
}
FileHandler :: FileHandler( char FilePath[ 100 ] ) {
strcpy( this->FilePath, FilePath ) ;
this->ReadFile() ;
}
void FileHandler :: ReadFile() {
this->fp = fopen(this->FilePath, "r");
if (this->fp == NULL) {
printf("Error opening file.\n");
exit(1);
}
char buffer[1024];
int row = 0;
int column = 0;
this->MaxCustomer = 0;
while (fgets(buffer, 1024, this->fp)) {
column = 0;
row++;
if (row == 0) continue; // Skip header
char *value = strtok(buffer, ",");
while (value) {
if (column == 0) {
strcpy(this->C[this->MaxCustomer].Name, value);
} else if (column == 1) {
this->C[this->MaxCustomer].Age = atoi(value);
} else if (column == 2) {
strcpy(this->C[this->MaxCustomer].Category, value);
} else if (column == 3) {
strcpy(this->C[this->MaxCustomer].Product, value);
} else if (column == 4) {
this->C[this->MaxCustomer].Price = atof(value);
}
value = strtok(NULL, ",");
column++;
}
this->MaxCustomer++;
}
fclose(this->fp);
}
void Customer :: GetCustomerInfo( char StrMode[ 50 ] ) {
printf( "--------| %-8s\n", StrMode ) ;
printf("%-8s : %-10s\n", "Name", this->Name ) ;
printf("%-8s : %-4d\n", "Age", this->Age ) ;
printf("%-8s : %-10s\n", "Category", this->Category ) ;
printf("%-8s : %-10s\n", "Product", this->Product ) ;
printf("%-8s : %-5.2f\n", "Price", this->Price ) ;
}
void FileHandler :: GetFilePath() {
printf("File Path: %s\n", this->FilePath);
}