-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
89 lines (77 loc) · 2.54 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
#include "class/Customer.h"
#include "class/Product.h"
#include <fstream>
#include <iostream>
using namespace std;
void importProducts(ifstream &fin, Product productList[]);
int searchID(Product productList[], int productID);
int main(__attribute__((unused)) int argc,
__attribute__((unused)) char **argv) {
Product productList[5]{};
Customer customer;
int i, productID, findProductID;
//file check, import function execution
ifstream inFile("../products.txt");
if (!inFile)
cout << "No file found!" << endl;
else
importProducts(inFile, productList);
customer.readData(); //entry of customer data
do {
//display of the list header
cout << "\nList of Available Products" << endl;
cout << "------------------------------" << endl;
cout << "ID | ";
cout.width(17);
cout << left << "Description";
cout << "| Price" << endl;
cout << "------------------------------" << endl;
//display of the available products
for (i = 0; i < 5; i++)
productList[i].printData();
cout << "------------------------------" << endl;
cout << "Provide a product ID to add in the cart(press 0 for checkout): ";
cin >> productID;
findProductID = searchID(
productList, productID); // check the list for the provided product ID
//if the product ID exists, adds the product to the cart
if (findProductID >= 0) {
customer.purchaseProduct(productList[findProductID]);
cout << "...Product added in cart..." << endl;
} else if (productID != 0)
cout << "...This product ID does not exist!" << endl;
} while (productID != 0);
cout << "...checkout!\n" << endl;
customer.printData(); // displays customer data
cout << "Thank you for your order! We will be happy to serve you again!" << endl;
return 0;
}
/*
* Imports the product list from a file
*/
void importProducts(ifstream &fin, Product productList[]) {
int i = 0, id;
char description[20];
float price;
fin >> id;
while (!fin.eof()) {
fin.get(description, 19);
fin >> price;
productList[i].setData(id, description, price);
fin >> id;
i++;
}
}
/*
* search if the product ID exists else return -1
*/
int searchID(Product productList[], int productID) {
int i = 0;
while (i < 5) {
if (productID == productList[i].getProductID()) {
return i;
} else
i++;
}
return -1;
}