157,675 views
7 votes
7 votes
Given two integers as user inputs that represent the number of drinks to buy and the number of bottles to restock, create a VendingMachine object that performs the following operations:

Purchases input number of drinks Restocks input number of bottles.
Reports inventory Review the definition of "VendingMachine.cpp" by clicking on the orange arrow.
A VendingMachine's initial inventory is 20 drinks.

Ex: If the input is: 5 2
the output is: Inventory: 17 bottles

User Jeffrey Knight
by
2.8k points

2 Answers

19 votes
19 votes

Final answer:

To create a VendingMachine object that performs the given operations, we can define a class called VendingMachine with appropriate functions. The purchaseDrinks() function subtracts the number of drinks from the inventory, the restockBottles() function adds the number of bottles to the inventory, and the reportInventory() function displays the current inventory.

Step-by-step explanation:

To create a VendingMachine object that performs the given operations, we can define a class called VendingMachine with appropriate functions. Here's an example of how the code might look:

#include <iostream>

using namespace std;

class VendingMachine {

private:

int inventory;

public:

VendingMachine() { inventory = 20; }

void purchaseDrinks(int drinks) {

inventory -= drinks;

}

void restockBottles(int bottles) {

inventory += bottles;

}

void reportInventory() {

cout << "Inventory: " << inventory << " bottles" << endl;

}

};

int main() {

int drinks, bottles;

cin >> drinks >> bottles;

VendingMachine machine;

machine.purchaseDrinks(drinks);

machine.restockBottles(bottles);

machine.reportInventory();

return 0;

}

In this code, the VendingMachine class has three functions: purchaseDrinks(), restockBottles(), and reportInventory(). The purchaseDrinks() function subtracts the number of drinks from the inventory, the restockBottles() function adds the number of bottles to the inventory, and the reportInventory() function displays the current inventory. The main() function takes user inputs for the number of drinks and bottles, creates a VendingMachine object, calls the appropriate functions, and reports the updated inventory.

User Roan
by
3.1k points
9 votes
9 votes

Answer:

In C++:

#include <iostream>

using namespace std;

class VendingMachine {

public:

int initial = 20;};

int main() {

VendingMachine myMachine;

int purchase, restock;

cout<<"Purchase: "; cin>>purchase;

cout<<"Restock: "; cin>>restock;

myMachine.initial-=(purchase-restock);

cout << "Inventory: "<<myMachine.initial<<" bottles";

return 0;}

Step-by-step explanation:

This question is incomplete, as the original source file is not given; so, I write another from scratch.

This creates the VendingMachine class

class VendingMachine {

This represents the access specifier

public:

This initializes the inventory to 20

int initial = 20;};

The main begins here

int main() {

This creates the object of the VendingMachine class

VendingMachine myMachine;

This declares the purchase and the restock

int purchase, restock;

This gets input for purchase

cout<<"Purchase: "; cin>>purchase;

This gets input for restock

cout<<"Restock: "; cin>>restock;

This calculates the new inventory

myMachine.initial-=(purchase-restock);

This prints the new inventory

cout << "Inventory: "<<myMachine.initial<<" bottles";

return 0;}

User Erwin Kaddy
by
2.8k points