Question

Project 1 - Wallet Following the class diagram shown below, create the class Wallet. A Wallet...

Project 1 - Wallet

Following the class diagram shown below, create the class Wallet. A Wallet represents a carrier of bills and coins. You use your wallet to pay for things, which reduces the balance held inside. When you visit an ATM, you can fill it back up with cash again. A sample driver for this class is shown below. You should probably make a more thorough driver to test your class better. I will have my own driver which will aim to exploit any possible edge cases.

Wallet Class Description

Wallet( );
Wallet( int d, int c );

int getDollars( );
int getCents( );
bool canPayFor( int dollarAmount, int centsAmount );
void payFor( int dollarAmount, int changeAmount );
void visitATMForCash( int dollarAmount );

int dollars;
int cents;

Wallet Sample Driver Code

int atm = 0, d = 0, c = 0, x = 0, y = 0;
char cont = 'y';


cout << "Hello, how much money do you have in your wallet?"
<< endl;
cout << "Dollars: ";
cin >> d;
cout << "Cents: ";
cin >> c;
cout << endl;

Wallet w(d, c);

cout << "How many dollars would you like to withdraw from the
ATM? ";
cin >> atm;

w.visitATMForCash(atm);

cout << "Currently you have: " << endl;
cout << "Dollars: " << w.getDollars() << endl
<< "Cents: " << w.getCents() << endl <<
endl;


do {
  cout << "How much are you going to spend?" << endl;
  cout << "Dollars: ";
  cin >> x;
  cout << "Cents: ";
  cin >> y;

  if (w.canPayFor(x, y) == true) {
    w.payFor(x, y);
    cout << "\nYou now have: " << endl;
    cout << "Dollars:" << w.getDollars() << endl
<< "Cents:" << w.getCents() << endl;
  }
  else {
    cout << "\nSorry, you do not have enough money to spend.
Please spend less." << endl;
  }

  cout << "Are you spending more money (y/n)? ";
  cin >> cont;
  cout << endl;
} while (cont == 'y');

cout << "You have this amount left in your wallet: " <<
endl;
cout << "Dollars: " << w.getDollars() << " Cents:
" << w.getCents() << endl << endl;

Sample Driver Output

Hello, how much money do you have in your wallet?
Dollars:  10
Cents:  0

How many dollars would you like to withdraw from the ATM?  5
Currently you have:
Dollars: 15
Cents: 0

How much are you going to spend?
Dollars:  6
Cents:  56

You now have:
Dollars: 8
Cents: 44
Are you spending more money (y/n)?  y

How much are you going to spend?
Dollars:  10
Cents:  0

Sorry, you do not have enough money to spend. Please spend less.
Are you spending more money (y/n)?  n

You have this amount left in your wallet:
Dollars: 8 Cents: 44
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Working code implemented in C++ and appropriate comments provided for better understanding:

Here I am attaching code for these 3 files:

  • main.cpp
  • wallet.cpp
  • wallet.h

Source code for main.cpp:

#include "wallet.h"
#include <iostream>

using namespace std;

int main() {
int atm = 0, d = 0, c = 0, x = 0, y = 0;
char cont = 'y';

cout << "Hello, how much money do you have in your wallet?" << endl;
cout << "Dollars: ";
cin >> d;
cout << "Cents: ";
cin >> c;
cout << endl;

Wallet w(d, c);

cout << "How many dollars would you like to withdraw from the ATM? ";
cin >> atm;
w.visitATMForCash(atm);
cout << "Currently you have: " << endl;
cout << "Dollars: " << w.getDollars() << endl
<< "Cents: " << w.getCents() << endl
<< endl;

do {
cout << "How much are you going to spend?" << endl;
cout << "Dollars: ";
cin >> x;
cout << "Cents: ";
cin >> y;
if (w.canPayFor(x, y) == true) {
w.payFor(x, y);
cout << "\nYou now have: " << endl;
cout << "Dollars: " << w.getDollars() << endl
<< "Cents: " << w.getCents() << endl;
} else {
cout << "\nSorry, you do not have enough money to spend. Please spend "
"less."
<< endl;
}

cout << "Are you spending more money (y/n)? ";
cin >> cont;
cout << endl;
} while (cont == 'y');

cout << "You have this amount left in your wallet:" << endl;
cout << "Dollars: " << w.getDollars() << " Cents: " << w.getCents() << endl
<< endl;
return 0;
}

Code Screenshots:

#include wallet.h #include <iostream> using namespace std; int main() { int atm - 0, d = 0, c = 0, x = 0, y = 0; char cont

Source code for wallet.cpp:

#include "wallet.h"

Wallet::Wallet()
{
dollars = 0;
cents = 0;
}

Wallet::Wallet(int d, int c)
{
dollars = d;
cents = c;
}

int Wallet::getDollars()
{
return dollars;
}

int Wallet::getCents()
{
return cents;
}

bool Wallet::canPayFor(int dollarAmount, int centsAmount)
{
if (dollarAmount > dollars)
{
return false;
}
else if (dollarAmount == dollars && centsAmount > cents)
{
return false;
}
else
{
return true;
}
}

void Wallet::payFor(int dollarAmount, int centsAmount)
{
dollars -= dollarAmount;
cents -= centsAmount;
if (cents < 0)
{
cents = 100 + cents;
dollars--;
}
}

void Wallet::visitATMForCash(int dollarAmount)
{
dollars += dollarAmount;
}

Code Screenshots:

#include wallet.h Wallet::Wallet() { dollars = 0; cents = 0; ہا Wallet::Wallet(int d, int c) { dollars = d; cents = c; ہا i

Source code for wallet.h:

class Wallet
{
public:
Wallet();
Wallet(int d, int c);

int getDollars();
int getCents();
bool canPayFor(int dollarAmount, int centsAmount);
void payFor(int dollarAmount, int changeAmount);
void visitATMForCash(int dollarAmount);

private:
int dollars;
int cents;
};

Code Screenshots:

class Wallet { public: Wallet(); Wallet(int d, int c); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int getDollars(); int getCents(

Output Screenshots:

> clang++-7 -pthread -std=c++17 -o main main.cpp wallet.cpp > ./main Hello, how much money do you have in your wallet? Dollar

Hope it helps, if you like the answer give it a thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
Project 1 - Wallet Following the class diagram shown below, create the class Wallet. A Wallet...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

    C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information.   Optional bonus (10%): Do some research on make...

  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

  • Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which...

    Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which has been diagrammed below and described in the online course content. For this unit, I'd like you to identify additional member variables (data) that would be appropriate for the class FlashDrive. I would also like you to identify additional methods (functions) that would be appropriate for the class FlashDrive. Submit a .h file for FlashDrive with the new possible methods and members you have...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • LW: Class Constructors Objectives Write a default constructor for a class. Note that this const...

    LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...

  • 1. in this programe i want add some products to be shown after choosing choise (...

    1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • // P13_2.cpp - This program adds money of two different people #include<iostream> #include<cstdlib> using namespace std;...

    // P13_2.cpp - This program adds money of two different people #include<iostream> #include<cstdlib> using namespace std; class AltMoney {     public:         AltMoney();         AltMoney(int d, int c);         friend void add(AltMoney m1, AltMoney m2, AltMoney& sum);         void display_money( );     private:         int dollars;         int cents; }; void read_money(int& d, int& c); int main( ) {      int d, c;      AltMoney m1, m2, sum;      sum = AltMoney(0,0);      read_money(d, c);      m1 = AltMoney(d,c);      cout...

  • In c++ Please. Now take your Project 4 and modify it.  You’re going to add another class...

    In c++ Please. Now take your Project 4 and modify it.  You’re going to add another class for getting the users name. Inherit it. Be sure to have a print function in the base class that you can use and redefine in the derived class. You’re going to also split the project into three files.  One (.h) file for the class definitions, both of them.  The class implementation file which has the member function definitions. And the main project file where your main...

  • C++ Check Book Program I need to have a checkbook program that uses the following items....

    C++ Check Book Program I need to have a checkbook program that uses the following items. My code that I have appears below. 1) Math operations using built-in math functions 2) Class type in a separate .h file with member functions (accessor functions, get, set, show, display find, etc). There needs to be an overloaded function base/derived classes or a template. 3) Binary File #include <iostream> using namespace std; int main() {     double checking,savings;     cout<<"Enter the initial checking...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT