3
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class StockRecord {
private:
char id[12];
char company[50];
float purchasePrice;
float currentPrice;
int numberOfStock;
public:
StockRecord(const char *_id = "",const char *_company = "", float _purchasePrice = 0, int _numberOfStock = 0) {
strcpy(id, _id);
strcpy(company, _company);
purchasePrice = _purchasePrice;
numberOfStock = _numberOfStock;
}
~StockRecord(){}
StockRecord(const StockRecord &s) {
strcpy(id, s.id);
strcpy(company, s.company);
purchasePrice = s.purchasePrice;
currentPrice = s.currentPrice;
numberOfStock = s.numberOfStock;
}
void setNewPrice(float p){currentPrice = p;}
float value() const {return static_cast<float>(numberOfStock)*currentPrice;}
float profit() const {return static_cast<float>(numberOfStock)*(currentPrice-purchasePrice);}
friend ostream &operator<<(ostream &os, const StockRecord &st);
};
ostream &operator<<(ostream &os, const StockRecord &st) {
os << st.company << " "
<< st.numberOfStock << " "
<< st.purchasePrice << " "
<< st.currentPrice << " "
<< st.profit() << endl;
return os;
}
class Client {
char fullName[60];
int id;
StockRecord *companies;
int numberOfCompanies;
public:
Client(const char *_fullName = "", int _id = 0, const StockRecord *_sr = nullptr, int _numberOfCompanies = 0) {
strcpy(fullName, _fullName);
id = _id;
numberOfCompanies = _numberOfCompanies;
if (numberOfCompanies > 0 and _sr != nullptr) {
companies = new StockRecord[numberOfCompanies];
for (int i = 0; i<numberOfCompanies; i++) {
companies[i] = _sr[i];
}
}
else
companies = nullptr;
}
~Client() {
delete[] companies;
}
Client(const Client &c) {
strcpy(fullName, c.fullName);
id = c.id;
numberOfCompanies = c.numberOfCompanies;
if (numberOfCompanies > 0 and c.companies != nullptr) {
companies = new StockRecord[numberOfCompanies];
for (int i = 0; i<numberOfCompanies; i++) {
companies[i] = c.companies[i];
}
}
else
companies = nullptr;
}
Client &operator+=(const StockRecord &s) {
StockRecord *temp = new StockRecord[numberOfCompanies+1];
for (int i = 0; i<numberOfCompanies; i++) {
temp[i] = companies[i];
}
temp[numberOfCompanies] = s;
delete[] companies;
companies = temp;
numberOfCompanies++;
return *this;
}
float totalValue() const {
float val = 0.0;
for (int i = 0; i<numberOfCompanies; i++)
val += companies[i].value();
return val;
}
friend ostream &operator<<(ostream &os, const Client &c);
};
ostream &operator<<(ostream &os, const Client &c) {
os << c.id << " " << c.totalValue()<<endl;
for (int i = 0; i<c.numberOfCompanies; i++)
os << c.companies[i];
return os;
}
int main() {
int test;
cin >> test;
if(test == 1){
float price;
cout << "=====TEST NA KLASATA StockRecord=====" << endl;
StockRecord sr("1", "Microsoft", 60.0, 100);
cout << "Konstruktor OK" << endl;
cin >> price;
sr.setNewPrice(price);
cout << "SET metoda OK" << endl;
}
else if(test == 2){
cout << "=====TEST NA METODITE I OPERATOR << OD KLASATA StockRecord=====" << endl;
char id[12], company[50];
float price, newPrice;
int n, shares;
cin >> n;
for(int i = 0; i < n; ++i){
cin >> id;
cin >> company;
cin >> price;
cin >> newPrice;
cin >> shares;
StockRecord sr(id, company, price, shares);
sr.setNewPrice(newPrice);
cout << sr.value() << endl;
cout << sr;
}
}
else if(test == 3){
cout << "=====TEST NA KLASATA Client=====" << endl;
char companyID[12], companyName[50], clientName[50];
int clientID, n, shares;
float oldPrice, newPrice;
bool flag = true;
cin >> clientName;
cin >> clientID;
cin >> n;
Client c(clientName, clientID);
cout << "Konstruktor OK" << endl;
for(int i = 0; i < n; ++i){
cin >> companyID;
cin >> companyName;
cin >> oldPrice;
cin >> newPrice;
cin >> shares;
StockRecord sr(companyID, companyName, oldPrice, shares);
sr.setNewPrice(newPrice);
c += sr;
if(flag){
cout << "Operator += OK" << endl;
flag = false;
}
}
cout << c;
cout << "Operator << OK" << endl;
}
return 0;
}Editor is loading...
Leave a Comment