-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPurchase.cpp
More file actions
98 lines (90 loc) · 2.37 KB
/
Copy pathPurchase.cpp
File metadata and controls
98 lines (90 loc) · 2.37 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
#include "Purchase.h"
#include "User.h"
#include "Book.h"
#include <string.h>
#include <vector>
#include <ctime>
#include <fstream>
using namespace std;
void Purchase::checkout(User user,ShoppingCart shopCart)
{
vector<Book> cart;
cart=shopCart.getCart();
strcpy(userName,user.getName());
strcpy(userAddrees,user.getAddress());
totalPrice=getTotalPrice(shopCart);
if(totalPrice == 0)
{
cout<<"\nYOUR SHOPPING CART IS EMPTY !"<<endl;
return;
}
time_t now = time(0);
char* datte = ctime(&now);
string sDate=datte;
strcpy(date, sDate.c_str());
cout<<"\nYour Purchases\n"<<endl;
for(int i=0; i<cart.size(); i++)
{
cout<<"\nBook Name : "<<cart[i].getTitle()<<"\t\t"<<"Price : "<<cart[i].getPrice()<<endl;
}
cout<<"\n\nTotalPrice Is : "<<totalPrice<<endl;
cout<<"\nDate Is :"<<date<<endl;
addPurchaseToPurchaseHistory();
cart.clear();
}
double Purchase::getTotalPrice(ShoppingCart shopCart)
{
totalPrice=0;
vector<Book> cart;
cart=shopCart.getCart();
for(int i=0; i<cart.size(); i++)
{
totalPrice+=cart[i].getPrice();
}
return totalPrice;
}
void Purchase::addPurchaseToPurchaseHistory()
{
Purchase purchase;
purchase=*this;
fstream purchaseFile;
purchaseFile.open("purchase.txt",ios::out|ios::app);
if(!purchaseFile)
{
cerr << "Error: file purchase could not be opened" << endl;
}
purchaseFile.write((char*)&purchase, sizeof(purchase));
if(!purchaseFile)
{
cerr << "Could not write to file\n";
}
purchaseFile.close();
}
void Purchase::displayEntirePurchaseHistory()
{
Purchase purchase;
fstream purchaseFile;
purchaseFile.open("purchase.txt",ios::in);
if(!purchaseFile)
{
cerr << "Error: file could not be opened" << endl;
}
int flag=0;
while(purchaseFile.read((char*)&purchase, sizeof(purchase)))
{
flag=1;
purchase.displayPurchaseDetails();
}
if(flag == 0)
cout<<"\nYOUR SHOPPING CART IS EMPTY!"<<endl;
purchaseFile.close();
}
void Purchase::displayPurchaseDetails()
{
cout<<"=======================\n";
cout<<"At Date : "<<date<<endl;
cout<<"User Name : "<<userName<<endl;
cout<<" His Address : "<<userAddrees<<endl;
cout<<"Total Price Of his Cart : "<<totalPrice<<endl;
cout<<"=======================\n";
}