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?
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
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; };
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
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 {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT