-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingcart.cpp
68 lines (55 loc) · 1.76 KB
/
shoppingcart.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
#include "shoppingcart.h"
#include "ui_shoppingcart.h"
#include "subject.h"
#include "shoppingcartvisitor.h"
#include "BubbleSort.h"
#include "BubbleSortIncreasing.h"
#include <iostream>
#include <fstream>
using std::endl;
using std::cout;
ShoppingCart::ShoppingCart(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ShoppingCart)
{
ui->setupUi(this);
cart_table_visitor.setTable(ui->tableWidget);
}
ShoppingCart::~ShoppingCart()
{
delete ui;
}
void ShoppingCart::on_checkoutButton_clicked()
{
emit Checkout();
BubbleSortIncreasing bsi;
bsi.sort(&shopping_cart);
cart_table_visitor.fillTable(&shopping_cart);
ShoppingCartVisitor shopping_cart_visitor;
this->ui->label->setStyleSheet("font-weight: bold; color: red");
this->ui->label->setText("Total Price: $" + QString::number(shopping_cart_visitor.calculateTotalPrice(&shopping_cart)));
this->setEnabled(false);
ofstream file("Checkout.csv");
if (file.is_open()) {
for (int i = 0; i < shopping_cart.getSize(); i++) {
Item *p = shopping_cart.getItem(i);
file << p->getName() << "," << p->getPrice() << endl;
}
file.close();
}
}
void ShoppingCart::on_deleteButton_clicked()
{
QModelIndexList selectedPets = ui->tableWidget->selectionModel()->selectedRows();
for(int i = 0; i < selectedPets.count(); i++){
int row = selectedPets[i].row();
emit Delete(shopping_cart.getItem(row));
}
}
void ShoppingCart::update(Subject* s){
shopping_cart = SortableItemVector(s->getShoppingCart());
cart_table_visitor.fillTable(&shopping_cart);
}
void ShoppingCart::closeEvent(QCloseEvent *event){
emit observerDestroyed(this);
}