题目要求
Check the file DataFinal.txt under Content - > FinalExam2022 that contains the data of 4 products (Product Name, Reference number, original price, Sell tax rate (%) ). Consider an application which is designed to generate a bill for purchasing products. In this application, there will be one class called Product. The Product class should have the following (private) member variables:
-
The product name as a string 2. The product reference number as an integer 3. The product original price as a double 4. The product sell tax rate as a double 5. The product selling price as a double The product class should have the following (public) member functions:
-
Default constructor: The default constructor should set any strings to the empty string (i.e. ""), and any integers and doubles to zero.
-
Overloaded constructor: a constructor that takes the product name, reference number, original price, sell tax rate as arguments. The appropriate member variables must be set to the corresponding values passed into the constructor. Additionally, the selling price should be set to zero in this constructor.
-
Setters and getters for all member variables: You must create setter / getter (mutator and accessor) methods for all member variables.
-
Function CalculateAmountToPay: this function calculates the selling price after tax and update the selling price variable.
-
Function Display: this function displays the content of all member variables.
-
Overloaded operator+ function: this function adds two products and returns a Product object. The resulting product must concatenate the two product names (example: "SmartTV + Oven"), set the reference number and tax rate to 0, update the original price with the sum of the original prices of the two products (before tax) and finally update the selling price with the sum of the selling prices of the two products (after tax). 7. Overloaded Stream operator function: this function displays the content of all member variables (except reference number and sell tax rate) and the total tax amount that the client should pay. 8. SortFunction: takes as input an array of products and its size. This function sorts the selling price from the highest to the lowest and finally display the sorted list of products (Product name + Selling price). In main, Illustrate the use of this class in a main program. This program should read 4 products from a file that has the following columns (Product Name, Reference number, original price, Sell tax rate (%)) Next, implement an array of products (size = 4) and iterate through the array to populate it with values from the file. You should use setters to update the product member variables. In each iteration, you should call the CalculateAmountToPay function and the Display function. Finally, define an object called SumProduct using the default constructor, iterate through the array a second time and in each iteration calculate the Sum of all products using the overloaded operator+ function and display the content of SumProduct using the overloaded stream operator function. The final output should be similar to this Figure Q1-1:
答案
构建头文件 Product.h
通过Set 与Get方法设计使得只有唯一的接口来修改私有变量
#pragma once
#include <string>
#include <ostream>
using namespace std;
class Product
{
private:
string product_name;
int product_referenceNumber;
double product_originalPrice;
double product_sellTaxRate;
double product_sellingPrice;
public:
Product();
Product(string name, int referenceNumber, double originalPrice, double sellTaxRate);
// Getters
string Getname() {return product_name;}
int GetreferenceNumber() {return product_referenceNumber;}
double GetoriginalPrice() {return product_originalPrice;}
double GetsellTaxRate() {return product_sellTaxRate;}
double GetsellingPrice() {return product_sellingPrice;}
// Setters
void Setname(string name) {product_name = name;}
void SetreferenceNumber(int referenceNumber) {product_referenceNumber = referenceNumber;}
void SetoriginalPrice(double originalPrice) {product_originalPrice = originalPrice;}
void SetsellTaxRate(double sellingPrice) {product_sellTaxRate = sellingPrice;}
void SetsellingPrice(double sellingPrice) {product_sellingPrice = sellingPrice;}
void Display();
void CalculateAmountToPay(double oriPrice,double selTaxRlate);
static void SortFunction(vector<Product> *p,int size);
//void Product::SortFunction(vector<Product> &provec,int size);
Product operator+(Product &p1);
};
bool compare(Product &p1,Product &p2);
ostream& operator<<(ostream& s,Product p);
头文件定义实现
#include "Product.h"
#include <iostream>
#include <ostream>
using namespace std;
Product::Product()
{
product_name = "";
product_referenceNumber = 0;
product_originalPrice = 0;
product_sellingPrice = 0;
product_sellTaxRate = 0;
}
Product::Product(string name, int referenceNumber, double originalPrice, double sellTaxRate)
{
product_name = name;
product_referenceNumber = referenceNumber;
product_originalPrice = originalPrice;
product_sellTaxRate = sellTaxRate;
product_sellingPrice = 0;
}
void Product::CalculateAmountToPay(double oriPrice,double selTaxRlate)
{
double price = oriPrice - selTaxRlate;
SetsellingPrice(price);
// cout << "selling price after tax is " << GetsellingPrice() << endl;
}
void Product::Display()
{
cout << "product_name is " << this->Getname()<< endl;
cout << "product_referenceNumber is " << this->GetreferenceNumber()<< endl;
cout << "product_originalPrice is " << this->GetoriginalPrice() << endl;
cout << "product_sellTaxRate is " << this->GetsellTaxRate()<< endl;
cout << " product_sellingPrice is " << this->GetsellingPrice()<< endl;
}
Product Product::operator+(Product &p1)
{
Product p;
//cout << "this name is " << this->Getname().append(p1.Getname()) << endl;
p.Setname(this->Getname().append(p1.Getname()));
p.SetreferenceNumber(0);
p.SetsellTaxRate(0);
p.SetoriginalPrice(this->GetoriginalPrice()+p1.GetoriginalPrice());
p.SetsellingPrice(this->GetsellingPrice()+p1.GetsellingPrice());
p.Display();
return p;
}
bool compare(Product &p1,Product &p2)
{
return (p1.GetsellingPrice()< (p2.GetsellingPrice()) );
}
void Product::SortFunction(vector<Product> *p,int size)
{
}
ostream& operator<<(ostream& s,Product p)
{
s << "product name is " << p.Getname() << endl;
return s;
}
测试文件
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "Product.h"
using namespace std;
string readFromStringLines(string &str)
{
size_t pos = 0;
string sapce = " ";
string sub_str;
pos = str.find(sapce);
if(pos!=string::npos)
{
sub_str = str.substr(0,pos);
str.erase(0,pos+sapce.length());
}
return sub_str;
}
int main()
{
ifstream infile;
infile.open("DataFinal.txt");
string str,sub_str1,sub_str2,sub_str3,sub_str4;
int i=0;
vector<Product> productVector;
Product *productArr[4];
while(getline(infile,str)&& i<4)
{
cout<<"line string is "<< str<<endl;
sub_str1 = readFromStringLines(str);
sub_str2 = readFromStringLines(str);
sub_str3 = readFromStringLines(str);
sub_str4 = str;
//cout << "tex rate is "<< stod(sub_str4)<< endl;
productArr[i] = new Product(sub_str1,stoi(sub_str2),stod(sub_str3),stod(sub_str4));
productArr[i]->CalculateAmountToPay(stod(sub_str3),stod(sub_str4));
productArr[i]->Display();
productVector.push_back(Product(sub_str1,stoi(sub_str2),stod(sub_str3),stod(sub_str4)));
i++;
}
//Product product_stream_test("SmartTV",12342,1500,25);
//cout << "overload stream test " << product_stream_test<< endl;
productVector.push_back(Product("SmartTV",12342,1500,25));
productVector.push_back(Product("Oven",12002,1100,15));
productVector.push_back(Product("Stove",45342,900,12));
cout << "sort start" << endl;
sort(productVector.begin(),productVector.end(),compare);
for(int i=0;i<productVector.size();i++)
{
productVector[i].Display();
}
infile.close();
return 0;
}
测试输入文件
SmartTV 12342 1500 25
Oven 12002 1100 15
Stove 45342 900 12
Laptop 10304 2500 18