Skip to content

Code review - #8

Open
Akshama wants to merge 1 commit into
masterfrom
Vending-Machine
Open

Akshama wants to merge 1 commit into
masterfrom
Vending-Machine

Conversation

@Akshama

@Akshama Akshama commented Mar 27, 2018

Copy link
Copy Markdown
Owner

No description provided.

@mooreaarond mooreaarond left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a few notes here to get you to think about things. One big thing I would call out is that you should always validate input to public methods. You never know how someone is going to use these methods, so you should always make sure they give you valid values. I also would want you to think a little more about how you interact with a vending machine as I don't think your main method here accurately reflects that

Comment thread 6_VendingMachine.cpp
{
public:
int coins_5;
int coins_10;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These probably shouldn't be public, so that someone can't just arbitrarily increase the values or set them to negative numbers

Comment thread 6_VendingMachine.cpp
currency()
{
coins_5=100;
coins_10=50;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These probably shouldn't be hard coded values

Comment thread 6_VendingMachine.cpp
coins_10=50;
}
};
int id=0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This id shouldn't be a global value, maybe a member of the vending machine class?

Comment thread 6_VendingMachine.cpp
class Product
{
int p_id;
float p_price;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to be careful using float for values representing money because of rounding issues. The classic example is that 0.1 can't be exactly represented by floating point numbers.
http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/

Comment thread 6_VendingMachine.cpp
int p_id;
float p_price;
public:
int p_count;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be private so that people have to use your increase/decrease_p_count methods and can't change this to invalid values

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, does it make sense for the Product itself to know how many of them there are? Does a bag of chips know how many other chips there are in the machine?

Comment thread 6_VendingMachine.cpp
cout <<"Insufficient change";
}

void update_VM(float amount,int item_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate amount > 0 and item_id is a valid id before indexing into items_list

Comment thread 6_VendingMachine.cpp
coins_10+=cnt_10;
coins_5+=cnt_5;
}
void pay_price(float bal)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do they actually get the change back if this is void?

Comment thread 6_VendingMachine.cpp
{
Vending_Machine VM;
cout<< "Welcome!";
cout<<"\nChoose your option\n 1.Request the price of an item\n2. Select and buy items\n3. Cancel request";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this how a vending machine usually works? Typically you can select an item or add money in any order and different things happen based on the case

Comment thread 6_VendingMachine.cpp
{
cout<<"\nEnter the Item ID";
cin>>p_id;
cout<<"Price: "<<VM.items_list[p_id]->get_price();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should verify that p_id is a valid value before indexing into an array.

Comment thread 6_VendingMachine.cpp
int cnt_10,cnt_5;
cin>>cnt_10>>cnt_5;
VM.add_coins(cnt_5,cnt_10);
VM.update_VM(pay,p_id);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't return item/change to the user?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants