In: Computer Science
Online Store Project Assignment Four
Introduction
Project Four introduces the ability to read and write data to files. This project will create an online store application circa 1990. The application you will build is a standard C++ console project (visual studios). The store will contain basic operations to shop for items, check out of the store, calculate taxes and totals.
All online store applications are “data driven” in that the contents of the store changes over time. In addition, shipping rates change over time. So this project will read shipping rates and tax rates in from a file. It assumes that these rates are based upon the zipcode provided by the user. The sample data for this file is located here:
90001 0.09000 1.55 1.65 1.72
90002 0.09000 1.55 1.65 1.72
90003 0.09000 1.55 1.65 1.72
9000411 0.09000 1.55 1.65 1.72
90005 0.09000 1.55 1.65 1.72
90006 9 1.55 1.65 1.72
90007 0.09000 1.55 1.65 1.72
90008 0.09000 1.55 1.65 1.72
90009 0.09000 1.55 1.65 -1.72
90010 0.09000 1.55 1.65 1.72
90011 0.09000 -1.55 1.65 1.72
90012 0.09000 1.55 1.65 1.72
90013AB 0.09000 1.55 1.65 1.72
90014 0.09000 1.55 1.65 1.72
90015 0.09000 1.55 1.65 1.72
90016 0.09000 1.55 1.65 1.72
90017 0.09000 1.55 1.65 1.72
90018 0.09000 1.55 1.65 1.72
90019 0.09000 1.55 1.65 1.72
90020 0.09000 1.55 1.65 1.72
90021 0.09000 1.55 1.65 1.72
90022 0.09000 1.55 1.65 1.72
90023 0.09000 1.55 1.65 1.72
90024 0.09000 1.55 1.65 1.72
90025 0.09000 1.55 1.65 1.72
90026 0.09000 1.55 1.65 1.72
90027 0.09000 1.55 1.65 1.72
90028 0.09000 1.55 1.65 1.72
90029 0.09000 1.55 1.65 1.72
90030 0.09000 1.55 1.65 1.72
90031 0.09000 1.55 1.65 1.72
90032 0.09000 1.55 1.65 1.72
90033 0.09000 1.55 1.65 1.72
90034 0.09000 1.55 1.65 1.72
90035 0.09000 1.55 1.65 1.72
90036 0.09000 1.55 1.65 1.72
90037 0.09000 1.55 1.65 1.72
90038 0.09000 1.55 1.65 1.72
90039 0.09000 1.55 1.65 1.72
90040 0.09000 1.55 1.65 1.72
90041 0.09000 1.55 1.65 1.72
90042 0.09000 1.55 1.65 1.72
90043 0.09000 1.55 1.65 1.72
90044 0.09000 1.55 1.65 1.72
90045 0.09000 1.55 1.65 1.72
90046 0.09000 1.55 1.65 1.72
90047 0.09000 1.55 1.65 1.72
90048 0.09000 1.55 1.65 1.72
90049 0.09000 1.55 1.65 1.72
90050 0.09000 1.55 1.65 1.72
90051 0.09000 1.55 1.65 1.72
90052 0.09000 1.55 1.65 1.72
90053 0.09000 1.55 1.65 1.72
90054 0.09000 1.55 1.65 1.72
When a consumer checks out of the store with their shopping cart, most eCommerce applications will email an invoice to the customer. Unfortunately, emailing receipts to customers is beyond the scope of this class! So you will need to write the invoice to a file. The format is described below. You will need to upload a sample output file along with the .cpp file.
Steps and Requirements
Follow the steps below to create your program.
Weight |
Modifier |
1-10 |
1x |
11-50 |
5x |
> 50 |
10x |
For example, total weight = 50 ounces shipped via USPS to 90001, results in amount = 5 * 1.55 = $7.55
After calling the above functions, the checkOut() routine should display the subtotal, tax, and total amounts to the screen. It should save the invoice or receipt to a file. You should print out the customer name at the top. Follow this with the item descriptions, price, quantity and extended price. Then print the subtotal, tax, shipping, and grand total (on separate lines of the file). Name the file “invoice.txt”.
#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
// tax with corresponding data
class TAXRATE {
//Zipcode taxrate USPSrate UPSrate FedexRate
private:
int zipcode;
float taxrate;
float USPSrate;
float USPrate;
float FedexRate;
public:
// default constructor
TAXRATE() {}
void setZipCode(int i) { zipcode = i; }
void setTaxRate(float f) { taxrate = f; }
void setUSPSRate(float f) { USPSrate = f; }
void setUPSRate(float f) { USPrate = f; }
void setFedexRate(float f) { FedexRate = f; }
int getZipCode() { return(zipcode); }
float getTaxRate() { return(taxrate); }
float getUSPSRate() { return(USPSrate); }
float getUPSRate() { return(USPrate); }
float getFedexRate() { return(FedexRate); }
};
vector<TAXRATE> taxList;// contail all tax list
struct CART {
string Prd;
string dec;
float Price;
float Qnty;
int weight;
};
// load from file
void loadFromFile(string s) {
ifstream out(s.c_str());
int a;
float b, c, d, e;
bool type = false;
string tmp;
while (!out.eof()) {
type = false;
out >> tmp;
a = stoi(tmp);// string to
int
if (a < 10000) { type = true;
}
out >> b;
if (b > .1 || b<0) { type =
true; }
out >> c;
if (c <0) { type = true; }
out >> d;
cout << d << " |
";
if (d <0) { type = true; }
out >> e;
if (e <0) { type = true; }
// if any error skip to
load
// type = true mean error
if (type == false) {
TAXRATE
tr;
tr.setZipCode(a);
tr.setTaxRate(b);
tr.setUSPSRate(c);
tr.setUPSRate(d);
tr.setFedexRate(e);
taxList.push_back(tr);
}
}
out.close();
}
// memu list
int displayMenu() {
int ret = 0;
while (true) {
system("cls");
cout << "Hot Tub Time Machine
Parts and Supplies";
cout << "\n 1) Create
Customer Account";
cout << "\n 2) Shop for
Items";
cout << "\n 3) Proceed to
Check Out";
cout << "\n 4) Exit
Store";
cout << "\n User Choice :
";
int i;
cin >> i;
if (i >= 1 && i <= 4)
{
ret = i;
break;
}
}
return(ret);
}
// customer name
void createCustomer(string &a,string &b,string
&c,int &d) {
cout << "\nName : "; cin >> a;
cout << "\nAddress : "; cin >> b;
cout << "\nCity : "; cin >> c;
cout << "\nZIPCODE : "; cin >> d;
}
void maintainCart(CART *carts) {
// cart list display for choose
while (true) {
cout << "\n 1)
"<<carts[0].Prd<<" {$" << carts[0].Price <<
"}";
cout << "\n 2) " <<
carts[1].Prd << " {$" << carts[1].Price <<
"}";
cout << "\n 3) " <<
carts[2].Prd << " {$" << carts[2].Price <<
"}";
cout << "\n 4) " <<
carts[3].Prd << " {$" << carts[3].Price <<
"}";
cout << "\n 5) " <<
carts[4].Prd << " {$" << carts[4].Price <<
"}";
cout << "\n 6) " <<
carts[5].Prd << " {$" << carts[5].Price <<
"}";
cout << "\n Your choice :
";
int i;
cin >> i;
if (i >= 1 && i <= 6)
{
cout <<
"\nEnter qualtity : ";
float qty;
cin >>
qty;
// add
quantiti
carts[i-1].Qnty
= qty;
break;
}
}
}
float priceAndQuantity(float a,float b) {
return(a*b);
}
// calculate shipping
float calculateShipping(int zip, CART *a) {
float ret = 0;
while (true) {
cout << "\n Choose shipping
method";
cout << "\n 1) USPS";
cout << "\n 2) UPS";
cout << "\n 3) FEDES";
cout << "\n User choice
:";
int i;
cin >> i;
if (i == 1) {
for (int a = 0;
a < taxList.size(); a++) {
if (taxList.at(a).getZipCode() == zip) {
ret =
taxList.at(a).getUPSRate();
break;
}
}
break;
}
if (i == 2) {
for (int a = 0;
a < taxList.size(); a++) {
if (taxList.at(a).getZipCode() == zip) {
ret =
taxList.at(a).getUSPSRate();
break;
}
}
break;
}
if (i == 3) {
for (int a = 0;
a < taxList.size(); a++) {
if (taxList.at(a).getZipCode() == zip) {
ret =
taxList.at(a).getFedexRate();
break;
}
}
break;
}
}
int sum = 0;
for (int x = 0; x < 6; x++) {
sum += a[x].weight;
}
if (sum < 10){ret = ret;}
if (sum >10 && sum<=50) { ret = 5*ret;
}
if (sum > 50) { ret = 10*ret; }
return(ret*sum);
}
// calculate tax
float calculateTax(int zip,int subtotal) {
float ret = 0;
for (int a = 0; a < taxList.size(); a++) {
if (taxList.at(a).getZipCode() ==
zip) {
ret =
taxList.at(a).getTaxRate();
break;
}
}
return(ret*subtotal);
}
// calculate total
float calculateTotal(float a,float b,float c) {
return(a+b+c);
}
// receipt
void displayReceipt(int zip,CART *a,float &x,float &y,float
&z) {
float sum = 0;
for (int i = 0; i < 6; i++) {
if (a[i].Qnty != 0) {
cout <<
"\n" << a[i].Prd << " [ unit price : $" <<
a[i].Price << " , Quantity : " << a[i].Qnty << "
] $" << priceAndQuantity(a[i].Price, a[i].Qnty);
sum +=
priceAndQuantity(a[i].Price, a[i].Qnty);
}
}
float czip = calculateShipping(zip,a);
cout << "\nShipping Cost : $" <<
czip;
float tzip = calculateTax(zip, sum);
cout << "\nTax : $" << tzip;
cout << "\nTotal Cost : $" <<
calculateTotal(sum, tzip, czip);
x = sum;
y = czip;
z = tzip;
}
// check out
void checkOut(int zipcode, CART *a) {
if (zipcode == 0) {
while (true) {
cout <<
"\nEnter zipcode : ";
int zip;
cin >>
zip;
if (zip >=
10000) {
break;
}
}
}
float x, y, z;
displayReceipt(zipcode,a,x,y,z);
ofstream out("invoice.txt");
out << x;
out << endl;
out << y;
out << endl;
out << z;
out << endl;
out << calculateTotal(x,y,z);
out.close();
}
int main() {
string Customer_name;
string address;
string city;
int zipcode;
CART carts[6];
// each cart details
carts[0].Prd = "Product A";
carts[1].Prd = "Product B";
carts[2].Prd = "Product C";
carts[3].Prd = "Product D";
carts[4].Prd = "Product E";
carts[5].Prd = "Product F";
carts[0].dec = "Description A";
carts[1].dec = "Description B";
carts[2].dec = "Description C";
carts[3].dec = "Description D";
carts[4].dec = "Description E";
carts[5].dec = "Description F";
carts[0].Price = 345.50;
carts[1].Price = 200.50;
carts[2].Price = 634.30;
carts[3].Price = 123.00;
carts[4].Price = 698.60;
carts[5].Price = 583.80;
carts[0].Qnty = 0;
carts[1].Qnty = 0;
carts[2].Qnty = 0;
carts[3].Qnty = 0;
carts[4].Qnty = 0;
carts[5].Qnty = 0;
carts[0].weight = 4;
carts[1].weight = 5;
carts[2].weight = 3;
carts[3].weight = 1;
carts[4].weight = 4;
carts[5].weight = 1;
cout << "Enter file name : ";
string file;
cin >> file;
loadFromFile(file);
cout << "\nData loaded";
for (;;) {
int ret = displayMenu();
if(ret==1){
createCustomer(Customer_name, address, city, zipcode);
}
if(ret==2){
maintainCart(carts);
}
if(ret==3){
checkOut(zipcode, carts);
break;
}
if (ret == 4) {
break;
}
}
cout << endl;
system("pause");
return(0);
}