Question

In: Computer Science

Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize...

Step 1: Extend the dispenserType class per the following specifications.

  • Add a parameterized constructor to initialize product name (name), product cost (cost) and product quantity (noOfItems). This constructor will be invoked from main.cpp. One such call to this constructor will look like:
dispenserType sanitizer("hand sanitizer", 50, 100);
  • Add the declaration and definition of this parameterized constructor to .h and .cpp files.

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

    • int getCurrentBalance() - Function to show the current amount in the cash register. This function returns value of cashOnHand.
    • void acceptAmount(int amountInt) - Function to receive the amount deposited by the customer and update the amount in the register. Returns updated cashOnHand.
    • parameterized constructor - Accepts an integer and uses it to set the cash in the register to a specific amount. If no value is specified when the object is declared, the default value assigned to cashOnHand is 500.
  • Private member

    • int cashOnHand

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:
  
  
  
};

Solutions

Expert Solution

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


Related Solutions

Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
Task 1: Implement the constructor for the Hostess struct. It must initialize all member variables, including...
Task 1: Implement the constructor for the Hostess struct. It must initialize all member variables, including any arrays. There is another function that performs initialization. You must change the program so that the object’s initialization is done automatically, i.e. no explicit call is needed for an initialization function. #ifndef HOSTESS_H #define HOSTESS_H #include "Table.h" #include <iostream> struct Hostess{ void Init(); void PlaceArrivalInQueue(int arrivals); void ShiftLeft(); void Print(int hour, int minute, int arrivals); int GetNumTables(){return numTables;} int Seat(int table, int time);...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs);...
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs); //Destructor. Clear all nodes. ~DoubleLinkedList(); // Insert function. Returns true if item is inserted, // false if the item it a duplicate value bool insert(int x); // Removes the first occurrence of x from the list, // If x is not found, the list remains unchanged. void remove(int x); //Assignment operator. const DoubleLinkedList& operator=(const DoubleLinkedList & rhs); private: struct node{ int data; node* next;...
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2....
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2. Add code to the generateRandomArray method which will fill testArray with random integers between 1 and 10 3. Add code to the printArray method which will print each value in testArray. 4. Write an accessor method, getArray which will return the testArray Here's the code starter code: import java.util.ArrayList; public class TestArrays { private int[] testArray; public TestArrays() { } public void generateRandomArray() {...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the first and last names to "None", the seller ID to "ZZZ999", and the sales total to 0.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Extend the program by adding rules for the following family relationships (add more facts as you...
Extend the program by adding rules for the following family relationships (add more facts as you need ). Rules father(X.Y) > Father (z,w) where X is Y's father father(X,Y):>male(X),Parent(X, Y). brother(X, Y): where X is Y's brother brother(X,Y):-male().praent(X,Y). Brother (y.w) sister(X,Y):-famale(X),praent(X, Y). sister(X, Y): where X is Y's sister Sister(w.y) son(X, Y)> son(X,Y):-male(X), praent(Y,X). where X is Y's son Son(y.z) daughter(X, Y) :- Daughter (w,x) where X is Y's daughter daughter(X,Y):-famale(X), praent(Y.X). where X and Y are sibling Sibling(X, Y):-praent(X,Y),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT