8 Replies - 1763 Views - Last Post: 15 April 2009 - 08:03 AM Rate Topic: -----

#1 Strikerx22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-April 09

Need opinion on code management and modularity

Posted 13 April 2009 - 09:12 PM

In class we have to take an already built code and create seprate header and implementation files for better reuse and modularity. So I would like to know what you all think of how I've done it and any advice you can give to make it better.

//Header file for cashRegister.h
#ifndef H_cashRegister
#define H_cashRegister
#include <iostream>

using namespace std;

class cashRegister
{
public:
	int getCurrentBalance() const;
	   //Function to show the current amount in the cash 
	   //register.
	   //Postcondition: The value of cashOnHand is returned.

	void acceptAmount(int amountIn);
	   //Function to receive the amount deposited by 
	   //the customer and update the amount in the register.
	   //Postcondition: cashOnHand = cashOnHand + amountIn;

	cashRegister(int cashIn = 500); 
	   //Constructor
	   //Sets the cash in the register to a specific amount.
	   //Postcondition: cashOnHand = cashIn;
	   //			   If no value is specified when the 
	   //			   object is declared, the default value 
	   //			   assigned to cashOnHand is 500.

private:
	 int cashOnHand;	 //variable to store the cash 
						 //in the register
};

int cashRegister::getCurrentBalance() const
{
	return cashOnHand;
}

void cashRegister::acceptAmount(int amountIn)
{
	cashOnHand = cashOnHand + amountIn;
}

cashRegister::cashRegister(int cashIn)
{
	if (cashIn >= 0)
		cashOnHand = cashIn;
	else
		cashOnHand = 500;
}
#endif


//Header file for dispenserType.h
#ifndef H_dispenserType
#define H_dispenserType
#include <iostream>

using namespace std;

class dispenserType
{
public:
	int getNoOfItems() const; 
	  //Function to show the number of items in the machine.
	  //Postcondition: The value of numberOfItems is returned.

	int getCost() const; 
	  //Function to show the cost of the item.
	  //Postcondition: The value of cost is returned.

	void makeSale();  
	  //Function to reduce the number of items by 1.
	  //Postcondition: numberOfItems--;

	dispenserType(int setNoOfItems = 50, int setCost = 50); 
	  //Constructor 
	  //Sets the cost and number of items in the dispenser
	  //to the values specified by the user.
	  //Postcondition: numberOfItems = setNoOfItems;
	  //			   cost = setCost;
	  //			   If no value is specified for a 
	  //			   parameter, then its default value is 
	  //			   assigned to the corresponding member 
	  //			   variable.

private:
	int numberOfItems;	 //variable to store the number of 
						   //items in the dispenser
	int cost;  //variable to store the cost of an item
};

int dispenserType::getNoOfItems() const
{
	return numberOfItems;
}

int dispenserType::getCost() const
{
	return cost;
}

void dispenserType::makeSale()
{
	numberOfItems--;
}

dispenserType::dispenserType(int setNoOfItems, int setCost)
{
	if (setNoOfItems >= 0)
		 numberOfItems = setNoOfItems;
	else	
		numberOfItems = 50;

	if (setCost >= 0)
		cost = setCost;
	else
		cost = 50;
}
#endif


//Header file for menues
#ifndef H_menues
#define H_menues
#include <iostream>
#include "cashRegister.h"
#include "dispenserType.h"

using namespace std;

void showSelection()
{
	cout << "*** Welcome to Shelly's Candy Shop ***" << endl;
	cout << "To select an item, enter " << endl;
	cout << "1 for Candy" << endl;
	cout << "2 for Chips" << endl;
	cout << "3 for Gum" << endl;
	cout << "4 for Cookies" << 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 << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
			 << endl << endl;
	}
	else
		cout << "Sorry, this item is sold out." << endl;
}//end sellProduct
#endif


#include <iostream>
#include "cashRegister.h"
#include "dispenserType.h"
#include "menues.h"

using namespace std;

int main()
{
	cashRegister counter;
	dispenserType candy(100, 50); 
	dispenserType chips(100, 65);
	dispenserType gum(75, 45);
	dispenserType cookies(100, 85);

	int choice;  //variable to hold the selection

	showSelection();
	cin >> choice;

	while (choice != 9)
	{
		switch (choice)
		{
		case 1: 
			sellProduct(candy, counter);
			break;
		case 2: 
			sellProduct(chips, counter);
			break;
		case 3: 
			sellProduct(gum, counter);
			break;
		case 4: 
			sellProduct(cookies, counter);
			break;
		default: 
			cout << "Invalid selection." << endl;
		}//end switch

		showSelection();
		cin >> choice;
	}//end while

	return 0;
}//end main


Is This A Good Question/Topic? 0
  • +

Replies To: Need opinion on code management and modularity

#2 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Re: Need opinion on code management and modularity

Posted 13 April 2009 - 09:31 PM

This...
void cashRegister::acceptAmount(int amountIn)
{
	cashOnHand = cashOnHand + amountIn;
}



Can be simplified to...

void cashRegister::acceptAmount(int amountIn)
{
	  cashOnHand += amountIn; 
}



They both do the same thing.

This...
cashRegister::cashRegister(int cashIn)
{
	if (cashIn >= 0)
		cashOnHand = cashIn;
	else
		cashOnHand = 500;
}


Function implementation is also the same implementation in another function. But other than that it looks great. Your comments are really lengthy by the way. Just 4 or 5 words each should help other programmers understand. Your code should be so clear that they should not have to read the comments to know how to use it. Good job. Nice encapsulation. :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#3 Strikerx22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-April 09

Re: Need opinion on code management and modularity

Posted 13 April 2009 - 09:40 PM

Thanks. Should have mentioned that I'm not suppose to change the code but thanks for the advice. Also the comments were already written for the program but I agree that they are lenghty.

This post has been edited by Strikerx22: 13 April 2009 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: Need opinion on code management and modularity

Posted 14 April 2009 - 12:17 AM

It is looking good but to create separate header and implementation files you need to move the main function body into a separate .cpp file.

So you have a class with function prototypes like you have there but instead of implementing the functions at the bottom they should be in a separate file like this

.h (header)
#ifndef  _CASH_REGISTER_H_
#define _CASH_REGISTER_H_

//Header file for cashRegister.h

#include <iostream>

using namespace std;

class cashRegister
{
public:
	int getCurrentBalance() const;
	   //Function to show the current amount in the cash
	   //register.
	   //Postcondition: The value of cashOnHand is returned.

	void acceptAmount(int amountIn);
	   //Function to receive the amount deposited by
	   //the customer and update the amount in the register.
	   //Postcondition: cashOnHand = cashOnHand + amountIn;

	cashRegister(int cashIn = 500);
	   //Constructor
	   //Sets the cash in the register to a specific amount.
	   //Postcondition: cashOnHand = cashIn;
	   //			   If no value is specified when the
	   //			   object is declared, the default value
	   //			   assigned to cashOnHand is 500.

private:
	 int cashOnHand;	 //variable to store the cash
						 //in the register
};

#endif



.cpp (implementation)

#include "CashRegister.h"

int cashRegister::getCurrentBalance() const
{
	return cashOnHand;
}

void cashRegister::acceptAmount(int amountIn)
{
	cashOnHand = cashOnHand + amountIn;
}

cashRegister::cashRegister(int cashIn)
{
	if (cashIn >= 0)
		cashOnHand = cashIn;
	else
		cashOnHand = 500;
}



:^:
Was This Post Helpful? 0
  • +
  • -

#5 Strikerx22  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-April 09

Re: Need opinion on code management and modularity

Posted 14 April 2009 - 12:16 PM

Thanks. I took the implementation code from the two classes and created two seprate .cpp files for them. Is there anything I should change? Should the menues header actually be a .cpp file instead or is it fine as a header?
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: Need opinion on code management and modularity

Posted 15 April 2009 - 12:17 AM

Well it will probably work that way, I don't really know what to do with that. Maybes keep it in your main.cpp above the loop but again that's probably not a good idea.

Since your not allowed to modify the code you won't be able to create a menu class so i would just leave it as it is, I think you have succeeded in what was asked of you :D
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need opinion on code management and modularity

Posted 15 April 2009 - 07:02 AM

Quote

Your comments are really lengthy by the way. Just 4 or 5 words each should help other programmers understand. Your code should be so clear that they should not have to read the comments to know how to use it.


Holy hell! I don't think I have ever heard of "too much documentation". While I am sure that it is possible to have comments that are too long I am quite sure that it takes a while to get there.

The nice thing about comments is that like the program, the programmer does not need to read them. I didn't when initially looking over the code. For me Comments are a second pass read since on the first path my eyes are scanning code and what it does.

Please DON'T worry about your comments being too long. Do try to keep them relevant to the code at hand but don't worry about length.

Trust me, were this code developed for me I would be asking for MORE documentation not less. As a Team Lead I am always having to push for better documentation.

Remember that generally tools such as Doxygen will go though and extract the in-code documentation to build the API documentation for your code. As programmers we are more often than not payed for the code not the end program -- and part of the deliverable is the documentation that allows the customer to USE and MAINTAIN the code that we provide. documentation is critical to this end.

So please -- don't go about telling people "your comments are too long" because its really dumb advice -- one day they will probably be getting bitched at by their boss for not providing enough documentation.
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Need opinion on code management and modularity

Posted 15 April 2009 - 07:36 AM

Amen NickDMax!!

Too little doco is the bane of my existence where I work and it's a constant struggle to get people to write worthwhile comments.

While one is learning or if one expects code to be read by people with low proficiency comments about what is happening are good but this can be a problem in keeping them in sync as code changes. I understand that trade off but it is often overstated in my opinion.

What is never a bad idea are comments that explain why something is being done and especially why it is being done this way (e.g. "using the fooBar algorithm here because we need high throughput and latency is not an issue at this point" ).

This post has been edited by janotte: 15 April 2009 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Need opinion on code management and modularity

Posted 15 April 2009 - 08:03 AM

Moving to C++ Homework.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1