Skip to content

Instantly share code, notes, and snippets.

@NerdyNerves
Last active August 29, 2015 13:58
Show Gist options
  • Save NerdyNerves/10091254 to your computer and use it in GitHub Desktop.
Save NerdyNerves/10091254 to your computer and use it in GitHub Desktop.
C++
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "Inventory.h"
using namespace std;
// *******************************************************************
// setItemNumber() sets the value of the member variable itemNumber. *
// *******************************************************************
void Inventory::setItemNumber(int itemNum) {
// Validates input for non-negative numbers
if (itemNum >= 0) {
itemNumber = itemNum;
}
else {
cout << "Invalid item number.\n";
exit(EXIT_FAILURE);
}
}
// *******************************************************************
// setQuantity() sets the value of the member variable quantity. *
// *******************************************************************
void Inventory::setQuantity(int q) {
// Validates input for non-negative numbers
if (q >= 0) {
quantity = q;
}
else {
cout << "Invalid quantity.\n";
exit(EXIT_FAILURE);
}
}
// *******************************************************************
// setCost() sets the value of the member variable cost. *
// *******************************************************************
void Inventory::setCost(double c) {
// Validates input for non-negative numbers
if (c >= 0.0) {
cost = c;
}
else {
cout << "Invalid cost.\n";
exit(EXIT_FAILURE);
}
}
// *******************************************************************
// setTotalCost() sets the value of the member variable totalCost. *
// *******************************************************************
void Inventory::setTotalCost(int q, double c) {
double tempTotal = q * c;
totalCost = tempTotal;
}
// int getItemNumber();
// int getQuantity();
// double getCost();
// double getTotalCost();
// *******************************************************************
// getItemNumber() returns value in the private member itemNumber *
// *******************************************************************
int Inventory::getItemNumber() const {
return itemNumber;
}
// *******************************************************************
// getQuantity() returns value in the private member quantity *
// *******************************************************************
int Inventory::getQuantity() const {
return quantity;
}
// *******************************************************************
// getCost() returns value in the private member cost *
// *******************************************************************
double Inventory::getCost() const {
return cost;
}
// *******************************************************************
// getTotalCost() returns value in the private member totalCost *
// *******************************************************************
double Inventory::getTotalCost() const {
return totalCost;
}
#ifndef INVENTORY_H
#define INVENTORY_H
class Inventory {
private:
int itemNumber;
int quantity;
double cost;
double totalCost;
public:
// Default Constructor
Inventory() {
itemNumber = 0;
quantity = 0;
cost = 0;
}
// Second Constructor
Inventory(int itemNum, int q, double c) {
itemNumber = itemNum;
quantity = q;
cost = c;
}
void setItemNumber(int);
void setQuantity(int);
void setCost(double);
void setTotalCost(int, double);
int getItemNumber() const;
int getQuantity() const;
double getCost() const;
double getTotalCost() const;
};
#endif // INVENTORY_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "Inventory.h"
using namespace std;
int main() {
Inventory item; // Define an instance of the inventory class
int itemNumber; // Local variable for item number
int quantity; // Local variable for quantity
double cost; // Local variable for cost
double totalCost; // Local variable for totalCost
int choice; // Holds users menu choice
cout << "Calculating cost of an item's inventory.\n\n";
cout << "Enter the Item Number: ";
cin >> itemNumber;
cout << "Enter the Quantity on hand: ";
cin >> quantity;
cout << "What is this item's cost per unit? ";
cin >> cost;
// Store itemNumber, quantity and cost in item object
item.setItemNumber(itemNumber);
item.setQuantity(quantity);
item.setCost(cost);
// Set the total cost
item.setTotalCost(quantity, cost);
// Display results
cout << endl;
cout << "Item Number: " << item.getItemNumber() << endl;
cout << "Quantity: " << item.getQuantity() << endl;
cout << "Cost Per Item: " << item.getCost() << endl;
cout << "Total Cost: " << item.getTotalCost() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment