class Piece For Critique
Nov 30, 2017 at 1:58am UTC
This is not a question problem. More of a solved code that I would appreciate advice on.
I do already know about the "using namespace std;" and "std::" before lines.
I just like utilizing "using namespace std;", because since I am a beginner at this, it helps me keep my code clean as possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class RetailItem
{
private :
string description;
int onHand;
double itemPrice;
public :
RetailItem()
{
description = "<Clear>" ;
onHand = 0;
itemPrice = 0;
}
RetailItem(string desc, int qty, double cost)
{
description = desc;
onHand = qty;
itemPrice = cost;
}
string getDescription() { return description; } //Getters
int getUnits() { return onHand; }
double getPrice() { return itemPrice; }
void SetDescription(string desc) { description = desc; } //Setters
void SetUnits(int qty) { onHand = qty; }
void SetPrice(double cost) { itemPrice = cost; }
};
int main()
{
RetailItem Items[3];
Items[0].SetDescription("Shirt" ); // Setting up each item's data can be done two ways.
Items[0].SetUnits(10); // Array addressing: [0],[1],[2]
Items[0].SetPrice(33.00); // Constructor Assigning the Data: RetailItem Item1("Shirt", 10, 33.00);
Items[1].SetDescription("Jeans" );
Items[1].SetUnits(40);
Items[1].SetPrice(34.95);
Items[2].SetDescription("Long Sleeve Shirt" );
Items[2].SetUnits(20);
Items[2].SetPrice(24.95);
for (int i = 0; i <= 2; i++);
cout << "Welcome to The Retail Store!!\n" << endl;
cout << "Item : " << Items[0].getDescription() << endl;
cout << "Qty : " << Items[0].getUnits() << endl;
cout << fixed << setprecision(2) << "Price: " << "$" << Items[0].getPrice() << "\n" << endl;
cout << "Item : " << Items[1].getDescription() << endl;
cout << "Qty : " << Items[1].getUnits() << endl;
cout << fixed << setprecision(2) << "Price: " << "$" << Items[1].getPrice() << "\n" << endl;
cout << "Item : " << Items[2].getDescription() << endl;
cout << "Qty : " << Items[2].getUnits() << endl;
cout << fixed << setprecision(2) << "Price: " << "$" << Items[2].getPrice() << "\n" << endl;
cout << "The Total Inventory is: " << (Items[0].getUnits() + Items[1].getUnits() + Items[2].getUnits()) << endl;
cout << "The Total Inventory Woth is: " << "$" << ((Items[0].getUnits() * Items[0].getPrice()) + (Items[1].getUnits() * Items[1].getPrice()) +
(Items[2].getUnits() * Items[2].getPrice())) << "\n" << endl;
system("pause" );
}
I only use system pause because the VS 2017 shuts down the program after successfully running it without letting me see the results.
Have at it.
Nov 30, 2017 at 9:23am UTC
I just like utilizing "using namespace std;", because since I am a beginner at this, it helps me keep my code clean as possible.
I don't see what these 5 letters less has to do with keeping code clean.
You can directly initialize your array since you have the appropriate constructor:
RetailItem Items[] = {{"Shirt" , 10, 33.00}, {"Jeans" , ...}, ...};
Note that the size of the array is determined by the compiler.
The loop on line 51 is useless. It can be written it like so:
1 2 3 4 5 6 7 8
cout << "Welcome to The Retail Store!!\n" << endl;
for (int i = 0; i < std::size(Items); i++) // Note: No ; / std::size(Items) to determine the array size
{
cout << "Item : " << Items[i].getDescription() << endl;
cout << "Qty : " << Items[i].getUnits() << endl;
cout << fixed << setprecision(2) << "Price: " << "$" << Items[i].getPrice() << "\n" << endl;
}
Note that
std::size(Items)
is C++17. You may use the C++11
std::end(Items) - std::begin(Items)
as a replacement.
This way you can change the array at any time without changing any other line of code.
Topic archived. No new replies allowed.