-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocks.cpp
103 lines (95 loc) · 1.98 KB
/
stocks.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
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
99
100
101
102
103
// beginning of stocks.cpp file
#include <iostream>
#include <cstring>
class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot();
public:
void acquire(const char * co, int n, double pr);
void buy(int num, double price);
void sell(int num, double price);
void update(double price);
void show();
}; // note semicolon at the end of a class
inline void Stock::set_tot() // use inlien in defintion
{
total_val = shares * share_val;
}
// more stocks.cpp -- implementing the class member functions
void Stock::acquire(const char * co, int n, double pr)
{
std::strncpy(company, co, 29); // truncate co to fit company
company[29] = '\0';
if (n < 0)
{
std::cerr << "Number of shares can't be negative. "
<< company << " shares set to 0.\n";
shares = 0;
} else {
shares = n;
}
share_val = pr;
set_tot();
}
void Stock::buy(int num, double price)
{
if (num < 0)
{
std::cerr << "Number of shares purchased can't be negative. "
<< "Transaction is aborted.\n";
} else {
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(int num, double price)
{
using std::cerr;
if (num < 0) {
cerr << "Number of shares sold cannot be negative. "
<< "Transaction is aborted.\n";
} else if (num > shares) {
cerr << "You can't sell more than you have! "
<< "Transaction is aborted.\n";
} else {
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
using std::cout;
using std::endl;
cout << "Company: " << company
<< " Shares: " << shares << endl
<< " Shares Price: $" << share_val
<< " Total Worth: $" << total_val << endl;
}
int main()
{
using std::cout;
using std::ios_base;
Stock stock1;
stock1.acquire("NonoSmart", 20, 12.50);
cout.setf(ios_base::fixed);
cout.precision(2);
cout.setf(ios_base::showpoint);
stock1.show();
stock1.buy(15, 18.25);
stock1.show();
stock1.sell(400, 20.00);
stock1.show();
return 0;
}