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

Write a class called Snack. It should have data members for the name of a snack,

ID: 3835590 • Letter: W

Question

Write a class called Snack. It should have data members for the name of a snack, the price of the snack (double), and the number of calories in the snack (int). The class should have a constructor that takes three parameters and uses them to initialize the data members. It should also have a default constructor that initializes the data members to "bottled water", 1.75 and 0, respectively. The class should have get methods for all three data members.

Write a class called VendSlot. It should have two data members - a Snack, and the amount of that Snack that is currently in the slot. It should also have a default constructor that initializes the data members to a default Snack object and 5. The class should have a constructor that takes two parameters and uses them to initialize the data members. It should have get methods for both data members. It should have a method called decrementAmount that decreases the amount by 1.

Write a class called MiniVend, which represents a vending machine with four slots. It should have five data members: four VendSlots and the money in the machine. The class should have a constructor that takes 5 parameters - 4 VendSlot objects and the initial money. It should have a get method that returns the amount of money in the machine. It should have a method called numEmptySlots that returns the number of slots that have no Snacks left. It should have a method called valueOfSnacks that returns the total value of the Snacks in the machine. It should have a method called buySnack that takes as a parameter the number of the slot from which to purchase a Snack (from 0 to 3) - if that slot is empty, nothing should happen, otherwise the amount of that Snack should be decremented and the money increased buy the price of the Snack that was selected.

The functions should have the following names:

For the Snack class:

getName

getPrice

getNumCalories

For the VendSlot class:

getSnack

getAmount

decrementAmount

For the MiniVend class:

getMoney

numEmptySlots

valueOfSnacks

buySnack

For example, these classes could be used as follows:

The files must be named Snack.hpp, Snack.cpp, VendSlot.hpp, VendSlot.cpp, MiniVend.hpp, and MiniVend.cpp.

Explanation / Answer

The C++ files mentioned in the problem with given functionalities:


//File Name: Snack.hpp

#ifndef SNACK_HPP
#define SNACK_HPP
#include <string>
using namespace std;
class Snack
{
private:
   //private data members
   string name;
   double price ;
   int calr;
public:
   //default constructor
   Snack();
   //parameterized constructor
   Snack(string name, double price, int cal);
   //get methods for all three data ypes
   string getName();
   double getPrice();
   int getNumCalories();
};
#endif


//File Name: Snack.cpp

#include <iostream>
#include <string>
#include "snack.hpp"

using namespace std;

//default constructor
Snack::Snack()
{
name ="botteld water";
price = 1.75;
calr = 0;
}
//parameterized constructor
Snack::Snack(string name, double price, int cal)
{
name=name;
price =price;
calr=cal;
}

//Get methods for all variables
string Snack :: getName()
{
return name;
}

double Snack :: getPrice()
{
return price;
}

int Snack :: getNumCalories()
{
return calr;
}


//File Name: VendSlot.hpp

#ifndef VENDSLOT_HPP
#define VENDSLOT_HPP
#include <string>
#include "Snack.hpp"

using namespace std;
class VendSlot
{
private:
   Snack s;
   int snAmount;
  
//the public scope methods
public:
   VendSlot();
   VendSlot(Snack s1, int amd);
   Snack getSnack();
   int getAmount();
   int decrementAmount();
};

#endif
#endif


//File VendSlot.cpp

#include <iostream>
#include "VendSlot.hpp"
using namespace std;

VendSlot :: VendSlot()
{
Snack s1;
s=s1;
snAmount =5;
}
VendSlot :: VendSlot(Snack s1,int amd)
{
s=s1;
snAmount=5;
}

Snack VendSlot :: getSnack()
{
Snack ss;
ss=s;
return ss;
}
int VendSlot :: getAmount()
{
return snAmount;
}
int VendSlot :: decrementAmount()
{
snAmount = snAmount -1;
return snAmount;
}

//File Name: MiniVend.hpp

#ifndef MINIVEND_H
#define MINIVEND_H
#include "VendSlot.hpp"

class MiniVend
{
private:
VendSlot vs[4];
double money;

//public functions
public:
MiniVend(VendSlot v1,VendSlot v2,VendSlot v3,VendSlot v4,double m);
int numEmptySlots();

double getMoney();
double valueOfSnacks();
void buySnack(int slo);
};

#endif


//file Name: MiniVend.cpp

#include <iostream>
using namespace std;
#include "MiniVend.hpp"

#include <iostream>
using namespace std;
#include "MiniVend.hpp"


MiniVend::
MiniVend(VendSlot v1,VendSlot v2,VendSlot v3,VendSlot v4,double m)
{
vs[0]=v1;
vs[1]=v2;
vs[2]=v3;
vs[3]=v4;
money=m;
}

double MiniVend::getMoney()
{
   return money;
}

int MiniVend :: numEmptySlots()
{
   int amd=0;
   for(int j=0;j<4;j++)
   {
       //if snack amount is 0
       if(vs[j].getAmount()==0)
       {
           amd++;
       }
   }
   return amd;
}

double MiniVend::valueOfSnacks()
{
   double val=0;
   for(int j=0;j<4;j++)
   {
       val=val+ vs[j].getAmount()*vs[j].getSnack().getPrice();

   }

   //return the price
   return val;
}


void MiniVend:: buySnack(int slo)
{
   if(vs[slo].getAmount()==0)
       cout<<"The slot is empty"<<endl;
   else
   {
       vs[slo].decrementAmount();
       money=money+vs[slo].getSnack().getPrice();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote