stock00.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // stock00.cpp -- implementing the Stock class
  2. // version 00
  3. #include <iostream>
  4. #include "stock00.h"
  5. void Stock::acquire(const std::string & co, long n, double pr)
  6. {
  7. company = co;
  8. if (n < 0)
  9. {
  10. std::cout << "Number of shares can't be negative; "
  11. << company << " shares set to 0.\n";
  12. shares = 0;
  13. }
  14. else
  15. shares = n;
  16. share_val = pr;
  17. set_tot();
  18. }
  19. void Stock::buy(long num, double price)
  20. {
  21. if (num < 0)
  22. {
  23. std::cout << "Number of shares purchased can't be negative. "
  24. << "Transaction is aborted.\n";
  25. }
  26. else
  27. {
  28. shares += num;
  29. share_val = price;
  30. set_tot();
  31. }
  32. }
  33. void Stock::sell(long num, double price)
  34. {
  35. using std::cout;
  36. if (num < 0)
  37. {
  38. cout << "Number of shares sold can't be negative. "
  39. << "Transaction is aborted.\n";
  40. }
  41. else if (num > shares)
  42. {
  43. cout << "You can't sell more than you have! "
  44. << "Transaction is aborted.\n";
  45. }
  46. else
  47. {
  48. shares -= num;
  49. share_val = price;
  50. set_tot();
  51. }
  52. }
  53. void Stock::update(double price)
  54. {
  55. share_val = price;
  56. set_tot();
  57. }
  58. void Stock::show()
  59. {
  60. std::cout << "Company: " << company
  61. << " Shares: " << shares << '\n'
  62. << " Share Price: $" << share_val
  63. << " Total Worth: $" << total_val << '\n';
  64. }