Piggybank Homework help

Pages: 12
I have read through the description and solved the problem accordingly. I hope the fact I post a solution to your homework instead of giving hints will not be a problem.
This required the following:

1. Main.cpp is exaclty as-is provided in your first post, so that the assignment is not failed.
This contains the main function (Driver Program) which is
used to test your class. It will be provided to you. DO NOT modify this
main function.
They want to pass amount in cents and not dollars to the removeCoins() function- they have it. Although I believe it is an unneccesary complication, we can easily adapt.

2. .h files are also as provided in first post.
Coins.h – This contains the class definition for the Coins class and each of
the classes that are derived from Coins. This file will be provided for you.
PiggyBank.h - This contains the class definition for the PiggyBank class.
This file will be provided for you.
This means no protected class members. It has to be done in a different way - with initializer list. Also, no friend functions or classes.

3. I included <string> and <iomanip> headers to make printing functions work. I hope it is not against any policy in your course. Those are included in .cpp files, so it is legal.

4. The removeCoins() function is supposed to work in a way that would rather give too much money than to little, as output and description suggest:
Remove coins from the PiggyBank getting the least number of
coins needed to fulfill the request. The coins are not removed
when the total value in the bank is less than the requested amount
which is given in cents. Note: you may have to remove more
money than the requested amount based on which coins are in
the bank.
In my previous post I called this an option B. For this purpose, between while loops you can see ifs, which are to verify if in any point there is a need to withdraw more than requested.

5. The description only mentions one constructor for coins class - with two arguments and default values set to zero - Coins (int = 0, int = 0). I believe that the fact that in your first post you put a parameterless constructor in the class is an error.

I will post the coins.cpp and piggybank.cpp files contents for you to review. The output I get from this is identical to the model one you put in one of the first post. Note that, you posted it twice, and the second one is not correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************\
 *                              *
 *         COINS.CPP            *
 *                              *
 \******************************/
#include <iostream>
#include <cstdlib>
#include <string>  // to cout std::string in print function
#include <iomanip> // setprecision and fixed in print function

#include "Coins.h"
using namespace std;

const double Pennies::VALUE     = 0.01;
const double Nickels::VALUE     = 0.05;
const double Dimes::VALUE       = 0.10;
const double Quarters::VALUE    = 0.25;

const double Pennies::WEIGHT     = 2.500;
const double Nickels::WEIGHT     = 5.000;
const double Dimes::WEIGHT       = 2.268;
const double Quarters::WEIGHT    = 5.670;

/* ***
 * Constructors
 */
// COINS
Coins::Coins(int number = 0, int type = 0)
{
	Coins::number = number;
	Coins::type = type;
	switch (type)
	{
	case PENNY:
		value = Pennies::VALUE * number;
		weight = Pennies::WEIGHT * number;
		break;
	case NICKEL:
		value = Nickels::VALUE * number;
		weight = Nickels::WEIGHT * number;
		break;
	case DIME:
		value = Dimes::VALUE * number;
		weight = Dimes::WEIGHT * number;
		break;
	case QUARTER:
		value = Quarters::VALUE * number;
		weight = Quarters::WEIGHT * number;
		break;
	default:
		break;
	}
}
// SUBCOINS
// Construcors have no bodies "{}"
// but they use initializer list
Pennies::Pennies(int number) 
	: Coins(number, PENNY)	{};

Nickels::Nickels(int number) 
	: Coins(number, NICKEL)	{};

Dimes::Dimes(int number)
	: Coins(number, DIME) {};

Quarters::Quarters(int number)
	: Coins(number, QUARTER) {};

// ***
int Coins::getNumber()
{
	return number;
}
int Coins::getType()
{
	return type;
}
double Coins::getValue()
{
	return value;
}
double Coins::getWeight()
{
	return weight;
}
void Coins::print()
{
	string coinTypeName = "";
	switch(type)
	{
	case PENNY:
		coinTypeName = " pennies";
		break;
	case NICKEL:
		coinTypeName = " nickels";
		break;
	case DIME:
		coinTypeName = " dimes";
		break;
	case QUARTER:
		coinTypeName = " quarters";
		break;
	default:
		break;
	}
	cout << setprecision(2) << fixed << number << coinTypeName 
        << " ($" << value << ") " << "(" << weight << " grams)" << endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/********************************\
 *                              *
 *         PIGGYBANK.CPP        *
 *                              *
 \*******************************/
#include <iostream>
#include <cstdlib>
#include <iomanip> // setprecision and fixed in print function

#include "PiggyBank.h"
#include "Coins.h"
using namespace std;

PiggyBank::PiggyBank()
{
	numPennies = 0;
	numNickels = 0;
	numDimes = 0;
	numQuarters = 0;
	value = 0.0;
	weight = 0.0;
}

void PiggyBank::addCoins (Coins& c) 
{
	if(weight  + c.getWeight() < THREE_LBS) { 
		switch(c.getType()) 
		{
		case(PENNY):
			numPennies += c.getNumber();
			break;
		case(NICKEL):
			numNickels+= c.getNumber();
			break;
		case(DIME):
			numDimes+= c.getNumber();
			break;
		case(QUARTER):
			numQuarters+= c.getNumber();
			break;
		default:
			break;
		}
		value += c.getValue();
		weight += c.getWeight();
	}
	else {
		cout << "Piggy bank weight exceeded! No coins added." << endl << endl;
	}
}

void PiggyBank::removeCoins (int amount) 
{
	int valueOfWithdrawal = 0;	// in pennies
	double weightOfWithdrawal = 0.0;
	int penniesToRemove = 0;
	int nickelsToRemove = 0;
	int dimesToRemove = 0;
	int quartersToRemove = 0;

	if ( (amount * 0.01) <= value)
	{
		int stillRequiredAmount = amount;
		int smallerCoinsValue = numQuarters * QUARTER +
								numDimes * DIME + 
								numNickels * NICKEL +
								numPennies * PENNY;;
		
		// How many quarters to withdraw?
		while (valueOfWithdrawal + QUARTER < amount 
			&& numQuarters - quartersToRemove > 0)
		{
			quartersToRemove++;
			valueOfWithdrawal += QUARTER;
		}
		
		stillRequiredAmount = (amount - valueOfWithdrawal);
		smallerCoinsValue -= numQuarters * QUARTER; 
		
		if( numQuarters - quartersToRemove > 0 
			&& smallerCoinsValue  < stillRequiredAmount )
		{
			quartersToRemove++;
			valueOfWithdrawal += QUARTER;
		}

		// How many dimes to withdraw?
		while (valueOfWithdrawal + DIME < amount 
			&& numDimes - dimesToRemove > 0)
		{
			dimesToRemove++;
			valueOfWithdrawal += DIME;
		}
		stillRequiredAmount = (amount - valueOfWithdrawal);
		smallerCoinsValue -= numDimes * DIME;

		if(numDimes - dimesToRemove > 0 
			&& smallerCoinsValue  < stillRequiredAmount )
		{
			dimesToRemove++;
			valueOfWithdrawal += DIME;
		}

		// How many Nickels to withdraw?
		while (valueOfWithdrawal + NICKEL < amount 
			&& numNickels - nickelsToRemove > 0)
		{
			nickelsToRemove++;
			valueOfWithdrawal += NICKEL;
		}
		
		stillRequiredAmount = (amount - valueOfWithdrawal);
		smallerCoinsValue -= numNickels * NICKEL;

		if(numNickels - dimesToRemove > 0 
			&& smallerCoinsValue  < stillRequiredAmount )
		{
			nickelsToRemove++;
			valueOfWithdrawal += NICKEL;
		}


		while (valueOfWithdrawal + PENNY < amount 
			&& numPennies - penniesToRemove > 0)
		{
			penniesToRemove++;
			valueOfWithdrawal += PENNY;
		}

		stillRequiredAmount = (amount - valueOfWithdrawal);
		smallerCoinsValue -= numPennies * PENNY;

		if(numPennies - penniesToRemove > 0 
			&& smallerCoinsValue  < stillRequiredAmount )
		{
			penniesToRemove++;
			valueOfWithdrawal += PENNY;
		}

		weightOfWithdrawal = penniesToRemove * Pennies::WEIGHT +
			nickelsToRemove * Nickels::WEIGHT + 
			dimesToRemove * Dimes::WEIGHT + 
			quartersToRemove * Quarters::WEIGHT;

		value -= (valueOfWithdrawal * 0.01);
		weight -= weightOfWithdrawal;
		numQuarters -= quartersToRemove;
		numDimes -= dimesToRemove;
		numNickels -= nickelsToRemove;
		numPennies -= penniesToRemove;

		cout << "Remove: " << endl << "  "
			<< penniesToRemove  << " pennies, " 
			<< nickelsToRemove  << " nickels, " 
			<< dimesToRemove 	 << " dimes, and " 
			<< quartersToRemove << " quarters." << endl; 
		cout << "  For a total of $" << setprecision(2) 
			<< fixed << (valueOfWithdrawal * 0.01) << endl << endl;
	}
	else
	{
		cout << "Piggy bank has insufficient funds to honor this request." 
			<< endl << endl;
	}
}
double PiggyBank::getValue()
{
	return value;
}
double PiggyBank::getWeight()
{
	return weight;
}
void PiggyBank::print()
{
	cout << endl 
		<< "Piggy bank now has: " << endl
		<< "  " << numPennies << " pennies, " << numNickels << " nickels, " 
		<< numDimes << " dimes, and " << numQuarters << " quarters." << endl;

	cout << "  The total worth is $" << setprecision(2) << fixed << value << endl
		<< "  with the coins weighing " << weight << " grams." << endl << endl;
}
Last edited on
O my goodness, thank you so much, you've been so patient and helpful, and I appreciate you helping me through this, you've been absolutely wonderful. Thank you again, and have a beautiful week, and a fantastic holiday season!!
Topic archived. No new replies allowed.
Pages: 12