C++(Wallet) flashdrive Following the class diagram shown below, create the class
ID: 3597336 • Letter: C
Question
C++(Wallet) flashdrive
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.
Your submission should follow this organization:
main.cpp this is your driver file
wallet.h
wallet.cpp
Wallet
Sample Driver Code
Sample Driver Output
Wallet
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 my_Dollars;
int my_Cents;
Sample Driver Code
Wallet w;cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
w.visitATMForCash( 50 );
cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
if (w.canPayFor( 25, 35 )) {
// you have enough to pay for it...
w.payFor( 25, 35 );
cout << "buying $25.35 worth..." << endl;
}
cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
// this time you won't have enough money
if (w.canPayFor( 25, 35 )) {
// you have enough to pay for it...
w.payFor( 25, 35 );
cout << "buying $25.35 worth..." << endl;
}
cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
Sample Driver Output
d: 0 c: 0d: 50 c: 0
buying $25.35 worth...
d: 24 c: 65
d: 24 c: 65
Explanation / Answer
PLEASE REFER BELOW CODE
1) wallet.h
#include<iostream>
#include<cmath>
//class decleration for Wallet
using namespace std;
class Wallet
{
private:
int my_Dollars;
int my_Cents;
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 );
};
2) wallet.cpp
#include "wallet.h"
//constructor for class Wallet
Wallet::Wallet()
{
my_Dollars = 0;
my_Cents =0;
}
//constructor for class Wallet with parameters
Wallet::Wallet(int d,int c)
{
my_Dollars = d;
my_Cents = c;
}
//return dollar amount
int Wallet::getDollars()
{
return my_Dollars;
}
//return cents
int Wallet::getCents()
{
return my_Cents;
}
//chacking if amount we can pay or not
bool Wallet::canPayFor(int dollarAmount, int centsAmount)
{
float current_amt = my_Dollars + my_Cents/100.0; //converting current amount in dollars and cents
float required_amt = dollarAmount + centsAmount/100.0; //converting required amount in dollars and cents
if(required_amt < current_amt) //if required amount is not sufficient then we have to return false else true
return true;
else
return false;
}
//paying required amount
void Wallet::payFor(int dollarAmount, int changeAmount)
{
float current_amt = my_Dollars + my_Cents/100.0;
float required_amt = dollarAmount + changeAmount/100.0;
//separating cents from amount and assigning value to my_Cents
float amt = current_amt - required_amt;
int b = amt;
float a = amt - b;
my_Dollars = (int)amt;
my_Cents = ceil(a * 100); //multiplying cents by 100 to convert it into integer
}
//adding amount to wallet
void Wallet::visitATMForCash(int dollarAmount)
{
my_Dollars += dollarAmount;
}
3) driver.cpp
#include "wallet.h"
int main()
{
Wallet w;
cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
w.visitATMForCash( 50 );
cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
if (w.canPayFor( 25, 35 )) {
// you have enough to pay for it...
w.payFor( 25, 35 );
cout << "buying $25.35 worth..." << endl;
}
cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
// this time you won't have enough money
if (w.canPayFor( 25, 35 )) {
// you have enough to pay for it...
w.payFor( 25, 35 );
cout << "buying $25.35 worth..." << endl;
}
cout << "d:" << w.getDollars( );
cout << " c:" << w.getCents( ) << endl;
return 0;
}
PLEASE REFER BELOW OUTPUT
d:0 c:0
d:50 c:0
buying $25.35 worth...
d:24 c:65
d:24 c:65
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.