C++/c++

Advertisement


Question
thankz for guiding mi throughout the process.i did nt gt frustrated.it is veri hard.i am trying veri hard to understand the program.

i hv problem doing the account calculation,invest,summary.
Account:Final balance is computed as original balance plus interest earned.
Investment:the gain/loss is computed as the difference between current value and invested value.
Summary:The customers shall be listed in ascending order of customerID.
Total bank balance is computed as the sum of bank balances of all bank accounts.
Total investment value is computed as the sum of current values of all investments accounts.
Grand total is computed as the sum of total bank balance and total investment value.
could u pls help mi.i will be so greatly appericated for all the help frm u.

“Individual Customer Statistics”
One customer can have more than one bank accounts and investment accounts, associated by customerID.
For bank accounts, the interest rates are derived based on following table:
Customer Type          Account Type
         Savings      Checkings    Fixed Deposit
Normal          1.5%          1.0%         2.0%
Silver          Normal+0.5%    Normal+0.25%  Normal+1.0%
Gold          Normal+1.0%    Normal+0.5%   Normal+2.0%
Platinum        Normal+1.5%    Normal+0.75%  Normal+3.0%

output:
*** Individual Customer Statistics ***    
*** Customer Particulars ***   
Customer ID: 10001   
Name: Mickey Mouse   
Address: Sengkang   
Phone: 91111111   
Customer Type: Gold   
  
*** Bank accounts ***   
Number: 700001   
Type: Savings   
Original Balance: $10000.00   
Num of years: 2   
Interest earned: $500.00    
Final Balance: $10500.00   
  
Number: 800001   
Type: Checkings   
Original Balance: $20000.00   
Num of years: 3   
Interest earned: $900.00   
Final Balance: $20900.00   
  
*** Investment accounts ***   
Number: 3300001   
Type: Stock   
Invested value: $10000.00   
Current value: $12000.00   
Gain/ Loss: $2000.00   
  
Number: 8800001   
Type: RealEstate   
Invested value: $100000.00   
Current value: $120000.00   
Gain/ Loss: $20000.00   

*** Summary of Customer Portfolios ***          
customerID totalBankBalance totalInvestValue grandTotal
10001      $31,400.00       $132,000.00        $163,400.00

code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>

using namespace std;


class Customer
{
public:
  enum CustomerType
  {
      Normal = 1,
      Gold,
      Silver,
      Platinum
  };

  Customer () {}
  void setCustomer(int, string, string, string, int, CustomerType);
  void display();
  bool read(ifstream& fin);
  CustomerType getType(void)const{return type;}

private:
  int customerID;
  string firstName;
  string lastName;
  string address;
  int phoneNum;
  CustomerType type;

  static const int ID_LENGTH = 5;
  static const int PHONE_LENGTH = 8;
};

class Account
{
public:
  enum AccountType
  {
      Savings = 1,
      Checkings,
      FixedDeposit
  };

  Account () {}
  void setAccount(int, int, AccountType, int, int, int);
  void display();
  bool read(ifstream& fin);
  

private:
  int customerID;
  int accountNum;
  int balance;
  int numYears;
  int finalBalance;
  AccountType type;

  static const int ID_LENGTH = 5;
  static const int ACCOUNT_LENGTH = 6;

};

class Investment
{
public:
  enum InvestmentType
  {
      Stock = 1,
      RealEstate
  };

  Investment () {}
  void setInvestment(int, int, InvestmentType, int, int, int);
  void display();
  bool read(ifstream& fin);

private:
  int customerID;
  int investNum;
  int investValue;
  int currValue;
  int gain_loss;
  InvestmentType type;

  static const int ID_LENGTH = 5;
  static const int INVESTNUM_LENGTH = 7;

};


void Customer::setCustomer(int id, string fn, string ln, string a, int pn, CustomerType ct)
{
  customerID = id;
  firstName = fn;
  lastName = ln;
  address = a;
  phoneNum = pn;
  type = ct;
}

void Customer::display()
{
  cout << endl;
  cout << "======================================" << endl;
  cout << "*** Individual Customer Statistics ***" << endl;
  cout << "*** Customer Particulars ***" << endl;
  cout << "CustomerID: " << customerID << ' ' << "\nName: " << firstName << "  " <<  lastName << "  "
       << "\nAddress: " << address << " " << "\nPhone: " << phoneNum << " " << "\nCustomer Type: " << type << endl;
  
  switch(type)
 {
  case Normal:   cout << "Normal\n";
         break;
  case Gold:     cout << "Gold\n";
         break ;
  case Silver:   cout << "Silver\n";
         break;
  case Platinum: cout << "Platinum\n";
         break;
  default:       cout << "invalid value\n";
 }   
}

bool allDigits(const string& input)
{
  for(size_t ix = 0; ix < input.size(); ++ix)
  {
      if (!isdigit(input[ix])) return false;
  }
  return true;
}

void toLowerCase(string& input)
{
  for(size_t ix = 0; ix < input.size(); ++ix) input[ix] = tolower(input[ix]);
}

bool Customer::read(ifstream& fin)
{
  // First read individual strings from the file
  string idText;
  string firstNameText;
  string lastNameText;
  string addressText;
  string phoneText;
  string typeText;


  // Read the entire line in, then create a string stream from the line.
  // The string stream will break the line down into its pieces.
  string line;
  getline(fin, line);
  istringstream ss (line);
  ss >> idText >> firstNameText >> lastNameText >> addressText >> phoneText >> typeText;


  // Now check that each input is valid

  // Check ID
  if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

  // Check names
  if (firstNameText.length() == 0) return false;
  if (lastNameText.length() == 0) return false;

  // Check address
  if (addressText.length() == 0) return false;

  // Check phone
  if (phoneText.length() != PHONE_LENGTH || !allDigits(phoneText)) return false;

  // Check customer type
  CustomerType inputType;
  toLowerCase(typeText);
  if(typeText == "normal")inputType = Normal;
  else if (typeText =="gold")inputType = Gold;
  else if (typeText == "silver")inputType = Silver;
  else if (typeText == "platinum")inputType = Platinum;
  else return false;

  // Now assign all inputs to the customer
  setCustomer(atoi(idText.c_str()),
      firstNameText,
      lastNameText,
      addressText,
      atoi(phoneText.c_str()),
      inputType);
  return true;
}

void Account::setAccount(int id, int an, AccountType at, int bl, int ny, int fb)
{
  customerID = id;
  accountNum = an;
  balance = bl;
  numYears = ny;
  finalBalance = fb;
  type = at;
}

void Account::display()
{
  cout << endl;
  cout << "=====================" << endl;
  cout << "*** Bank accounts ***" << endl;
  cout << "Number: " << accountNum << "\nType: " << type  
       << "\nOriginal Balance: " << "$" << balance << "\nNumber of Years: " << numYears << "\nInterest Earned: "
     << "$" << earn << "\nFinal Balance: " << "$" << finalBalance << endl;
  
  switch(type)
 {
  case Savings:       cout << "Savings\n";
         break;
  case Checkings:     cout << "Checkings\n";
         break ;
  case FixedDeposit:  cout << "FixedDeposit\n";
         break;
  default:          cout << "invalid value\n";
 }
 
 finalBalance=balance+earn;  
}

bool is_positive_double(const std::string& str )
{
  std::istringstream stm(str) ;
  double d = -1.0 ;

  // try to read a double from the stream
  if( stm >> d )
  {
      char cruff_remaining ;
      // if there are more chars left
      if( stm >> cruff_remaining ) return false ;

      else return d > 0.0 ; // no more chars, success if number read is positive
  }

  return false ; // read of double failed
}

bool Account::read(ifstream& fin)
{
  // First read individual data from the file
  string idText;
  string accountNumText;
  string typeText;
  string balanceText;
  string numYearsText;


  // Read the entire line in, then create a string stream from the line.
  // The string stream will break the line down into its pieces.
  string line;
  getline(fin, line);
  istringstream ss (line);
  ss >> idText >> accountNumText >> typeText >> balanceText >> numYearsText;


  // Now check that each input is valid

  // Check ID
  if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

  // Check AccountNum
  if (accountNumText.length() != ACCOUNT_LENGTH || !allDigits(accountNumText)) return false;

  // Check account type
  AccountType inputType;
  toLowerCase(typeText);
  if (typeText == "savings")inputType = Savings;
  else if (typeText == "checkings")inputType = Checkings;
  else if (typeText == "fixeddeposit")inputType = FixedDeposit;
  else return false;
  
  // Check balance
  if(!is_positive_double(balanceText)) return false;

  // Check years
  if (numYearsText.length() == 0) return false;

  // Now assign all inputs to the account
  setAccount(atoi(idText.c_str()),
      atoi(accountNumText.c_str()),
      inputType,
      atoi(balanceText.c_str()),
      atoi(numYearsText.c_str()));
  return true;

}

void Investment::setInvestment(int id, int in, InvestmentType it, int iv, int cv, int gl)
{
  customerID = id;
  investNum = in;
  investValue = iv;
  currValue = cv;
  gain_loss = gl;
  type = it;
}

void Investment::display()
{
  cout << endl;
  cout << "===================" << endl;
  cout << "*** Investments ***" << endl;
  cout << "Number: " << investNum << "\nType: " << type << "\nInvested Value: " << "$" << investValue
     << "\nCurrent Value: " << "$" << currValue << "\nGain/Loss: " << "$" << gain_loss << endl;
  
  switch(type)
 {
  case Stock:         cout << "Stock\n";
         break;
  case RealEstate:    cout << "RealEstate\n";
         break ;
  default:          cout << "invalid value\n";
 }
 
  ////////////////////////////////////////////////////////
  // Print out gain_loss
  ////////////////////////////////////////////////////////
  int currValue,investValue;

   gain_loss=currValue-investValue;  
}

bool characters(const std::string& str )
{
  std::istringstream stm(str) ;
  double d = -1.0 ;

  // try to read a double from the stream
  if( stm >> d )
  {
      char cruff_remaining ;
      // if there are more chars left
      if( stm >> cruff_remaining ) return false ;

      else return d > 0.0 ; // no more chars, success if number read is positive
  }

  return false ; // read of double failed
}

bool Investment::read(ifstream& fin)
{
  // First read individual data from the file
  string idText;
  string investNumText;
  string typeText;
  string investValueText;
  string currValueText;



  // Read the entire line in, then create a string stream from the line.
  // The string stream will break the line down into its pieces.
  string line;
  getline(fin, line);
  istringstream ss (line);
  ss >> idText >> investNumText >> typeText >> investValueText >> currValueText;


  // Now check that each input is valid

  // Check ID
  if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

  // Check AccountNum
  if (investNumText.length() != INVESTNUM_LENGTH || !allDigits(investNumText)) return false;

  // Check account type
  InvestmentType inputType;
  toLowerCase(typeText);
  if (typeText =="stock")inputType = Stock;
  else if (typeText == "realestate")inputType =  RealEstate;
  else return false;

  // Check invest value
  if(!characters(investValueText)) return false;

  // Check current value
  if (currValueText.length() == 0) return false;

  // Now assign all inputs to the investment
  setInvestment(atoi(idText.c_str()),
      atoi(investNumText.c_str()),
      inputType,
      atoi(investValueText.c_str()),
      atoi(currValueText.c_str()));
  return true;


}


int main()
{
  
  ////////////////////////////////////////////////////////
  // Open files
  ////////////////////////////////////////////////////////

  ifstream customerFile("customers.txt");
  ifstream accountFile("account.txt");
  ifstream investmentFile("investment.txt");

  if (!customerFile.is_open())
  {
      cout << "Error opening customer file";
      exit (1);
  }

  if (!accountFile.is_open())
  {
      cout << "Error opening accounts file";
      exit (1);
  }

  if (!investmentFile.is_open())
  {
      cout << "Error opening investments file";
      exit (1);
  }

  ////////////////////////////////////////////////////////
  // Read in files
  ////////////////////////////////////////////////////////

  vector<Customer> customers;
  vector<Account> accounts;
  vector<Investment> investments;

  Customer c;
  while (!customerFile.eof())
  {
      if (c.read(customerFile)) customers.push_back(c);
  }

  Account a;
  while (!accountFile.eof())
  {
      if (a.read(accountFile)) accounts.push_back(a);
  }

  Investment i;
  while (!investmentFile.eof())
  {
      if (i.read(investmentFile)) investments.push_back(i);
  }

  ////////////////////////////////////////////////////////
  // Print out overall statistics
  ////////////////////////////////////////////////////////
  int gold = 0;
  int platinum = 0;
  int silver = 0;
  int normal = 0;
  int type = 0;
  for (size_t i = 0; i < customers.size(); ++i)
  {
      switch(customers[i].getType())
      {
        case Customer::Normal: ++normal;
         break;
      case Customer::Silver: ++silver;
         break;
       case Customer::Gold: ++gold;
         break;
        case Customer::Platinum: ++platinum;
         break;
      }
  }
  
  type = normal+silver+gold+platinum;
  
  cout << "===================================" << endl;
  cout << "*** Overall Customer Statistics ***" << endl;
  cout << "Number of customers type Normal: " << normal << endl;
  cout << "Number of customers type Silver: " << silver << endl;
  cout << "Number of customers type Gold: " << gold << endl;
  cout << "Number of customers type Platinim: " << platinum << endl;
  cout << "Total number of customers: " << type << endl;
  cout << endl;

  ////////////////////////////////////////////////////////
  // Print out individual customers
  ////////////////////////////////////////////////////////
  for (size_t i = 0; i < customers.size(); ++i)
  {
      customers[i].display();
  }

  ////////////////////////////////////////////////////////
  // Print out bank accounts
  ////////////////////////////////////////////////////////
  for (size_t i = 0; i < accounts.size(); ++i)
  {
      accounts[i].display();
  }
   
  ////////////////////////////////////////////////////////
  // Print out investment accounts
  ////////////////////////////////////////////////////////
  for (size_t i = 0; i < investments.size(); ++i)
  {
      
     investments[i].display();
  }
  
  cout << "======================================" << endl;
  cout << "*** Summary of Customer Portfolios ***" << endl;
  cout << customerID << totalBankBalance <<totalInvestValue <<   grandTotal << endl;
     
  
  system("pause");
  return 0;

}  

Answer
Hello Lisa.

This assignment is certainly getting complicated. Here are my comments for your latest code. I have done work only on the Account class. All my new code starts with //NEW. You should look for //NEW and read what I've done.

I believe that the account.txt file does not have the final balance. The final balance must be calculated. So it's not right to include the final balance in the setAccount method. I've taken it out of the parameter list and instead, I set the final balance to the starting balance. The final balance is updated when interest is calculated.

Similarly, for investment gain/loss is not in the file. It must be calculated by subtracting the current value from the invested value. I have not done any work on that. Perhaps you could do it.

At the end of main, you are doing
cout << customerID << totalBankBalance <<totalInvestValue <<   grandTotal << endl;
what are those variables? They are not defined anywhere.

The interest calculations are complicated because interest depends on the account type AND the customer type.

With your design, you need to:
go through each account,
find the customer for that account
get the customer type
calculate the interest based on the account type and customer type

We will need to make the vectors holding customers, accounts, and investments global so all functions can see them.
We will need a function to find the customerType given a customerID.
We will need an addInterest method in the account.

I don't think my Account::addInterest method is the best because it needs Account to know about Customer types, but it's the only thing I can think of for the design you have.

You should look carefully at Account::addInterest to make sure it is doing what you need. The assignment is a little confusing.

The code is getting a little long to cut and paste. If you like, you can contact me at zlatko.c.help at gmail.com and send your code as an attachment.

When you make changes, try to get the code to compile. Look at the compiler errors, and think about what needs to be done to fix them. Think of the compiler as a coach. That might teach you more about programming.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>

/*

*/

using namespace std;


class Customer
{
public:
   enum CustomerType
   {
       Normal = 1,
       Gold,
       Silver,
       Platinum
   };

   Customer () {}
   void setCustomer(int, string, string, string, int, CustomerType);
   void display();
   bool read(ifstream& fin);
   CustomerType getType(void)const{return type;}
   //NEW
   int getId(void) const { return customerID; }

private:
   int customerID;
   string firstName;
   string lastName;
   string address;
   int phoneNum;
   CustomerType type;

   static const int ID_LENGTH = 5;
   static const int PHONE_LENGTH = 8;
};

class Account
{
public:
   enum AccountType
   {
       Savings = 1,
       Checkings,
       FixedDeposit
   };

   Account () {}
   void setAccount(int id, int an, AccountType at, int bl, int ny);
   void display();
   bool read(ifstream& fin);
   void addInterest(void); // NEW


private:
   int customerID;
   int accountNum;
   int balance;
   int numYears;
   int finalBalance;
   int earn; // NEW
   AccountType type;

   static const int ID_LENGTH = 5;
   static const int ACCOUNT_LENGTH = 6;

};

class Investment
{
public:
   enum InvestmentType
   {
       Stock = 1,
       RealEstate
   };

   Investment () {}
   void setInvestment(int id, int in, InvestmentType it, int iv, int cv);
   void display();
   bool read(ifstream& fin);

private:
   int customerID;
   int investNum;
   int investValue;
   int currValue;
   int gain_loss;
   InvestmentType type;

   static const int ID_LENGTH = 5;
   static const int INVESTNUM_LENGTH = 7;

};

// NEW
// Global vectors to hold the customers, accounts, and investments
vector<Customer> customers;
vector<Account> accounts;
vector<Investment> investments;

// NEW
// getCustomerType returns the customer type for a given customer ID
Customer::CustomerType getCustomerType(int customerId)
{
   for (size_t i = 0; i < customers.size(); ++i)
   {
       if (customers[i].getId() == customerId) return customers[i].getType();
   }
   cout << "Error: customer " << customerId << " was not found\n";
   return Customer::Normal;
}

void Customer::setCustomer(int id, string fn, string ln, string a, int pn, CustomerType ct)
{
   customerID = id;
   firstName = fn;
   lastName = ln;
   address = a;
   phoneNum = pn;
   type = ct;
}

void Customer::display()
{
   cout << endl;
   cout << "======================================" << endl;
   cout << "*** Individual Customer Statistics ***" << endl;
   cout << "*** Customer Particulars ***" << endl;
   cout << "CustomerID: " << customerID << ' ' << "\nName: " << firstName << "  " <<  lastName << "  "
       << "\nAddress: " << address << " " << "\nPhone: " << phoneNum << " " << "\nCustomer Type: " << type << endl;

   switch(type)
   {
   case Normal:   cout << "Normal\n";
       break;
   case Gold:     cout << "Gold\n";
       break ;
   case Silver:   cout << "Silver\n";
       break;
   case Platinum: cout << "Platinum\n";
       break;
   default:       cout << "invalid value\n";
   }   
}

bool allDigits(const string& input)
{
   for(size_t ix = 0; ix < input.size(); ++ix)
   {
       if (!isdigit(input[ix])) return false;
   }
   return true;
}

void toLowerCase(string& input)
{
   for(size_t ix = 0; ix < input.size(); ++ix) input[ix] = tolower(input[ix]);
}

bool Customer::read(ifstream& fin)
{
   // First read individual strings from the file
   string idText;
   string firstNameText;
   string lastNameText;
   string addressText;
   string phoneText;
   string typeText;


   // Read the entire line in, then create a string stream from the line.
   // The string stream will break the line down into its pieces.
   string line;
   getline(fin, line);
   istringstream ss (line);
   ss >> idText >> firstNameText >> lastNameText >> addressText >> phoneText >> typeText;


   // Now check that each input is valid

   // Check ID
   if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

   // Check names
   if (firstNameText.length() == 0) return false;
   if (lastNameText.length() == 0) return false;

   // Check address
   if (addressText.length() == 0) return false;

   // Check phone
   if (phoneText.length() != PHONE_LENGTH || !allDigits(phoneText)) return false;

   // Check customer type
   CustomerType inputType;
   toLowerCase(typeText);
   if(typeText == "normal")inputType = Normal;
   else if (typeText =="gold")inputType = Gold;
   else if (typeText == "silver")inputType = Silver;
   else if (typeText == "platinum")inputType = Platinum;
   else return false;

   // Now assign all inputs to the customer
   setCustomer(atoi(idText.c_str()),
       firstNameText,
       lastNameText,
       addressText,
       atoi(phoneText.c_str()),
       inputType);
   return true;
}

void Account::setAccount(int id, int an, AccountType at, int bl, int ny)
{
   customerID = id;
   accountNum = an;
   balance = bl;
   numYears = ny;
   type = at;
   earn = 0; // NEW
   finalBalance = balance; // NEW
}

void Account::display()
{
   cout << endl;
   cout << "=====================" << endl;
   cout << "*** Bank accounts ***" << endl;
   cout << "Number: " << accountNum << "\nType: " << type  
       << "\nOriginal Balance: " << "$" << balance << "\nNumber of Years: " << numYears
       << "\nInterest Earned: "  << "$" << earn
       << "\nFinal Balance: " << "$" << finalBalance << endl;

   switch(type)
   {
   case Savings:       cout << "Savings\n";
       break;
   case Checkings:     cout << "Checkings\n";
       break ;
   case FixedDeposit:  cout << "FixedDeposit\n";
       break;
   default:          cout << "invalid value\n";
   }
}

// NEW
void Account::addInterest(void)
{
   Customer::CustomerType customerType = getCustomerType(customerID);
   float interestRate;

   // Set the normal interest rate
   switch(type)
   {
   case Savings:
       interestRate = 1.5;
       break;
   case Checkings:
       interestRate = 1.0;
       break;
   case FixedDeposit:
       interestRate = 2.0;
       break;
   }

   // Add bonuses for account type and customer type
   switch(customerType)
   {
   case Customer::Silver:
       switch(type)
       {
       case Savings:
         interestRate += 0.5;
         break;
       case Checkings:
         interestRate += 0.25;
         break;
       case FixedDeposit:
         interestRate += 1.0;
         break;
       }
       break;
   case Customer::Gold:
       switch(type)
       {
       case Savings:
         interestRate += 1.0;
         break;
       case Checkings:
         interestRate += 0.5;
         break;
       case FixedDeposit:
         interestRate += 2.0;
         break;
       }
       break;
   case Customer::Platinum:
       switch(type)
       {
       case Savings:
         interestRate += 1.5;
         break;
       case Checkings:
         interestRate += 0.75;
         break;
       case FixedDeposit:
         interestRate += 3.0;
         break;
       }
       break;
   }

   finalBalance = balance;
   earn = 0;
   for(int year = 1; year <= this->numYears; ++year)
   {
       float interest = finalBalance * (interestRate / 100);
       finalBalance += interest;
       earn += interest;
   }
}

bool is_positive_double(const std::string& str )
{
   std::istringstream stm(str) ;
   double d = -1.0 ;

   // try to read a double from the stream
   if( stm >> d )
   {
       char cruff_remaining ;
       // if there are more chars left
       if( stm >> cruff_remaining ) return false ;

       else return d > 0.0 ; // no more chars, success if number read is positive
   }

   return false ; // read of double failed
}

bool Account::read(ifstream& fin)
{
   // First read individual data from the file
   string idText;
   string accountNumText;
   string typeText;
   string balanceText;
   string numYearsText;


   // Read the entire line in, then create a string stream from the line.
   // The string stream will break the line down into its pieces.
   string line;
   getline(fin, line);
   istringstream ss (line);
   ss >> idText >> accountNumText >> typeText >> balanceText >> numYearsText;


   // Now check that each input is valid

   // Check ID
   if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

   // Check AccountNum
   if (accountNumText.length() != ACCOUNT_LENGTH || !allDigits(accountNumText)) return false;

   // Check account type
   AccountType inputType;
   toLowerCase(typeText);
   if (typeText == "savings")inputType = Savings;
   else if (typeText == "checkings")inputType = Checkings;
   else if (typeText == "fixeddeposit")inputType = FixedDeposit;
   else return false;

   // Check balance
   if(!is_positive_double(balanceText)) return false;

   // Check years
   if (numYearsText.length() == 0) return false;

   // Now assign all inputs to the account
   setAccount(atoi(idText.c_str()),
       atoi(accountNumText.c_str()),
       inputType,
       atoi(balanceText.c_str()),
       atoi(numYearsText.c_str()));
   return true;

}

void Investment::setInvestment(int id, int in, InvestmentType it, int iv, int cv)
{
   customerID = id;
   investNum = in;
   investValue = iv;
   currValue = cv;
   type = it;
}

void Investment::display()
{
   cout << endl;
   cout << "===================" << endl;
   cout << "*** Investments ***" << endl;
   cout << "Number: " << investNum << "\nType: " << type << "\nInvested Value: " << "$" << investValue
       << "\nCurrent Value: " << "$" << currValue << "\nGain/Loss: " << "$" << gain_loss << endl;

   switch(type)
   {
   case Stock:         cout << "Stock\n";
       break;
   case RealEstate:    cout << "RealEstate\n";
       break ;
   default:          cout << "invalid value\n";
   }

   ////////////////////////////////////////////////////////
   // Print out gain_loss
   ////////////////////////////////////////////////////////
   //int currValue,investValue;
   //gain_loss=currValue-investValue;  
}

bool characters(const std::string& str )
{
   std::istringstream stm(str) ;
   double d = -1.0 ;

   // try to read a double from the stream
   if( stm >> d )
   {
       char cruff_remaining ;
       // if there are more chars left
       if( stm >> cruff_remaining ) return false ;

       else return d > 0.0 ; // no more chars, success if number read is positive
   }

   return false ; // read of double failed
}

bool Investment::read(ifstream& fin)
{
   // First read individual data from the file
   string idText;
   string investNumText;
   string typeText;
   string investValueText;
   string currValueText;



   // Read the entire line in, then create a string stream from the line.
   // The string stream will break the line down into its pieces.
   string line;
   getline(fin, line);
   istringstream ss (line);
   ss >> idText >> investNumText >> typeText >> investValueText >> currValueText;


   // Now check that each input is valid

   // Check ID
   if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

   // Check AccountNum
   if (investNumText.length() != INVESTNUM_LENGTH || !allDigits(investNumText)) return false;

   // Check account type
   InvestmentType inputType;
   toLowerCase(typeText);
   if (typeText =="stock")inputType = Stock;
   else if (typeText == "realestate")inputType =  RealEstate;
   else return false;

   // Check invest value
   if(!characters(investValueText)) return false;

   // Check current value
   if (currValueText.length() == 0) return false;

   // Now assign all inputs to the investment
   setInvestment(atoi(idText.c_str()),
       atoi(investNumText.c_str()),
       inputType,
       atoi(investValueText.c_str()),
       atoi(currValueText.c_str()));
   return true;


}


int main()
{

   ////////////////////////////////////////////////////////
   // Open files
   ////////////////////////////////////////////////////////

   ifstream customerFile("customers.txt");
   ifstream accountFile("account.txt");
   ifstream investmentFile("investment.txt");

   if (!customerFile.is_open())
   {
       cout << "Error opening customer file";
       exit (1);
   }

   if (!accountFile.is_open())
   {
       cout << "Error opening accounts file";
       exit (1);
   }

   if (!investmentFile.is_open())
   {
       cout << "Error opening investments file";
       exit (1);
   }

   ////////////////////////////////////////////////////////
   // Read in files
   ////////////////////////////////////////////////////////

   Customer c;
   while (!customerFile.eof())
   {
       if (c.read(customerFile)) customers.push_back(c);
   }

   Account a;
   while (!accountFile.eof())
   {
       if (a.read(accountFile)) accounts.push_back(a);
   }

   Investment i;
   while (!investmentFile.eof())
   {
       if (i.read(investmentFile)) investments.push_back(i);
   }

   ////////////////////////////////////////////////////////
   // Print out overall statistics
   ////////////////////////////////////////////////////////
   int gold = 0;
   int platinum = 0;
   int silver = 0;
   int normal = 0;
   int type = 0;
   for (size_t i = 0; i < customers.size(); ++i)
   {
       switch(customers[i].getType())
       {
       case Customer::Normal: ++normal;
         break;
       case Customer::Silver: ++silver;
         break;
       case Customer::Gold: ++gold;
         break;
       case Customer::Platinum: ++platinum;
         break;
       }
   }

   type = normal+silver+gold+platinum;

   cout << "===================================" << endl;
   cout << "*** Overall Customer Statistics ***" << endl;
   cout << "Number of customers type Normal: " << normal << endl;
   cout << "Number of customers type Silver: " << silver << endl;
   cout << "Number of customers type Gold: " << gold << endl;
   cout << "Number of customers type Platinim: " << platinum << endl;
   cout << "Total number of customers: " << type << endl;
   cout << endl;

   ////////////////////////////////////////////////////////
   // Print out individual customers
   ////////////////////////////////////////////////////////
   for (size_t i = 0; i < customers.size(); ++i)
   {
       customers[i].display();
   }

   ////////////////////////////////////////////////////////
   // Print out bank accounts
   ////////////////////////////////////////////////////////
   for (size_t i = 0; i < accounts.size(); ++i)
   {
       accounts[i].display();
   }

   ////////////////////////////////////////////////////////
   // Print out investment accounts
   ////////////////////////////////////////////////////////
   for (size_t i = 0; i < investments.size(); ++i)
   {
       investments[i].display();
   }

   //NEW
   ////////////////////////////////////////////////////////
   // Add Interest to all accounts
   ////////////////////////////////////////////////////////
   for (size_t i = 0; i < accounts.size(); ++i)
   {
       accounts[i].addInterest();
   }

   cout << "======================================" << endl;
   cout << "*** Summary of Customer Portfolios ***" << endl;
   // What is this??? cout << customerID << totalBankBalance <<totalInvestValue <<   grandTotal << endl;


   system("pause");
   return 0;

}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.