In: Computer Science
Step 1: Extend the dispenserType class per the following specifications.
dispenserType sanitizer("hand sanitizer", 50, 100);
Step2: Define a new class, cashRegister. Start by creating cashRegister.h and cashRegister.cpp files. Be sure to update your CMakeLists.txt to add these new files. Your CMakeLists.txt CAN look like this
cmake_minimum_required(VERSION 3.15) project(Project2CS215) set(CMAKE_CXX_STANDARD 14) add_executable(Project2CS215 cashRegister.cpp cashRegister.h dispenserType.cpp dispenserType.h main.cpp)
The register has some cash on hand, it accepts the amount from the customer, and if the amount deposited is more than the cost of the item, then - if possible - it returns the change. For simplicity, assume that the user deposits the money greater than or equal to the cost of the product. The cash register should also be able to show to the candy machine's owner the amount of money in the register at any given time.
Public functions
Private member
Step3 - Lastly, add a function makeSale(). Next, see the function definition for makeSale() method in sellProduct function in main.cpp and understand the working of sellProduct(). When you are ready, go ahead and implement makeSale() function. Can you guess where does this function get added? Class dispenserType or cashRegister?
When a product is sold, the number of items in that dispenser is reduced by 1. Therefore, the function makeSale reduces the number of items in the dispenser by 1.
Files:
//main.cpp
#include <iostream>
#include "dispenserType.h"
#include "cashRegister.h"
using namespace std;
void showSelection();
void sellProduct(dispenserType&, cashRegister&);
int main() {
cashRegister counter(100);
dispenserType sanitizer("hand sanitizer", 50,
100);
dispenserType mask("mask", 85, 100) ;
dispenserType tissues("tissues", 25, 0);
dispenserType wipes("wipes", 100, 100);
int choice;
showSelection();
cin >> choice;
switch (choice) {
case 1:
sellProduct(sanitizer, counter);
break;
case 2:
sellProduct(mask, counter);
break;
case 3:
sellProduct(tissues, counter);
break;
case 4:
sellProduct(wipes, counter);
break;
default:
cout << "Invalid selection" << endl;
} // end switch
return 0;
} // end main
void showSelection() {
cout << "*** Welcome to SSU Vending
Machine ***" << endl;
cout << "To select an item, enter "
<< endl;
cout << "1 for Sanitizer" <<
endl;
cout << "2 for Mask" << endl;
cout << "3 for Tissues" <<
endl;
cout << "4 for Wipes" << endl;
cout << "9 to exit" << endl;
}//end showSelection
void sellProduct(dispenserType& product, cashRegister& pCounter){
int amount; //variable to hold the amount
entered
int amount2; //variable to hold the extra amount
needed
if (product.getNoOfItems() > 0) //if the
dispenser is not empty
{
cout << "Please
deposit " << product.getCost() << " cents" <<
endl;
cin >> amount;
if (amount <
product.getCost()) {
cout << "Please deposit another "
<< product.getCost()- amount << " cents" <<
endl;
cin >> amount2;
amount = amount + amount2;
}
if (amount >=
product.getCost()) {
pCounter.acceptAmount(amount);
product.makeSale();
cout << "Collect your item at the bottom and enjoy " <<
endl;
}
else
cout << "The amount is not enough. "
<< "Collect what you deposited." << endl;
cout <<
"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*";
}
else
cout << "Sorry,
this item is sold out." << endl;
} //end sellProduct
//dispenserType.cpp
#include "dispenserType.h"
dispenserType :: dispenserType() //degault constructor
{
setName("none");
setCost(0);
setNoOfItems(0);
}
void dispenserType :: setName(string productName) //set name of
product
{
name=productName;
}
void dispenserType :: setCost(int Cost) //set cost of
product
{
cost = Cost;
}
void dispenserType :: setNoOfItems(int itemStock) //set stock
{
numberOfItem = itemStock;
}
//dispenserType.h
#ifndef DISPENSERTYPE_H_INCLUDED
#define DISPENSERTYPE_H_INCLUDED
#include <string.h>
#include <iostream>
using namespace std;
class dispenserType
{
private:
int cost;
string name;
int numberOfItem;
public:
dispenserType(); //default constructor
void setName(string productName); //set name of product
void setCost(int Cost); //set cost of product
void setNoOfItems(int itemStock); //set stock of product
int getCost(){ return cost;} //get cost of item
int getNoOfItems(){ return numberOfItem;} //get stock
string getName(){ return name;} //get name
};
#endif // DISPENSERTYPE_H_INCLUDED
//cashRegister.cpp
#include "cashRegister.h"
//cashRegister.h
class cashRegister{
public:
private:
};
// dispenserType.h
#ifndef DISPENSERTYPE_H_INCLUDED
#define DISPENSERTYPE_H_INCLUDED
#include <string.h>
#include <iostream>
using namespace std;
class dispenserType
{
private:
int cost;
string name;
int numberOfItem;
public:
dispenserType(); //default constructor
dispenserType(string Name, int Cost, int NumOfItems); //
parameterized constructor
void setName(string productName); //set name of product
void setCost(int Cost); //set cost of product
void setNoOfItems(int itemStock); //set stock of product
int getCost(){ return cost;} //get cost of item
int getNoOfItems(){ return numberOfItem;} //get stock
string getName(){ return name;} //get name
void makeSale(); // decrement the quantity by 1
};
#endif // DISPENSERTYPE_H_INCLUDED
//end of dispenserType.h
// dispenserType.cpp
#include "dispenserType.h"
dispenserType :: dispenserType() //degault constructor
{
setName("none");
setCost(0);
setNoOfItems(0);
}
// initialize the name, cost and numberOfItem to specified
values
dispenserType::dispenserType(string Name, int Cost, int
NumOfItems)
{
setName(Name);
setCost(Cost);
setNoOfItems(NumOfItems);
}
void dispenserType :: setName(string productName) //set name of
product
{
name=productName;
}
void dispenserType :: setCost(int Cost) //set cost of
product
{
cost = Cost;
}
void dispenserType :: setNoOfItems(int itemStock) //set stock
{
numberOfItem = itemStock;
}
void dispenserType::makeSale()
{
numberOfItem--;
}
//end of dispenserType.cpp
// cashRegister.h
#ifndef CASHREGISTER_H_INCLUDED
#define CASHREGISTER_H_INCLUDED
class cashRegister
{
private:
int cashOnHand;
public:
cashRegister(int initialAmount=500); // default constructor with
default parameter
int getCurrentBalance(); // return the cash on hand
void acceptAmount(int amountInt); // add amountInt to
register
};
#endif // CASHREGISTER_H_INCLUDED
//end of cashRegister.h
// cashRegister.cpp
#include "cashRegister.h"
cashRegister::cashRegister(int initialAmount) :
cashOnHand(initialAmount)
{}
int cashRegister:: getCurrentBalance()
{
return cashOnHand;
}
void cashRegister::acceptAmount(int amountInt)
{
cashOnHand += amountInt; // add amountInt to cashOnHand
}
//end of cashRegister.cpp
// main.cpp
#include <iostream>
#include "dispenserType.h"
#include "cashRegister.h"
using namespace std;
void showSelection();
void sellProduct(dispenserType&, cashRegister&);
int main() {
cashRegister counter(100);
dispenserType sanitizer("hand sanitizer", 50, 100);
dispenserType mask("mask", 85, 100) ;
dispenserType tissues("tissues", 25, 0);
dispenserType wipes("wipes", 100, 100);
int choice;
showSelection();
cin >> choice;
switch (choice) {
case 1:
sellProduct(sanitizer, counter);
break;
case 2:
sellProduct(mask, counter);
break;
case 3:
sellProduct(tissues, counter);
break;
case 4:
sellProduct(wipes, counter);
break;
default:
cout << "Invalid selection" << endl;
} // end switch
return 0;
} // end main
void showSelection() {
cout << "*** Welcome to SSU Vending Machine ***" <<
endl;
cout << "To select an item, enter " << endl;
cout << "1 for Sanitizer" << endl;
cout << "2 for Mask" << endl;
cout << "3 for Tissues" << endl;
cout << "4 for Wipes" << endl;
cout << "9 to exit" << endl;
}//end showSelection
void sellProduct(dispenserType& product, cashRegister& pCounter){
int amount; //variable to hold the amount entered
int amount2; //variable to hold the extra amount needed
if (product.getNoOfItems() > 0) //if the dispenser is not
empty
{
cout << "Please deposit " << product.getCost() <<
" cents" << endl;
cin >> amount;
if (amount < product.getCost()) {
cout << "Please deposit another "
<< product.getCost()- amount << " cents" <<
endl;
cin >> amount2;
amount = amount + amount2;
}
if (amount >= product.getCost()) {
pCounter.acceptAmount(amount);
product.makeSale();
cout << "Collect your item at the bottom and enjoy " <<
endl;
}
else
cout << "The amount is not enough. "
<< "Collect what you deposited." << endl;
cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*";
}
else
cout << "Sorry, this item is sold out." << endl;
} //end sellProduct
// end of main.cpp
Output: