Advertisement
bwukki

Untitled

Feb 11th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. //Since all of the assignment from 5.30 to 5.35 were just changes to the Account and DollarAmount classes, here is the final header file with all the changes made to it
  2.  
  3.  
  4. // Fig. 5.8: DollarAmount.h
  5. // DollarAmount class stores dollar amounts as a whole numbers of pennies
  6. #include <string>
  7. #include <cmath>
  8.  
  9. class DollarAmount {
  10. public:
  11.     DollarAmount(int indollars, int inpennies){
  12.         amount = indollars*100+inpennies;
  13.         dollars = indollars;
  14.         cents = inpennies;
  15.     }
  16.    explicit DollarAmount(int64_t value) : amount{value} { }
  17.  
  18.    void add(DollarAmount right) {
  19.       amount += right.amount;
  20.       dollars = amount/100;
  21.       cents = amount % 100;
  22.    }
  23.    int getTotal() {
  24.     return(dollars*100+cents);
  25.    }
  26.  
  27.    void subtract(DollarAmount right) {
  28.       amount -= right.amount;class DollarAmount {
  29. public:
  30.     DollarAmount(int indollars, int inpennies){
  31.         amount = indollars*100+inpennies;
  32.         dollars = indollars;
  33.         cents = inpennies;
  34.     }
  35.    explicit DollarAmount(int64_t value) : amount{value} { }
  36.  
  37.    void add(DollarAmount right) {
  38.       amount += right.amount;
  39.       dollars = amount/100;
  40.       cents = amount % 100;
  41.    }
  42.  
  43.    void subtract(DollarAmount right) {
  44.       amount -= right.amount;
  45.    }
  46.  
  47.  
  48.     void divide(int divisor) {
  49.     amount = amount/divisor;
  50.     dollars = dollars/divisor;
  51.     cents = cents/divisor;
  52.     }
  53.    void addInterest(int rate, int divisor) {
  54.       DollarAmount interest{
  55.          (amount * rate + divisor / 2) / divisor
  56.       };
  57.  
  58.       add(interest);
  59.    }
  60.  
  61.    std::string toString() const {
  62.       std::string returnDollars{std::to_string(dollars)};
  63.       std::string returnCents{std::to_string(cents)};
  64.       return returnDollars + "." + (returnCents.size() == 1 ? "0" : "") + returnCents;
  65.    }
  66. private:
  67.    int64_t amount{0};
  68.    int dollars;
  69.    int cents;
  70. };
  71.    }
  72.  
  73.  
  74.     void divide(int divisor) {
  75.     amount = amount/divisor;
  76.     dollars = dollars/divisor;
  77.     cents = cents/divisor;
  78.     }
  79.  
  80.    void addInterest(int rate, int divisor) {
  81.       DollarAmount interest{
  82.          (amount * rate + divisor / 2) / divisor
  83.       };
  84.  
  85.       add(interest);
  86.       std::cout << "INTEREST RATE ENTERED = " << rate/(double)divisor << std::endl;
  87.    }
  88.  
  89.    std::string toString() const {
  90.       std::string returnDollars{std::to_string(dollars)};
  91.       std::string returnCents{std::to_string(cents)};
  92.       return returnDollars + "." + (returnCents.size() == 1 ? "0" : "") + returnCents;
  93.    }
  94. private:
  95.    int64_t amount{0};
  96.    int dollars;
  97.    int cents;
  98. };
  99.  
  100. class Account {
  101. public:
  102.     Account(std::string accountName, int initialBalanceDollars, int initialBalanceCents)
  103.         : name{accountName} {
  104.  
  105.         if (initialBalanceDollars > 0 || initialBalanceCents > 0) {
  106.             DollarAmount initialDeposit{initialBalanceDollars,initialBalanceCents};
  107.             balance.add(initialDeposit);
  108.     }
  109.     }
  110.     void deposit(int depositAmountDollars,int depositAmountCents) {
  111.     if (depositAmountDollars > 0) {
  112.         DollarAmount deposit{depositAmountDollars,depositAmountCents};
  113.         balance.add(deposit);
  114.     }
  115.     }
  116.  
  117.     std::string getBalance() {
  118.     return(balance.toString());
  119.     }
  120.  
  121.     void setName(std::string accountName) {
  122.     name = accountName;
  123.     }
  124.  
  125.     std::string getName() const {
  126.     return name;
  127.     }
  128.  
  129.     std::string withdraw(int withdrawAmountDollars,int withdrawAmountCents) {
  130.     int currentbalance = balance.getTotal();
  131.     if (withdrawAmountDollars > currentbalance) {
  132.         return("You do not have enough money in your account to do that.");
  133.     }
  134.     else {
  135.         DollarAmount withdrawAmount{-1*withdrawAmountDollars,-1*withdrawAmountCents};
  136.         balance.add(withdrawAmount);
  137.         return("Money withdrawn succesfully.");
  138.     }
  139.     }
  140.  
  141. private:
  142.     std::string name;
  143.     DollarAmount balance{0,0};
  144. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement