Question

Create a currency class, up to 5 individual currencies, with four attributes - currency name, whole...

Create a currency class, up to 5 individual currencies, with four attributes - currency name, whole parts and fractional parts and fractional's name such that 100 fractional parts equals 1 whole part, e.g. Pound, 1, 55, pence or Euro, 3, 33, cent. Define overloaded operators to add or subtract different currency objects - care should be taken that you can only perform these operation on objects of the same currency. Also, take care that fractional parts roll into whole parts. Define an overloaded input stream operator to accept values of any currency as well as an overloaded output stream operator to write out the value of the currency.. Initialize your currency objects demonstrating polymorphic construction. Your constructors should also use initialization lists. Define a Wallet class that will contain the 5 individual currency types you define and will implement the following - number of individual currency types, check if a currency type exists in the wallet, add money by currency, remove money by currency, remove all currencies, check if wallet is empty. To add or remove money into/from wallet, check if currency exists in the wallet. If so, add or remove the amount specified. Your main should allow the user to add a maximum of 5 different currency types to the wallet in the form of the base currency array - some examples are Dollar and cent, Euro and cent, Pound and pence, Ruble and kopeck. Then use a random number generator to add and remove currency values between 0 and 99 for both the whole and fractional values. You can think about using ENUMs for the currency types to be generated. Finally, output the total value and name of the currencies in your wallet. Finally empty the wallet and print relevant information about the operation.

Homework Answers

Answer #1

main.cpp

#include <iostream>
#include <iomanip>
#include "Wallet.h"

using namespace std;

void addMoney(Wallet&);
void subtractMoney(Wallet&);
void viewWallet(Wallet&);
void emptyWallet(Wallet&);


int main()
{
   Wallet myWallet;

   bool loop = false;

   while (loop == false)
   {
       int choice = 0;

       cout << endl << "   ***YOUR WALLET***" << endl << endl;
       cout << "   1: Add Money" << endl;
       cout << "   2: Subtract Money" << endl;
       cout << "   3: View Wallet" << endl;
       cout << "   4: Empty Wallet" << endl;
       cout << "   5: EXIT" << endl << endl;
       cout << "   Choice: ";

       cin >> choice;
      

       switch (choice)
       {
       case 1:
           system("cls");
           addMoney(myWallet);
           system("cls");
           break;

       case 2:
           system("cls");
           subtractMoney(myWallet);
           system("cls");
           break;

       case 3:
           system("cls");
           viewWallet(myWallet);
           system("cls");
           break;

       case 4:
           system("cls");
           emptyWallet(myWallet);
           system("cls");
           break;

       case 5:
           loop = true;
           break;

       default:
           cout << "\t\t**Please enter a valid choice**\n" << endl; //error message when choice is not from the menu
           break;

       }

       system("cls"); //clear screen

   }
  
   return 0;
}


void addMoney(Wallet &myWallet)
{

   bool loop = false;

   while (loop == false)
   {
       int choice = 0;

       double currency = 0;

       cout << endl << "   ***Add Money***" << endl << endl
           << "   1: Dollar" << endl
           << "   2: Dinar" << endl
           << "   3: Euro" << endl
           << "   4: Peso" << endl
           << "   5: Pound" << endl
           << "   6: Exit" << endl << endl
           << "   Choice: ";
       cin >> choice;


       switch (choice)
       {
       case 1:
           cout << "How much would you like to add? (Format: n.nn)\n";
           cin >> currency;
           myWallet.addMoney(Wallet::DOLLAR, currency);

           cout << "Added " << fixed << setprecision(2) << currency << " in dollars to wallet\n" << endl;
           break;

       case 2:
           cout << "How much would you like to add? (Format: n.nn)\n";
           cin >> currency;
           myWallet.addMoney(Wallet::DINAR, currency);

           cout << "Added " << fixed << setprecision(2) << currency << " in dinars to wallet\n" << endl;
           break;

       case 3:
           cout << "How much would you like to add? (Format: n.nn)\n";
           cin >> currency;
           myWallet.addMoney(Wallet::EURO, currency);

           cout << "Added " << fixed << setprecision(2) << currency << " in euros to wallet\n" << endl;
           break;

       case 4:
           cout << "How much would you like to add? (Format: n.nn)\n";
           cin >> currency;
           myWallet.addMoney(Wallet::PESO, currency);

           cout << "Added " << fixed << setprecision(2) << currency << " in pesos to wallet\n" << endl;
           break;

       case 5:
           cout << "How much would you like to add? (Format: n.nn)\n";
           cin >> currency;
           myWallet.addMoney(Wallet::POUND, currency);

           cout << "Added " << fixed << setprecision(2) << currency << " in pounds to wallet\n" << endl;
           break;

       case 6:
           loop = true;
           break;

       default:
           cout << "\t\t**Please enter a valid choice**\n" << endl;
           break;

       }

   }
}


void subtractMoney(Wallet &myWallet)
{
   bool loop = false;

   while (loop == false)
   {
       int choice = 0;

       double currency = 0;

       cout << endl << "   ***Subtract Money***" << endl << endl
           << "   1: Dollar" << endl
           << "   2: Dinar" << endl
           << "   3: Euro" << endl
           << "   4: Peso" << endl
           << "   5: Pound" << endl
           << "   6: Exit" << endl << endl
           << "   Choice: ";
       cin >> choice;


       switch (choice)
       {
       case 1:
           cout << "How much would you like to subtract? (Format: n.nn)\n";
           cin >> currency;
           myWallet.subtractMoney(Wallet::DOLLAR, currency);

           cout << "Subtracted " << fixed << setprecision(2) << currency << " in dollars to wallet\n" << endl;
           break;

       case 2:
           cout << "How much would you like to subtract? (Format: n.nn)\n";
           cin >> currency;
           myWallet.subtractMoney(Wallet::DINAR, currency);

           cout << "Subtracted " << fixed << setprecision(2) << currency << " in dinars to wallet\n" << endl;
           break;

       case 3:
           cout << "How much would you like to subtract? (Format: n.nn)\n";
           cin >> currency;
           myWallet.subtractMoney(Wallet::EURO, currency);

           cout << "Subtracted " << fixed << setprecision(2) << currency << " in euros to wallet\n" << endl;
           break;

       case 4:
           cout << "How much would you like to subtract? (Format: n.nn)\n";
           cin >> currency;
           myWallet.subtractMoney(Wallet::PESO, currency);

           cout << "Subtracted " << fixed << setprecision(2) << currency << " in pesos to wallet\n" << endl;
           break;

       case 5:
           cout << "How much would you like to subtract? (Format: n.nn)\n";
           cin >> currency;
           myWallet.subtractMoney(Wallet::POUND, currency);

           cout << "Subtracted " << fixed << setprecision(2) << currency << " in pounds to wallet\n" << endl;
           break;

       case 6:
           loop = true;
           break;

       default:
           cout << "\t\t**Please enter a valid choice**\n" << endl;
           break;

       }

   }
}

//Get values from wallet class and display them on screen
void viewWallet(Wallet &myWallet)
{
   cout << endl << "   ***Viewing Wallet***" << endl << endl
       << "   1: Dollars: " << fixed << setprecision(2) << myWallet.getMoney(Wallet::DOLLAR) << "\n"
       << "   2: Dinars: " << fixed << setprecision(2) << myWallet.getMoney(Wallet::DINAR) << "\n"
       << "   3: Euro: " << fixed << setprecision(2) << myWallet.getMoney(Wallet::EURO) << "\n"
       << "   4: Peso: " << fixed << setprecision(2) << myWallet.getMoney(Wallet::PESO) << "\n"
       << "   5: Pound: " << fixed << setprecision(2) << myWallet.getMoney(Wallet::POUND) << "\n"
       << endl << endl;

   system("pause");
}

//Calls function from wallet class to set all values to zero
void emptyWallet(Wallet &myWallet)
{
   cout << endl << "   ***Emptying Wallet...***" << endl;
  
   myWallet.emptyWallet();
  
   cout << endl << "   ***Wallet Emptied***" << endl;

   system("pause");
}

Wallet.h

#ifndef WALLET_H
#define WALLET_H

#include <string>
#include <iostream>

#include "Currency.h"
#include "Dollar.h"
#include "Dinar.h"
#include "Euro.h"
#include "Peso.h"
#include "Pound.h"

class Wallet
{
private:
   // Currency Pointer for each currency type
   Currency *cPtr[5];
  
public:

   //enumertaors to hold currency types
   enum currencyFlag
   {
       DOLLAR,
       DINAR,
       EURO,
       PESO,
       POUND
   };
static currencyFlag x;
   //defaullt constructor
   Wallet();

   //default destructor
   ~Wallet();

   int numOfCurrencies() const;
   bool checkCurrencyExist(currencyFlag);
   void addMoney(currencyFlag, const double);
   void subtractMoney(currencyFlag, const double);
   double getMoney(currencyFlag);
   void emptyWallet();
   bool checkIfEmpty();

};

#endif

Wallet.cpp

#include "Wallet.h"
#include <math.h>
#include <random>

Wallet::Wallet()
{
   std::random_device rd;

   cPtr[DOLLAR] = new Dollar;
   cPtr[DOLLAR]->setFractVal(rd() % 100);
   cPtr[DOLLAR]->setWholeVal(rd() % 100);
  
   cPtr[DINAR] = new Dinar;
   cPtr[DINAR]->setFractVal(rd() % 100);
   cPtr[DINAR]->setWholeVal(rd() % 100);

   cPtr[EURO] = new Euro;
   cPtr[EURO]->setFractVal(rd() % 100);
   cPtr[EURO]->setWholeVal(rd() % 100);

   cPtr[PESO] = new Peso;
   cPtr[PESO]->setFractVal(rd() % 100);
   cPtr[PESO]->setWholeVal(rd() % 100);

   cPtr[POUND] = new Pound;
   cPtr[POUND]->setFractVal(rd() % 100);
   cPtr[POUND]->setWholeVal(rd() % 100);
}

Wallet::~Wallet()
{
   delete cPtr;
}


int Wallet::numOfCurrencies() const
{
   unsigned currencyCount = 0;
  
   for (int i = 0; i < 6; i++)
   {
       if (!cPtr[i]->isZero())
           currencyCount++;
   }

   return currencyCount;
}

bool Wallet::checkCurrencyExist(Wallet::currencyFlag flag)
{
   if (cPtr[flag]->isZero())
       return true;
   else
       return false;

}
void Wallet::addMoney(Wallet::currencyFlag flag, const double x)
{
   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   cPtr[flag]->setWholeVal(cPtr[flag]->getWholeVal() + static_cast<unsigned>(whole));

   // Fractional part
   cPtr[flag]->setFractVal(cPtr[flag]->getFractVal() + static_cast<unsigned>(fract));

   // Update currency values
   cPtr[flag]->updateCurrencyVal();
}
void Wallet::subtractMoney(Wallet::currencyFlag flag, const double x)
{
   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   cPtr[flag]->setWholeVal(cPtr[flag]->getWholeVal() - static_cast<unsigned>(whole));

   // Fractional part
   cPtr[flag]->setFractVal(cPtr[flag]->getFractVal() - static_cast<unsigned>(fract));

   // Update currency values
   cPtr[flag]->updateCurrencyVal();
}

double Wallet::getMoney(Wallet::currencyFlag flag)
{
   double currencyValue = cPtr[flag]->getWholeVal() + (cPtr[flag]->getFractVal() / 100);
   return currencyValue;
}

void Wallet::emptyWallet()
{
   for (int i = 0; i < 6; i++)
   {
       cPtr[i]->setZero();
   }
}
bool Wallet::checkIfEmpty()
{
   bool empty = true;

   int i = 0;
   while (i < 6 && empty)
   {
       if (!cPtr[i]->isZero())
           empty = false;
   }
  
   return empty;
}

Pound.cpp


#include "Pound.h"
#include <math.h>
// Update whole and fractional parts
void Pound::updateCurrencyVal()
{
   if (fractVal > 100)
   {
       wholeVal += fractVal / 100;

       fractVal = fractVal % 100;
   }
}

//Operator overload implementation
Pound Pound::operator + (const Pound &x)
{
   Pound newPound;

   newPound.setWholeVal(this->wholeVal + x.getWholeVal());
   newPound.setFractVal(this->fractVal + x.getFractVal());
   newPound.updateCurrencyVal();

   return newPound;
}
Pound Pound::operator + (const double x)
{
   Pound newPound;

   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   newPound.setWholeVal(this->wholeVal + static_cast<unsigned>(whole));

   // Fractional part
   newPound.setFractVal(this->fractVal + static_cast<unsigned>(fract));

   // Update currency values
   newPound.updateCurrencyVal();

   return newPound;
}
Pound Pound::operator - (const Pound &x)
{
   Pound newPound;

   newPound.setWholeVal(this->wholeVal - x.getWholeVal());
   newPound.setFractVal(this->fractVal - x.getFractVal());
   newPound.updateCurrencyVal();

   return newPound;
}
Pound Pound::operator - (const double x)
{
   Pound newPound;

   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   newPound.setWholeVal(this->wholeVal - static_cast<unsigned>(whole));

   // Fractional part
   newPound.setFractVal(this->fractVal - static_cast<unsigned>(fract));

   // Update currency values
   newPound.updateCurrencyVal();

   return newPound;
}
Pound Pound::operator = (const Pound &x)
{
   Pound newPound;

   newPound.setWholeVal(x.getWholeVal());
   newPound.setFractVal(x.getFractVal());
   newPound.updateCurrencyVal();

   return newPound;
}
Pound Pound:: operator = (const double x)
{
   Pound newPound;
   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   newPound.setWholeVal(static_cast<unsigned>(whole));

   // Fractional part
   newPound.setFractVal(static_cast<unsigned>(fract));

   // Update currency values
   newPound.updateCurrencyVal();

   return newPound;
}

std::ostream& operator << (std::ostream &out, const Pound &x)
{
   double currencyValue = x.getWholeVal() + x.getFractVal();
   out << to_string(currencyValue);
   return out;
}
std::istream& operator >> (std::istream &in, Pound &x)
{
   double fract,
       whole,
       val;

   in >> val;

   // Extract whole and fractional parts
   fract = modf(val, &whole);
   fract *= 100;
   fract = round(fract);

   Pound newPound;

   x.setWholeVal(static_cast<unsigned>(whole)+x.getWholeVal());
   x.setFractVal(fract + x.getFractVal());
   x.updateCurrencyVal();
   return in;
}

Pound.h

#ifndef POUND_H
#define POUND_H

#include "Currency.h"
using namespace std;

//Pound class inherited form currency class
class Pound : public Currency
{
private:


public:

   //Constructors
   Pound(unsigned wV, unsigned fV) : Currency("Pound", "Pence", wV, fV)
   {
   }

   Pound() : Currency("Pound", "Pence")
   {
   }

   void updateCurrencyVal();

   // Overloaded Operators
   Pound operator + (const Pound &);
   Pound operator + (const double);
   Pound operator - (const Pound &);
   Pound operator - (const double);
   Pound operator = (const Pound &);
   Pound operator = (const double);

   friend std::ostream& operator << (std::ostream &, const Pound &);
   friend std::istream& operator >> (std::istream &, Pound &);
};

#endif


Peso.cpp

#include "Peso.h"
#include <math.h>

// Update whole and fractional parts
void Peso::updateCurrencyVal()
{
   if (fractVal > 100)
   {
       wholeVal += fractVal / 100;

       fractVal = fractVal % 100;
   }
}

//Operator overload implementation
Peso Peso::operator + (const Peso &x)
{
   Peso newPeso;

   newPeso.setWholeVal(this->wholeVal + x.getWholeVal());
   newPeso.setFractVal(this->fractVal + x.getFractVal());
   newPeso.updateCurrencyVal();

   return newPeso;
}
Peso Peso::operator + (const double x)
{
   Peso newPeso;

   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   newPeso.setWholeVal(this->wholeVal + static_cast<unsigned>(whole));

   // Fractional part
   newPeso.setFractVal(this->fractVal + static_cast<unsigned>(fract));

   // Update currency values
   newPeso.updateCurrencyVal();

   return newPeso;
}
Peso Peso::operator - (const Peso &x)
{
   Peso newPeso;

   newPeso.setWholeVal(this->wholeVal - x.getWholeVal());
   newPeso.setFractVal(this->fractVal - x.getFractVal());
   newPeso.updateCurrencyVal();

   return newPeso;
}
Peso Peso::operator - (const double x)
{
   Peso newPeso;

   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   newPeso.setWholeVal(this->wholeVal - static_cast<unsigned>(whole));

   // Fractional part
   newPeso.setFractVal(this->fractVal - static_cast<unsigned>(fract));

   // Update currency values
   newPeso.updateCurrencyVal();

   return newPeso;
}
Peso Peso::operator = (const Peso &x)
{
   Peso newPeso;

   newPeso.setWholeVal(x.getWholeVal());
   newPeso.setFractVal(x.getFractVal());
   newPeso.updateCurrencyVal();

   return newPeso;
}
Peso Peso:: operator = (const double x)
{
   Peso newPeso;
   double fract, whole;

   // Extract whole and fractional parts
   fract = modf(x, &whole);
   fract *= 100;
   fract = round(fract);

   // Whole part
   newPeso.setWholeVal(static_cast<unsigned>(whole));

   // Fractional part
   newPeso.setFractVal(static_cast<unsigned>(fract));

   // Update currency values
   newPeso.updateCurrencyVal();

   return newPeso;
}

std::ostream& operator << (std::ostream &out, const Peso &x)
{
   double currencyValue = x.getWholeVal() + x.getFractVal();
   out << to_string(currencyValue);
   return out;
}
std::istream& operator >> (std::istream &in, Peso &x)
{
   double fract,
       whole,
       val;

   in >> val;

   // Extract whole and fractional parts
   fract = modf(val, &whole);
   fract *= 100;
   fract = round(fract);

   Peso newPeso;

   x.setWholeVal(static_cast<unsigned>(whole)+x.getWholeVal());
   x.setFractVal(fract + x.getFractVal());
   x.updateCurrencyVal();
   return in;
}

Peso.h


#ifndef PESO_H
#define PESO_H

#include "Currency.h"

using namespace std;

//Peso class inherited form currency class
class Peso : public Currency
{
private:

public:

   //Constructors
   Peso(unsigned wV, unsigned fV) : Currency("Peso", "Centavo", wV, fV)
   {
   }

   Peso() : Currency("Peso", "Centavo")
   {
   }

   void updateCurrencyVal();

   // Overloaded Operators
   Peso operator + (const Peso &);
   Peso operator + (const double);
   Peso operator - (const Peso &);
   Peso operator - (const double);
   Peso operator = (const Peso &);
   Peso operator = (const double);

   friend std::ostream& operator << (std::ostream &, const Peso &);
   friend std::istream& operator >> (std::istream &, Peso &);
};

#endif


note: due to limited character i cant able to post all files.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions