Question

In: Computer Science

C++ Class class Billfold { private: int twenties{}, tens{}, fives{}, dollars{}; public: /* Step 1. (5...

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.
*/

Solutions

Expert Solution

//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!!!


Related Solutions

public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand  ...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below: Line Number: 5 Error description: Missing semicolon...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5;...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5; // initial array size    //DO NOT CHANGE this, it is set to 5 to make sure your code    //will pass all the tests and works with no issue.       // add all data fields which are required    /**    * ArrayBasedDeque() constructs an empty deque.    */    public ArrayBasedDeque(){        //complete    }       /**    *...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT