Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Money struct contains two integers. - One represents how many dollars you ha

ID: 3891180 • Letter: T

Question

The Money struct contains two integers. - One represents how many dollars you have - The other represents how many cents you have . The Account struct has three variables -A Money struct that will represent how much money is in the account - One variable to represent the interest rate as a decimal value. - The final variable will contain the name of the account. (Checking, Savings, CD, etc) Negative amounts of Money are stored by making both variables of the Money object negative Use the following function prototypes - Account createAccount (std::string file) e The function shall look for a file matching the name of the parameter that is passed to the function. The file contains the name, interest rate, and starting balance in that order e You may assume that each of the three items are contained on their own line e You may NOT assume that the name of the account contains only a single word e You may also assume that a positive and valid (whole number or no more than two decimal places) amount of money will be contained in the file e An Account object with the e If the file does not exist or otherwise cannot be opened, a default Account with name "Sav- proper values is then returned ings," an interest rate of 1%, and a starting balance of $100.00 is returned - createAccount(std::string nane, double rate, Noney balance) The function will return an account with the values already set based on the parameters passed - Account deposit(Account account, Money deposit) e The function shall not accept negative amounts of money If a negative amount of money is attempted to be deposited, an error message will be displayed, and the original account will be returned e A message shall be printed to the screen that takes the form "$X.XXdeposited into [NAME only if a successful deposit is made. The message appears on its own line NAME] shall be replaced with the name of the account The balance of the account shall be updated accordingly -Money withdraw(Account 8account, Money withdraw) The function shall not accept negative amounts of money If a negative amount of money is attempted to be withdrawn, an error message will be displayed, and a Money object equivalent to $0.00 will be returned e You may allow the account to be overdrawn, but by no more than $S0.00 e Amessage shall be printed to the screen that takes the form "SX.XX withdrawn from [NAME whether or not a successful withdrawl is made. The message appears on its own line NAME] shall be replaced with the name of the account e The balance of the account shall be updated accordingly

Explanation / Answer

struct Money{

   int dollars;
   int cents;
};

struct Account{

   Money balance;
   double rateOfIntrest;
   string name;
};

Account createAccount(std::string file){

    Account a;
    ifstream fin(file.c_str());
    if (!fin){
        cout << 'Error opening file ";
        a.name = "Savings";
        a.rateOfIntrest = 1
        a.balance.dollars = 100;
        a.balance.cents = 0;
        return a;
    }
    string word;
    a.name = "";
    while(fin >> word){
       if (isalpha(word[0])){
          a.name = a.name + " " + word;
       }
       else {
          fin >> word;
          a.rateOfIntrest = atof(word.c_str();
          fin >> word;
          int a = (int)atof(word.c_str());
          float b = atof(word.c_str())-a;
          int c = (int)(b * 100);
          a.balance.dollars = a;
          a.balance.cents = c;
          break;
         
       }       
    }
    return a;
}

Account createAccount(std::string name, double rate, Money balance){
     Account a;
     a.name = name;
     a.rateOfIntrest = rate;
     a.balance.dollars = balance.dollars;
     a.balance.cents = balance.cents;
     return a;
}

Account deposit(Account account, Money deposit){
    if (deposit.dollars < 0 && deposit.cents < 0){
       cout << "Error: Invalid Money ";
       return account;
    }
    account.balance.dollars = account.balance.dollars + deposit.dollars;
    account.balance.cents = account.balance.cents + deposit.cents;
    if (account.balance.cents > 100){
        int d = account.balance.cents / 100;
        int c = account.balance.cents % 100;
        account.balance.dollars = account.balance.dollars + d;
        account.balance.cents = c;
    }
    return account;
}

Money withdraw(Account &account, Money withdraw){

    cout << "$" << withdraw.dollars << "." << withdraw.cents << " withdrawn from " << account.name << endl;
    if (withdraw.dollars < 0 && withdraw.cents < 0){
       cout << "Error:Invalid Money ";
       Money a;
       a.dollars = 0;
       a.cents = 0;
       return a;
    }
    float bal = account.balance.dollars + account.balance.cents/100;
    float amt = withdraw.dollars + withdraw.cents/100;

    if (amt > (bal + 50)){
       cout << "Error:Insufficient fund ";
       Money a;
       a.dollars = 0;
       a.cents = 0;
       return a;
    }
    float total = bal - amt;
    int a = total;
    int b = (total - a)*100;
    account.balance.dollars = a;
    account.balance.cents = b;
    return withdraw;
   
}

void accure(Account account){
    float bal = account.balance.dollars + account.balance.cents/100;
    float earn = bal * account.rateOfIntrest;

    cout << "At " << account.rateOfIntres << "% your " << account.name << " accont earned $" << earn << endl;
}


void print(Money m){
    cout << "($" << m.dollars << "." << m.cents << ")" << endl;
   
}

void print(Account m){
    cout << "($" << m.balance.dollars << "." << m.balance.cents << ")" << endl;
   
}