Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PF1 Lab9 - MoneyBag! For this lab, you will create a virtual MoneyBag that can b

ID: 3720064 • Letter: P

Question

 PF1 Lab9 - MoneyBag!   For this lab, you will create a virtual MoneyBag that can be used to store change (no paper money).  You will provide methods to create/initialize the MoneyBags, compare two MoneyBags, add change to a MoneyBag, dump the contents of one MoneyBag to another, and make a purchase using a MoneyBag (subtracting the purchase price from a MoneyBag).  Instead of a lengthy description for how the methods should function, I am going to provide you with a test program that will be the "customer" for your class.  This will allow you  to determine what methods/etc you need to provide.  I'm also providing you with the output that this test program produced when I ran it with my MoneyBag class.  You need to CAREFULLY STUDY the output and the test program to see how the methods should function.  This is a logic problem in itself!  One hint is to notice that the subtract method, which is used to make a purchase of the specified amount should always return a MoneyBag with the fewest amount of coins in it.  In other words, you hand a clerk your moneybag, and he/she will "optimize" your change by returning it with the SMALLEST number of coins possible! So provided that your MoneyBag contains enough money to cover the price of the purchase, the composition of the change in the MoneyBag will not affect the composition of the change returned, which will always contain the fewest coins possible.  Your lab9 source code should be in the directory ~/PF1/lab9, and should include the main method provided below.  Note:  Your program should work for other sets of test data.  NOTE2:  DO NOT MODIFY THE CODE I PROVIDE FOR YOU!!!!!!  = TEST PROGRAM ====================================================== public class Lab9 {     public static void main(String args[])     {         MoneyBag mb0 = new MoneyBag();         MoneyBag mb1 = new MoneyBag(1, 2, 3, 4);         int value;          System.out.println("mb0:");         mb0.printContents();         value = mb0.getValue();         System.out.println("value = " + value + " ");          System.out.println("mb1:");         mb1.printContents();         value = mb1.getValue();         System.out.println("value = " + value + " ");          System.out.println("adding $0.50 to mb0");         mb0.add(2, 0, 0, 0);           System.out.println("mb0:");         mb0.printContents();         value = mb0.getValue();         System.out.println("value = " + value + " ");          System.out.print("Comparing mb0 and mb1: ");         mb0.compare(mb1);          System.out.println(" Adding $0.14 to mb0");         mb0.add(0, 0, 2, 4);           System.out.println("mb0:");         mb0.printContents();         value = mb0.getValue();         System.out.println("value = " + value + " ");          System.out.print("Comparing mb0 and mb1: ");         mb0.compare(mb1);          System.out.println(" Adding to mb0");         mb0.add(0, 0, 0, 1);           System.out.println("mb0:");         mb0.printContents();         value = mb0.getValue();         System.out.println("value = " + value + " ");          System.out.print("Comparing mb0 and mb1: ");         mb0.compare(mb1);          System.out.println(" Adding contents of mb1 into mb0");         mb0.add(mb1);          System.out.println("mb0:");         mb0.printContents();         value = mb0.getValue();         System.out.println("value = " + value + " ");         System.out.println("mb1:");         mb1.printContents();         value = mb1.getValue();         System.out.println("value = " + value + " ");          System.out.println("Creating a new MoneyBag");         MoneyBag mb2 = new MoneyBag(5, 4, 3, 2);         System.out.println("mb2:");         mb2.printContents();         value = mb2.getValue();         System.out.println("value = " + value + " ");          System.out.println("Subtracting $5.00");         mb2 = mb2.subtract(500);         System.out.println("mb2:");         mb2.printContents();         value = mb2.getValue();         System.out.println("value = " + value + " ");          System.out.println("Oops, let's try $0.50");         mb2 = mb2.subtract(50);         System.out.println("Change due:");         System.out.println("mb2:");         mb2.printContents();         value = mb2.getValue();         System.out.println("value = " + value + " ");          System.out.println("Subtracting $0.41");         mb2 = mb2.subtract(41);         System.out.println("Change due:");         System.out.println("mb2:");         mb2.printContents();         value = mb2.getValue();         System.out.println("value = " + value + " ");     } } = OUTPUT ============================================================ mb0: quarters = 0, dimes  = 0, nickels = 0, pennies = 0 value = 0  mb1: quarters = 1, dimes  = 2, nickels = 3, pennies = 4 value = 64  adding $0.50 to mb0 mb0: quarters = 2, dimes  = 0, nickels = 0, pennies = 0 value = 50  Comparing mb0 and mb1: IS LESS THAN  Adding $0.14 to mb0 mb0: quarters = 2, dimes  = 0, nickels = 2, pennies = 4 value = 64  Comparing mb0 and mb1: IS EQUAL TO  Adding to mb0 mb0: quarters = 2, dimes  = 0, nickels = 2, pennies = 5 value = 65  Comparing mb0 and mb1: IS GREATER THAN  Adding contents of mb1 into mb0 mb0: quarters = 3, dimes  = 2, nickels = 5, pennies = 9 value = 129  mb1: quarters = 0, dimes  = 0, nickels = 0, pennies = 0 value = 0  Creating a new MoneyBag mb2: quarters = 5, dimes  = 4, nickels = 3, pennies = 2 value = 182  Subtracting $5.00 Insufficient funds - purchase denied. mb2: quarters = 5, dimes  = 4, nickels = 3, pennies = 2 value = 182  Oops, let's try $0.50 Change due: mb2: quarters = 5, dimes  = 0, nickels = 1, pennies = 2 value = 132  Subtracting $0.41 Change due: mb2: quarters = 3, dimes  = 1, nickels = 1, pennies = 1 value = 91 

Explanation / Answer

lass MoneyBag
{
private:
int Quarters,dimes,nickels,pennies;
public:
MoneyBag(int Q, int D, int N, int P)
{
  Quarters = Q;
  dimes = D;
  nickels = N;
  pennies = P;
};

void printContents(void);
int GetValue();
void compare(MoneyBag M);
void Add(int Q, int D, int N, int P);
MoneyBag subtract(int Val);
}

MoneyBag:printContents()
{
cout<<"Quarters = "<<Quarters<<"Dimes = "<<dimes<<"Nickels = "<<nickels<<"pennies = "<<pennies;
}

int MoneyBag:GetValue()
{
return((Quarters*25) + (dimes * 10) + (nickels * 5) + (pennies));
}

void MoneyBag:compare(MoneyBag M)
{
int B0, B1;

B0 = ((this.Quarters*25) + (this.dimes * 10) + (this.nickels * 5) + (this.pennies));
B1 = ((M.Quarters*25) + (M.dimes * 10) + (M.nickels * 5) + (M.pennies));

if(B0>B1)
cout<< "Greater Then";
elseif(B0<B1)
cout<<"Less Then";
elseif(B0 == B1)
cout<<"Equals";
}

MoneyBag:Add(int Q, int D, int N, int P)
{
Quarters += Q;
dimes+= D;
nickels += N;
pennies += P;
}


MoneyBag:subtract(int Val)
{
int total,Q,D,N,P;

total = ((Quarters*25) + (dimes * 10) + (nickels * 5) + (pennies));

if(Val > total)
{
  cout<<"No sufficient Balance in MoneyBag";
  return;
}

Q = Val/25;

if(Quarters >= Q)
{
  Quarters -= Q;
  val = val - (Q * 25);
}
else
{
  while(Quarters < Q)
   Q--;

  if(Quarters >= Q)
  {
   Quarters -= Q;
   val = val - (Q * 25);
  }

}

D = Val/10;

if(dimes >= D)
{
  dimes -= D;
  val = val - (D * 10);
}
else
{
  while(dimes < D)
   Q--;

  if(dimes >= D)
  {
   dimes -= D;
   val = val - (D * 10);
  }

}

N = Val/5;

if(nickels >= N)
{
  nickels -= N;
  val = val - (N * 5);
}
else
{
  while(nickels < N)
   Q--;

  if(nickels >= N)
  {
   nickels -= N;
   val = val - (N * 5);
  }

}

P = Val;

if(pennies >= P)
{
  pennies -= P;
  val = val - p;
}

}