In: Computer Science
C++ Class
class Billfold
{
private:
int twenties{}, tens{}, fives{}, dollars{};
public:
/*
Step 1. (5 points)
In class Billfold, write two Billfold constructors:
default: set all bill counts to zero; do not allow the bill counts
to be uninitialed.
2nd: set all bill counts to non-negative initial values; use
parameters for:
twenty, ten, five, dollar.
IMPORTANT: prohibit negative values - if negative bill value
given,
use 0 for that bill instead. Accept other bills if >= 0.
Tip: you can combine both of these into one constructor using
default parameters
*/
Billfold();
Billfold(int, int, int, int);
/*
Step 9. (5 points)
To avoid redundant code and insure proper validation, modify
constructor(s)
as needed to call set() or modify().
*/
/*
Step 3. (5 points)
In class Billfold, write the definition for a new public member
function: total_value.
total_value returns the total value of all twenties, tens, fives
and dollars
in a Billfold object. For example: a billfold with 4 twenties, 3
tens,
2 fives and 1 dollar has a total value of
(4*20)+(3*10)+(2*5)+(1*1)=121 (dollars)
This is a getter / accessor, and should be marked const.
*/
int total_value() const;
/*
Step 5. (5 points)
In class Billfold, implement a mutator/setter called set() which
sets the
bill counts of all bills. Pre-validate any new bill counts; do not
allow any
bill count to be less than zero. If the ANY of the bill counts are
invalid
(negative), do not modify ANY bill counts. If the set() succeeds,
return true;
if the set() fails, return false.
*/
bool set(int, int, int, int);
/*
Step 6. (5 points)
In class Billfold, write a show() method that outputs the number of
each
bill in the billfold in one string, like this: "($20=4 $10=3 $5=2
$1=1)"
Do NOT output a newline. No endl or \n. Option: return a string so
the
caller can cout the string. Now class Billfold is not dependent on
iostream.
*/
void show() const;
/*
Step 8. (5 points)
In class Billfold, write a modify() method that modifies bill
counts
using a positive (increment), negative (decrement) or zero (keep
the same)
value. For ie:
billfold.modify(0, 0, 2, -3) This will: keep twenties, tens the
same;
increase fives by 2, and decrease dollars by 3.
IMPORTANT! Do not allow bill counts to become less than zero! In
modify,
if ANY bill will become negative, DO NOT MAKE ANY CHANGES.
If modify() succeeds, return true; if modify() fails, return
false
*/
bool modify(int, int, int, int);
};
template<typename T>
T get_input(const string &prompt, const string &err_msg="
invalid\n") {
while (true) {
cout<<prompt; T item{};
if ( cin>>item) return item;
cout<<err_msg; cin.clear(); cin.ignore(INT_MAX, '\n');
}
}
int main() {
cout << "COSC 1337 Billfold" << endl;
/*
Step 2. (5 points) (test Step 1)
In main, in one statement, declare a new Billfold object called
empty.
Use the default constructor.
In main, in one statement, declare a new Billfold object colled
billfold.
Initialize billfold with: 4 twenties, 3 tens, 2 fives, 1
dollar.
*/
/*
Step 4. (5 points) (test Step 3)
In main, write code to call the total_value method on the billfold
object.
Display the returned value, which is the total value of the dollars
in billfold.
Format the output so it displays in dollars and cents. Example:
$121.00
Hint: setprecision, fixed; cents will always be .00
*/
/*
Step 7. (5 points) (test Step 5 and Step 6)
In main, call set to change the values in billfold to: 8 twenties,
7 tens, 6 fives, 5 dollars.
In main, call show to display the contents of billfold (number of
each bill).
*/
/*
Step 10 (10 points) (Test Step 8)
In main, write a menu driven loop that allows the user the add or
remove bills
from the billfold object.
In a loop, display the total value, value and options. Use q to
quit. For example:
$265.00 ($20=8 $10=7 $5=6 $1=5) Modify d)ollar f)ive t)en t(w)enty
q)uit: d
+/-dollar: -3
$262.00 ($20=8 $10=7 $5=6 $1=2) Modify d)ollar f)ive t)en t(w)enty
q)uit: f
+/-five: 2
$272.00 ($20=8 $10=7 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty
q)uit: t
+/-ten: 4
$312.00 ($20=8 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty
q)uit: w
+/-twenty: -5
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty
q)uit: w
+/-twenty: -5
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty
q)uit: x
Invalid option, try again!
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty
q)uit: q
Use the provided get_input function to get the option letter and
the bill count.
Your user interface should look similar to example above.
Important: For user input, use get_input (provided). This
insures invalid numeric input
does not cause crash or infinite loop. If you cannot use get_input
as provided,
you can modify it; or use cin>>value. Avoid crash or infinite
loop if invalid input.
*/
/*
Step 11 (5 points, EXTRA CREDIT, only if you have time)
Create an array (or a vector) of 3 billfolds. No user input
necessary.
Construct billfold instances with these bill amounts:
billfold 1: 1 twenties; 1 tens; 1 fives; 1 dollars (value
$36.00)
billfold 2: 2 twenties; 4 tens; 6 fives; 8 dollars (value
$108.00)
billfold 3: 10 twenties; 10 tens; 10 fives; 10 dollars (value
$360.00)
Write a loop that displays the billfold number and amount;
and the total of all billfolds, to appear as follows:
billfold 1 $36.00
billfold 2 $108.00
billfold 3 $360.00
total: $504.00
*/
cout<<endl<<"Goodbye!"<<endl; // this should
appear as the last line of your output
return EXIT_SUCCESS; // Use return 0 if EXIT_SUCCESS is
undefined
}
/*
Step 12 (5 points) (Tests all Steps. NOT extra credit, this is
expected.)
Test your code as described above and paste the output at the
bottom.
*/
//C++ CODE TO COPY//
#include<iostream>
#include<iomanip>
using namespace std;
class Billfold
{
private:
int twenties{}, tens{}, fives{}, dollars{};
public:
/*
Step 1. (5 points)
In class Billfold, write two Billfold
constructors:
default: set all bill counts to zero; do not allow the
bill counts to be uninitialed.*/
/*
2nd: set all bill counts to non-negative initial
values; use parameters for:
twenty, ten, five, dollar.
IMPORTANT: prohibit negative values - if negative bill
value given,
use 0 for that bill instead. Accept other bills if
>= 0.
Tip: you can combine both of these into one
constructor using default parameters
*/
Billfold(int twen=0, int tens=0, int five=0, int
dollars=0)
{
this->twenties = twen;
this->tens = tens;
this->fives = five;
this->dollars = dollars;
}
/*
Step 9. (5 points)
To avoid redundant code and insure proper validation,
modify constructor(s)
as needed to call set() or modify().
*/
/*
Step 3. (5 points)
In class Billfold, write the definition for a new
public member function: total_value.
total_value returns the total value of all twenties,
tens, fives and dollars
in a Billfold object. For example: a billfold with 4
twenties, 3 tens,
2 fives and 1 dollar has a total value of
(4*20)+(3*10)+(2*5)+(1*1)=121 (dollars)
This is a getter / accessor, and should be marked
const.
*/
const int total_value()
{
int total = (this->twenties *
20) + (this->tens * 10) +
(this->fives
* 5) + (dollars * 1);
return total;
}
/*
Step 5. (5 points)
In class Billfold, implement a mutator/setter called
set() which sets the
bill counts of all bills. Pre-validate any new bill
counts; do not allow any
bill count to be less than zero. If the ANY of the
bill counts are invalid
(negative), do not modify ANY bill counts. If the
set() succeeds, return true;
if the set() fails, return false.
*/
bool set(int twen, int tens, int five, int
dollars)
{
if (twen < 0 || tens < 0 ||
five < 0 || dollars < 0)
return
false;
this->twenties = twen;
this->tens = tens;
this->fives = five;
this->dollars = dollars;
return true;
}
/*
Step 6. (5 points)
In class Billfold, write a show() method that outputs
the number of each
bill in the billfold in one string, like this: "($20=4
$10=3 $5=2 $1=1)"
Do NOT output a newline. No endl or \n. Option: return
a string so the
caller can cout the string. Now class Billfold is not
dependent on iostream.
*/
const void show()
{
cout << "( $20 = " <<
this->twenties << " $10 = " << this->tens
<<
" $5 = "
<< this->fives << " $1 = " << this->dollars
<< " )";
}
/*
Step 8. (5 points)
In class Billfold, write a modify() method that
modifies bill counts
using a positive (increment), negative (decrement) or
zero (keep the same)
value. For ie:
billfold.modify(0, 0, 2, -3) This will: keep twenties,
tens the same;
increase fives by 2, and decrease dollars by 3.
IMPORTANT! Do not allow bill counts to become less
than zero! In modify,
if ANY bill will become negative, DO NOT MAKE ANY
CHANGES.
If modify() succeeds, return true; if modify() fails,
return false
*/
bool modify(int twen, int tens, int five, int
dollars)
{
if (twen < 0)
if (this->twenties - twen <
0)
return
false;
if (tens < 0)
if (this->tens - tens <
0)
return
false;
if (five < 0)
if (this->fives - five <
0)
return
false;
if (dollars < 0)
if (this->dollars - dollars <
0)
return
false;
this->twenties =
this->twenties + twen;
this->tens = this->tens +
tens;
this->fives = this->fives +
five;
this->dollars = this->dollars
+ dollars;
return true;
}
};
template<typename T>
T get_input(const string &prompt, const string &err_msg = "
invalid\n") {
while (true) {
cout << prompt; T
item{};
if (cin >> item) return
item;
cout << err_msg; cin.clear();
cin.ignore(INT_MAX, '\n');
}
}
int main() {
cout << "COSC 1337 Billfold" << endl;
/*
Step 2. (5 points) (test Step 1)
In main, in one statement, declare a new Billfold
object called empty.
Use the default constructor.*/
Billfold billfold();
/*
In main, in one statement, declare a new Billfold
object colled billfold.
Initialize billfold with: 4 twenties, 3 tens, 2 fives,
1 dollar.
*/
Billfold *billfold2 = new Billfold(4, 3, 2, 1);
/*
Step 4. (5 points) (test Step 3)
In main, write code to call the total_value method on
the billfold object.
Display the returned value, which is the total value
of the dollars in billfold.
Format the output so it displays in dollars and cents.
Example: $121.00
Hint: setprecision, fixed; cents will always be
.00
*/
int total = billfold2->total_value();
cout << std::fixed;
cout << std::setprecision(2);
cout << "$" << total;
/*
Step 7. (5 points) (test Step 5 and Step 6)
In main, call set to change the values in billfold to:
8 twenties, 7 tens, 6 fives, 5 dollars.
*/
billfold2->set(8, 7, 6, 5);
/*
In main, call show to display the contents of billfold
(number of each bill).
*/
billfold2->show();
/*
Step 10 (10 points) (Test Step 8)
In main, write a menu driven loop that allows the user
the add or remove bills
from the billfold object.
In a loop, display the total value, value and options.
Use q to quit. For example:
$265.00 ($20=8 $10=7 $5=6 $1=5) Modify d)ollar f)ive
t)en t(w)enty q)uit: d
+/-dollar: -3
$262.00 ($20=8 $10=7 $5=6 $1=2) Modify d)ollar f)ive
t)en t(w)enty q)uit: f
+/-five: 2
$272.00 ($20=8 $10=7 $5=8 $1=2) Modify d)ollar f)ive
t)en t(w)enty q)uit: t
+/-ten: 4
$312.00 ($20=8 $10=11 $5=8 $1=2) Modify d)ollar f)ive
t)en t(w)enty q)uit: w
+/-twenty: -5
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive
t)en t(w)enty q)uit: w
+/-twenty: -5
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive
t)en t(w)enty q)uit: x
Invalid option, try again!
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive
t)en t(w)enty q)uit: q
Use the provided get_input function to get the
option letter and the bill count.
Your user interface should look similar to example
above.
Important: For user input, use get_input
(provided). This insures invalid numeric input
does not cause crash or infinite loop. If you cannot
use get_input as provided,
you can modify it; or use cin>>value. Avoid
crash or infinite loop if invalid input.
*/
char choice;
int bill;
while (true)
{
cout << "\n" <<
billfold2->total_value() << "$ ";
billfold2->show();
cout << " Modify d)ollar
f)ive t)en t(w)enty q)uit: ";
cin >> choice;
switch (choice)
{
case 'd':
cout <<
"\n+/-dollar: ";
cin >>
bill;
billfold2->modify(0, 0, 0, bill);
break;
case 'f':
cout <<
"\n+/-dollar: ";
cin >>
bill;
billfold2->modify(0, 0, bill, 0);
break;
case 't':
cout <<
"\n+/-dollar: ";
cin >>
bill;
billfold2->modify(0, bill, 0, 0);
break;
case 'w':
cout <<
"\n+/-dollar: ";
cin >>
bill;
billfold2->modify(bill, 0, 0, 0);
break;
case 'q':
return 0;
break;
default:
cout <<
"\nInvalid option, try again!";
break;
}
}
}
//PROGRAM OUTPUT//
//COMMENT DOWN FOR ANY QUERIES...
//HIT A THUMBS UP IF YOU DO LIKE IT!!!