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

Account Management System In this project, you will write a program to manage a

ID: 3832729 • Letter: A

Question

Account Management System In this project, you will write a program to manage a bank account and a stock portfolio. The program will be written using inheritance structure for the classes. Starting with the abstract base class Account write two derived classes stockAccount and bankAccount. All of the accounts should be linked together through the common cashBalance variable. The starting balance for the account will be $10000. The balance will change as you perform the transactions. In the main menu, you can select to work with either stock account, bank account, or exit the program. The sub menu for each will be as following Stock account Display current price for a stock symbol Buy stock Sell stock Display current portfolio Display transactions history Return to main menu Bank account Display current cash balance Deposit to account Withdraw from account Display transactions history Return to main menu 1. Stock Account This program manage your stock portfolio. You can purchase or sell stocks. Use the stock nformation in stock1 txt stock 2.txt stock 3.txt stock4.txt files for all of the transactions. When you initiate a transaction, you can randomly select one of the files for stock prices.

Explanation / Answer

Account Management system code for both stock and bank is given below :

The Explanation of the code is given in the comments.

code :

account.cpp

#include "account.h"
float account::cash_balance = 10000;
account::account() //constructor
{
   //cash_balance=10000;
}

account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H

class account //abstract base class
{
public:
account(); //constructor
static float cash_balance;

//pure virtual functions to make the base class abstract
virtual float getBalance()=0;
virtual void setBalance(float)=0;
};
#endif

bankAccount.cpp

#include<string>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<sstream>
#include<time.h>
#include<cstdlib>
#include "bankAccount.h"
#include "account.h"
using namespace std;

bankAccount::bankAccount() //constructor
{

}

bankAccount::~bankAccount() //destructor
{

}

string bankAccount::getDate() //fucntion to print the date
{
char dateStr[9];
_strdate_s(dateStr);
return dateStr;
}

float bankAccount::view_balance() //function to print the cash_balance
{
cout<<"Your account balance is "<<cash_balance<<"$ ";
return cash_balance;
}

void bankAccount::deposit_bank(float money) //function to deposit money into bank account
{
cash_balance+=money;
cout<<"Your cash balance after depositing "<<money<<"$ is "<<cash_balance<<"$"<<endl;

   //writing the transaction to the history / log file
ofstream myfile;
myfile.open("bank_transaction_history.txt",ios::app);
if(myfile.is_open())
{
myfile.eof();
myfile<<" Deposit $"<<money<<" "<<getDate()<<" $"<<cash_balance<<endl;
}
else
{
cout<<"Cannot record this transaction. File opening failed. ";
}
}

void bankAccount::withdraw_bank(float money) //function to withdraw money from the bank account
{
if(money<cash_balance)
{
cash_balance-=money;
cout<<"Your cash balance after withdrawal of "<<money<<"$ is "<<cash_balance<<"$"<<endl;

       //writing the transaction to the history / log file
ofstream myfile;
myfile.open("bank_transaction_history.txt",ios::app);
if(myfile.is_open())
{
myfile<<" Withdrawal $"<<money<<" "<<getDate()<<" "<<cash_balance<<endl;
}
else
{
cout<<"Cannot record this transaction. File opening failed. ";
}
}
else
cout<<"Transaction failed. Insufficient balance "; //if balance not sufficient to withdraw
}

void bankAccount::print_bankHistory() //function to print transactions of the bank account
{
ifstream myfile;
string line;
myfile.open("bank_transaction_history.txt",ios::app);
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
cout<<line<<endl;
}
myfile.close();
}
else
cout<<"Error in opening the file. ";
}

float bankAccount::getBalance()
{
   return cash_balance;
}

void bankAccount::setBalance(float blnc)
{
   cash_balance=blnc;
}

bankAccount.h

#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H

#include "account.h"
#include<string>
using namespace std;

class bankAccount:public account //public inheritance from class 'account'
{
public:
   //member functions
bankAccount();
   ~bankAccount();
float view_balance();
void deposit_bank(float);
void withdraw_bank(float);
void print_bankHistory();
string getDate();
   virtual float getBalance();
   virtual void setBalance(float);
};
#endif

main_test.cpp

#include<iostream>
#include<cstdlib>
#include<stdlib.h>
#include<string>
#include<fstream>
#include<sstream>
#include<time.h>


#include "account.h"
#include "stockportfolio.h"
#include "bankAccount.h"

using std::cout;
using std::cin;
using std::endl;
using std::string;
int main() {

/*Engine *ep; //creating an engine

   ep = engOpen(NULL);
   if (ep == NULL)
   {
       std::cout << "Error: NOT FOUND!" << endl;
       exit(1);
   } */
int choice=999,s_choice=999,b_choice=999;
float deposit, withdraw;
   int length = 0;
   int value = 0;
   double array_1[100]; //arrays for plotting
   double array_2[100];
stockaccount stk_obj; //creating objects of bankaccount and stockaccount
bankAccount bank_obj;

fstream myfile;
string line;
float sub;
myfile.open("cash_balance.txt"); //copying the cash balance from the text file that has the closing value from previous run
if(myfile.is_open())
{
while(getline(myfile,line))
{
istringstream iss(line);
while(iss>>sub)
{
account::cash_balance=sub;
}
}
}
else
cout<<"Error in opening the file ";

//cout<<"Cash balance at the beginning is "<<account::cash_balance<<"$ ";

stk_obj.update_stocks(); //updating the stocks from previous transactions
stk_obj.current_portfolio(); //priting the current portfolio
//cout<<"Sorting....."<<endl;
//stk_obj.sort_wrapper(); //function for sorting the portfolio
while(choice!=3)
{
cout<<"Welcome to the Account Management System. ";
cout<<"Please select an account to access: 1. Stock Portfolio Account 2. Bank Account 3. Exit";
cout<<" Option: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<" Stock Portfolio Account Please select an option: 1. Display the price for a stock symbol";
cout<<" 2. Display the current portfolio 3. Buy shares 4. Sell shares 5. View a graph for the portfolio value";
cout<<" 6. View transaction history 7. Return to previous menu ";
cout<<" Option: ";
cin>>s_choice;

while(s_choice!=7)
{
               switch (s_choice)
               {
               case 1:
                   stk_obj.display(); //function to display current value of stock
                   break;
               case 2:
                   stk_obj.sort_wrapper(); //function for sorting the portfolio
                   stk_obj.current_portfolio();
                   break;
               case 3:
                   stk_obj.buy_shares(); //function to buy shares
                   break;
               case 4:
                   stk_obj.sell_shares(); //function to sell shares
                   break;
               case 5: //for plotting the graph of changing portfolio
break;
case 6:
stk_obj.transac_history();
break;
case 7:
break;
default:
cout<<"Please enter a number in between 1 and 7"<<endl;
break;
}
cout<<" Stock Portfolio Account Please select an option: 1. Display the price for a stock symbol";
cout<<" 2. Display the current portfolio 3. Buy shares 4. Sell shares 5. View a graph for the portfolio value";
cout<<" 6. View transaction history 7. Return to previous menu ";
cout<<" Option: ";
cin>>s_choice;
}
}
break;


case 2:
{
cout<<" Bank Account Please select an option: 1. View account balance 2. Deposit money 3. Withdraw money";
cout<<" 4. Print out history 5. Return to previous menu"<<endl;
cout<<" Option: ";
cin>>b_choice;
while(b_choice!=5)
{
switch(b_choice)
{
case 1:
bank_obj.view_balance();
break;
case 2:
cout<<"Please enter the amount you wish to deposit: ";
cin>>deposit;
bank_obj.deposit_bank(deposit);
break;
case 3:
cout<<"Please enter the amount you wish to withdraw: ";
cin>>withdraw;
bank_obj.withdraw_bank(withdraw);
break;
case 4:
bank_obj.print_bankHistory(); //to print all transactions related to the account
break;
case 5:
break;
default:
cout<<"Please enter a number in between 1 and 5"<<endl;
break;
}
cout<<" Bank Account Please select an option: 1. View account balance 2. Deposit money 3. Withdraw money";
cout<<" 4. Print out history 5. Return to previous menu"<<endl;
cout<<" Option: ";
cin>>b_choice;
}
}
break;


case 3:
break;
default:
cout<<"Please enter a number in between 1 and 3"<<endl;
break;
}

}

//stk_obj.sort_wrapper();
stk_obj.current_portfolio();
float tpf_value=stockaccount::total_portfolio_value; //to save the portfolio value at the end in a file
ofstream myfile3;
myfile3.open("total_portfolio_value.txt",ios::app);
myfile3<<tpf_value<<endl;
myfile3.close();

ofstream myfile2;
myfile2.open("cash_balance.txt"); //to save cash balance at the end in a file
myfile2<<"$"<<account::cash_balance<<" "<<bank_obj.getDate()<<" "<<stk_obj.getTime()<<endl;
myfile2.close();

system("pause");
return 0;
}

stockportfolio.cpp

#include<string>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<sstream>
#include<time.h>
#include<cstdlib>

#include "stockportfolio.h"
using namespace std;

float stockaccount::total_portfolio_value=0; //initializing the static variable

stockaccount::stockaccount()//constructor
{
   firstPtr=0;
   lastPtr=0;
}

string stockaccount::getTime() //function to print the system time
{
char timeStr[9];
_strtime_s(timeStr);
//cout<<"The current time is "<<timeStr;
return timeStr;
}

void stockaccount::display() //function to display the value of a stock
{
int i;
string symb;
cout<<"Please enter the stock symbol: ";
cin>>symb;
fstream myfile; //creating a stream object
string line,sub;
bool stock=false;

   //to pick one of the two available files
i=rand()%2;
if(i==0)
   {
myfile.open("Results_1.txt");
   }
else
{
myfile.open("Results_2.txt");
}
if(myfile.is_open())
{
while(getline(myfile,line))
{
istringstream iss(line); //constructing an istringstream object
float val;
while(iss>>sub>>val)
{
if(sub==symb)
{
cout<<" Company-Symbol Price Per Share ";
cout<<" "<<sub<<" $"<<val<<endl;
stock=true;
break;
}
}
}
}
if(stock==false)
{
cout<<"The symbol you entered is not present in the file. Please enter a valid symbol."<<endl;
}

}

void stockaccount::current_portfolio() //function that displays the current portfolio
{
cout<<"Current cash balance = $"<<cash_balance<<" ";
ListNode *ptr4=firstPtr;
string sym;
int num=0;
float sum=0;
if(ptr4==0) // condition that checks if portfolio is empty
   {
       cout<<" No stocks to display ";
       return;
   }
   else
{
cout<<" CompanySymbol Number PricePerShare TotalValue ";
while(ptr4!=0)
{
sym=ptr4->symbol;
num=ptr4->number;
if(num!=0)
{
cout<<sym<<" "<<num<<" ";

fstream myfile; //creating an fstream object
string line,sub;

           //picking one value from the 2 files
int i=rand()%2;
if(i==0)
{
myfile.open("Results_1.txt");
}
else
{
myfile.open("Results_2.txt");
}
if(myfile.is_open())
{
while(getline(myfile,line))
{
istringstream iss(line); //constructing an istringstream object
float val;
while(iss>>sub>>val)
{
if(sub==sym)
{
float tot=num*val;
cout<<"$"<<val<<" "<<"$"<<tot<<" ";
sum=sum+tot; //sums up the values of all the shares
//break;
}
}
}

}
}

ptr4=ptr4->next;
}
}
   total_portfolio_value=sum+cash_balance; //to find out the total value of the portfolio
cout<<" Total portfolio value : $"<<total_portfolio_value<<endl;
}

void stockaccount::buy_shares() //function to buy shares
{
string symb;
int num;
float max_pershare;
time_t seconds;
seconds=time(NULL);
struct tm *timeinfo;
   timeinfo=localtime(&seconds);
bool stock=false;
bool available=false;

cout<<"Please enter the ticker symbol of the stock you want to buy: "<<endl;
cin>>symb;
cout<<"Please enter the number of shares you want to buy: "<<endl;
cin>>num;
cout<<"Please enter the maximum amount you are willing to pay for each share of the stock: "<<endl;
cin>>max_pershare;

fstream myfile; //creating an fstream object
string line,sub;

   //to randomly pick one of the 2 files to read the values
int i=rand()%2;
if(i==0)
   {
myfile.open("Results_1.txt");
   }
else
{
myfile.open("Results_2.txt");
}
if(myfile.is_open())
{
while(getline(myfile,line))
{
istringstream iss(line); //constructing an istringstream object
float val;

while(iss>>sub>>val)
{
if(sub==symb)
{
available=true;
if(max_pershare>val) //checking if the user is willing to pay as much as the stock costs
{
cout<<"The current value of each share is: $"<<val<<endl;
float total_val=num*val;
if(total_val>cash_balance)
{
cout<<"Transaction failed. Insufficient balance in your account. ";
}
else
{
ofstream myfile;
myfile.open("stock_transaction_history.txt",ios::app); //append to existing history / log file
if(myfile.is_open())
{
myfile<<"Buy ";
myfile<<symb<<" ";
myfile<<num<<" ";
myfile<<"$"<<val<<" ";
myfile<<"$"<<total_val<<" ";
myfile<<getTime()<<endl;
myfile.close();
}
else
{
cout<<"Transaction failed. Unable to open file"<<endl;
break;
}

ofstream myfile2;
myfile2.open("bank_transaction_history.txt",ios::app); //recording the transaction in bank account
if(myfile2.is_open())
{
myfile2<<" Withdrawal $"<<total_val<<" ";
}
else
{
cout<<"Cannot record this transaction. File opening failed. ";
}

cout<<"Buy ";
cout<<symb<<" ";
cout<<num<<" ";
cout<<"$"<<val<<" ";
cout<<"$"<<total_val<<endl;

cash_balance=cash_balance-total_val; //to deduct the amount spent in buying from the bank account
cout<<"Cash Balance = $"<<cash_balance<<endl;

ListNode *ptr=firstPtr;
while(ptr!=0)
{
if(ptr->symbol==symb) //if the node corresponding to the portfolio exists
{
ptr->number=num+ptr->number; //adding the number of shares currently bought to already existing shares
stock=true;
break;
}
ptr=ptr->next;
}
if(stock==false) //if a new stock is bought
{
cout<<"creating a new node for "<<symb<<endl;
ListNode *newNode=new ListNode(symb,num);
addNode(newNode);
}
break;
}

}
else
{
cout<<"Transaction failed. The maximum amount entered is less than the price of the stock"<<endl;
break;
}
}

}
}
}

if(available==false)
{
cout<<"Transaction failed. The stock you want to buy is not available ";
}
}

void stockaccount::addNode(ListNode *newNode) //function to add a new node to a list
{
if(firstPtr==0)
{
firstPtr=lastPtr=newNode;
}
else
{
// cout<<"adding "<<newNode->symbol;
newNode->next=firstPtr;
firstPtr->prev=newNode;
firstPtr=newNode;
newNode->prev=NULL;
}
}

void stockaccount::sell_shares() //function that implements selling of shares
{
string symb2;
int num;
float min_pershare;
bool available=false;
if(firstPtr!=0)
{
cout<<"Please enter the ticker symbol of the stock you wish to sell: "<<endl;
cin>>symb2;
   cout<<"Please enter the number of shares you wish to sell: "<<endl;
   cin>>num;
   cout<<"Please enter the minimum amount you want to sell each share of the stock for: "<<endl;
   cin>>min_pershare;

fstream myfile; //creating a stream object
string line,sub;

   //randomly choosing one of the 2 files
int i=rand()%2;
if(i==0)
   {
myfile.open("Results_1.txt");
   }
else
{
myfile.open("Results_2.txt");
}
if(myfile.is_open())
{
while(getline(myfile,line))
{
istringstream iss(line); //constructing an istringstream object
float val;
while(iss>>sub>>val)
{
ListNode *index=firstPtr;
while(index!=0)
{
if(sub==symb2&&index->symbol==symb2) //checking if the stock actually exists in the portfolio
{
available=true;
if(index->number<num) //to check if there are sufficient shares
{
cout<<"Transaction failed. You do not have enough shares to sell. ";
}
else
{
if(min_pershare<val)
{
float total_val=num*val;
ofstream myfile_in;
myfile_in.open("stock_transaction_history.txt",ios::app); //recording a transaction in the history file
if(myfile_in.is_open())
{
myfile_in<<"Sell ";
myfile_in<<symb2<<" ";
myfile_in<<num<<" ";
myfile_in<<"$"<<val<<" ";
myfile_in<<"$"<<total_val<<" ";
myfile_in<<getTime()<<endl;
myfile_in.close();
}
else
{
cout<<"Transaction failed. Unable to open file"<<endl;
break;
}

ofstream myfile3;
myfile3.open("bank_transaction_history.txt",ios::app); //recording the transaction in the bank account
if(myfile3.is_open())
{
myfile3<<" Deposit $"<<total_val<<" ";
}
else
{
cout<<"Cannot record this transaction. File opening failed. ";
}

cout<<"Sell ";
cout<<symb2<<" ";
cout<<num<<" ";
cout<<"$"<<val<<" ";
cout<<"$"<<total_val<<endl;

cash_balance=cash_balance+total_val; //increasing the cash balance by the amount obtained from selling shares
cout<<"Cash Balance="<<cash_balance<<endl;

ListNode *ptr=firstPtr;
while(ptr!=0)
{
if(ptr->symbol==symb2)
{
ptr->number=ptr->number-num;
if(ptr->number==0) //if the total number of shares becomes zero after selling, delete the node
{
delNode(symb2);
}
}
ptr=ptr->next;
}
}
else
{
cout<<"Transaction failed. The amount you entered is greater than the price of the stock"<<endl;
}
}

}
index=index->next;
}
}
}
}
if(available==false)
{
cout<<"Transaction failed. The stock you want to sell is not available ";
}
}
else
cout<<"You have no shares to sell ";

}

void stockaccount::delNode(string node) //function to delete node when value is zero
{
cout<<"entering into delete ";
ListNode *ptr=firstPtr;
ListNode *temp1=0;
ListNode *temp2=0;
if(firstPtr==0)
{
cout<<"Stock not found ";
}
while(ptr!=0) //loop to find the node to be deleted
{
if(ptr->symbol==node)
{
break;
}
else
{
ptr=ptr->next;
}
}
temp1=ptr->prev; //using temporary ptrs for the previous and next node of the node to be deleted
temp2=ptr->next;
if(ptr==0)
{
cout<<"Stock not found ";
}
else
{
if(ptr==firstPtr)
{
if(ptr==lastPtr)
{
cout<<"Selling the last stock you own ";
firstPtr=lastPtr=0;
}
else
firstPtr=firstPtr->next;

}
else
{
temp1->next=ptr->next;
temp2->prev=ptr->prev;
}
delete ptr;
}

}

void stockaccount::transac_history()
{
ifstream myfile;
string line;
myfile.open("stock_transaction_history.txt");
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
cout<<line<<endl;
}
myfile.close();
}
else
cout<<"Error in opening the file. ";
}

float stockaccount::getBalance()
{
return cash_balance;
}

void stockaccount::setBalance(float blnc)
{
cash_balance=blnc;
}

void stockaccount::update_stocks() //for updating the values of stocks bought
{
bool stock=false;
ifstream myfile;
myfile.open("stock_transaction_history.txt");
string line,event,stock_symb,date;
int number_shares;
float value_perShare, total_value;
while(getline(myfile,line))
{
istringstream iss(line);
       if (myfile.eof())
           break;
while(iss>>event>>stock_symb>>number_shares>>value_perShare>>total_value>>date)
{

if(event=="Buy") //if there is a transaction of buy
{
// cout<<"First ptr is "<<firstPtr->symbol<<endl;
ListNode *ptr=firstPtr;
//stock=true;
while(ptr!=0)
{
if(ptr->symbol==stock_symb)
{
stock=true;
ptr->number=number_shares+ptr->number;
// cout<<"Printing after adding "<<ptr->symbol<<" ";
// cout<<ptr->symbol<<" "<<ptr->number<<endl;
}
else
stock=false;
ptr=ptr->next;
}
if(stock==false)
{
// cout<<"creating "<<stock_symb<<endl;

ListNode *newNode=new ListNode(stock_symb,number_shares);
addNode(newNode); //adding a new node
}
      
}
else if(event=="Sell")
{
          
               ListNode *ptr5=firstPtr;
stock=false;
while(ptr5!=0)
{
if(ptr5->symbol==stock_symb)
{
stock=true;
ptr5->number=ptr5->number-number_shares;
if(ptr5->number==0)
{
delNode(stock_symb);
}
}
ptr5=ptr5->next;
}
/* if(stock==false)
{
cout<<"You do not have shares of this stock in your portfolio. ";
}*/
}
  
}
  

}
}

ListNode *stockaccount::sortList(ListNode *first,ListNode *second)
{
if(!first) // if first linked list is empty
return second;
if(!second) // if second linked list is empty
return first;

ifstream myfile;
string line,sub;
float temp_value1,temp_value2;
//ListNode *current=firstPtr;

int j=rand()%2;
if(j==0)
{
myfile.open("Results_1.txt");
}
else
{
myfile.open("Results_2.txt");
}
while(getline(myfile,line))
{
istringstream iss(line); //constructing an istringstream object
float val;

while(iss>>sub>>val)
{
if(sub==first->symbol)
{
temp_value1=val;
//cout<<"Found temp1 "<<temp_value1<<" ";
}
if(sub==second->symbol)
{
temp_value2=val;
//cout<<"Found temp2 "<<temp_value2<<" ";
}
}

}

if(((first->number)*temp_value1) > ((second->number)*temp_value2)) //to check the total value of all the shares
{
// cout<<"Correct ";
first->next=sortList(first->next,second);
first->next->prev = first;
first->prev = NULL;
// cout<<"returning... ";
return first;
}
else
{
// cout<<"Wrong ";
second->next = sortList(first,second->next);
// cout<<"Pass"<<endl;
second->next->prev = second;
second->prev = NULL;
// cout<<"Returning ";
return second;
}

}

ListNode *stockaccount::split(ListNode *first) //function to split the DLL recursively
{
ListNode *temp1=first;
ListNode *temp2=first;
ListNode *temp;

while(temp1->next&&temp1->next->next)
{
temp1=temp1->next->next;
temp2=temp2->next;
}
temp=temp2->next;
temp2->next=NULL;
return temp;
}

ListNode *stockaccount::merge_sort(ListNode *firstN) //function to perform merge sort
{
if(!firstN || !firstN->next)
{
return firstN;
}
ListNode *second=split(firstN);
firstN=merge_sort(firstN);
second=merge_sort(second);

return sortList(firstN,second);
}

void stockaccount::sort_wrapper() //wrapper function for merge sort
{
if(firstPtr==0)
{

}
else
{
//cout<<"firstptr is "<<firstPtr->symbol<<endl;
firstPtr=merge_sort(firstPtr);
}

}

stockportfolio.h

#ifndef STOCKPORTFOLIO_H
#define STOCKPORTFOLIO_H

#include "account.h"
#include<string>
using namespace std;

class ListNode //implementation of list nodes
{
friend class stockaccount; //declaring stockaccount as friend
public: //pubic member functions
ListNode(string& name,int numb)
:symbol(name),number(numb)
{
this->next=NULL;
this->prev=NULL;
}

private:
string symbol;
int number;
ListNode *next;//next node in the list
ListNode *prev;
};

class stockaccount:public account //public inheritance from class account
{
public:
stockaccount();
static float total_portfolio_value;
void display();
void current_portfolio();
void buy_shares();
void addNode(ListNode *);
void sell_shares();
void delNode(string node);
void transac_history();
void update_stocks();
string getTime();

   //functions that constitute the merge sort operation
ListNode *split(ListNode *);
ListNode *merge_sort(ListNode *);
ListNode *sortList(ListNode *first,ListNode *second);
void sort_wrapper();
virtual float getBalance();
   virtual void setBalance(float);
private:
ListNode *firstPtr; //pointer to first node
ListNode *lastPtr; //pointer to second node
int size_list; //size of the doubly linked list
};
#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote