-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcarttable.cpp
More file actions
76 lines (63 loc) · 2.28 KB
/
carttable.cpp
File metadata and controls
76 lines (63 loc) · 2.28 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
#include "carttable.h"
#include "PetDatabaseSortableByPrice.h"
#include "Dog.h"
#include "nonstackbasedsumvisitor.h"
#include "BubbleSortIncreasing.h"
#include <QTableWidgetItem>
#include <fstream>
#include <sstream>
void cartTable::AddtoTable(std::vector<QString> itemVector){
int row_number = rowCount();
insertRow(rowCount());
setItem(row_number, 0, new QTableWidgetItem(itemVector[1]));
if (itemVector[0] == "Pet"){
setItem(row_number, 1, new QTableWidgetItem(itemVector[3])); // if pet
}
else{
setItem(row_number, 1, new QTableWidgetItem(itemVector[2])); // if bundle
}
}
void cartTable::checkout(){
PetDatabaseSortableByPrice* database = new PetDatabaseSortableByPrice();
// cart table
int row_number = rowCount();
for (int i = 0; i < row_number; i++){
std::string name = (item(i,0)->text()).toStdString();
double price = (item(i,1)->text()).toDouble();
Dog* pet = new Dog(name, "", price, 0, "");
database->AddPet(pet);
}
// delete old entries
setRowCount(0);
// sorting
BubbleSortIncreasing bsi;
bsi.sort(database);
// insertback
int new_row = 0;
for (int i = 0; i < database->getSize(); i++){
new_row = rowCount();
insertRow(new_row);
setItem(new_row, 0, new QTableWidgetItem(QString::fromStdString(database->getPet(i)->GetName())));
setItem(new_row, 1, new QTableWidgetItem(QString::number(database->getPet(i)->GetPrice())));
}
// writing to outputfile
ofstream outfile;
outfile.open("Checkout.csv");
for (int i = 0; i < database->getSize(); i++){
string name = database->getPet(i)->GetName();
string price = to_string(database->getPet(i)->GetPrice());
outfile << name + "," + price + "\n";
}
outfile.close();
// giving final price to the label
NonStackBasedSumVisitor nsbsv;
database->Accept(&nsbsv);
double totalprice = nsbsv.getResult();
std::ostringstream priceConvert;
priceConvert << std::setprecision(6) << totalprice;
std::string finalPrice = "Total: $"+priceConvert.str();
QString qPrice = QString::fromStdString(finalPrice);
emit changeLabel(qPrice);
parentWidget()->setEnabled(false);
parentWidget()->parentWidget()->setEnabled(false);
}