object orientated programming

hi,
can someone kindly assist on my code?
A mini-mart has just installed a bar code reader to improve efficiency at their checkouts.
Assume that the bar code is to access a file that store the product descriptions, unit price
and quantity of each product sold in the shop. Assume that there are only 10 products in
this mini-mart.
The format of the product stored in the file is shown below :
Barcode : a text string of 4 numeric character
Product description : a text string of 20 characters
Unit price : a floating point value
Quantity : an integer
Design the necessary classes and member functions to achieve the following tasks :
a. Read the list of products available in the mini-mart from a text file.
b. Allow user to enter the bar code of a product via keyboard and the quantity
purchase during check out.


Part A
#include <fstream>
#include <iostream>
#include <string>

using namespace std;
void main()
{
ofstream outFile;
outFile.open("c:\\items.txt");
outFile << " Barcode Items Price Quantity" <<endl;
outFile << " 0001 biscuits $2.50 10" << endl;
outFile << " 0002 bread $1.50 20" << endl;
outFile << " 0003 rice $19.00 50" <<endl;
outFile << " 0004 milk $2.15 30" <<endl;
outFile << " 0005 chocolate $1.10 45" <<endl;
outFile << " 0006 sweet $0.50 100" <<endl;
outFile << " 0007 vegetables $1.45 15" <<endl;
outFile << " 0008 toothpaste $2.10 35" <<endl;
outFile << " 0009 sugar $1.30 40" <<endl;
outFile << " 0010 soap $1.50 30" <<endl;
outFile.close();
}

Part B:
how do i make a user type the barcode from the textbar?

I don't think your items.txt will be that useful. The problem is that each line is a different length.

To read from a file you need to know the record layout. Otherwise, you will need to parse the record looking for stuff. I would suggest a flat file where each field has a pre-set width.

Therefore, I would declare a struct that contains the barcode, description and price.

Next, I would define an array of this struct and populate it with data.

Next, I would write a function that takes this array, the barcode entered at the checkstand and the quantity as arguments. The function should return true or false if the barcode is valid or not. If valid, it calculates the charge and returns that using a pointer argument.

After the program is working, I would write the code to load the array from a disc file. Until then, I would just hard-code values temporarily until the program is working.

It doesn't appear from the problem description that you will need to do any object-oriented programming.
Hi weaknessforcats,

thanks for your important advice.

can you give an example of how the source code should be like? anything that i could start off with?

as far as my module was taught, it require some class and objects....

so please advise..thanks:)
Sorry, I can't do the code for you.

But I can advise you once you get going.
The data can be designed like this!Using the stringstream read the string!
1
2
3
4
 unsigned int Barcode;
string Items;
double Price;
unsigned int Quantity;


You can reference the code as this ( the class's design ):

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
/********************************************************************
	created:	2008/07/02
	created:	2:7:2008   22:17
	filename: 	E:\CODING\CSDN_Exception\main.cpp
	file path:	E:\CODING\CSDN_Exception
	file base:	main
	file ext:	cpp
	author:		hecan
	
	purpose:
	主函数中是一个简单的除数为0的异常处理
	//////////////==WinXp Sp2 + Vs6.0 Pass==////////////
	test filename:ExceptionTable.txt
	test data:
	字符串错误 2102 用户名错误 
	字符串错误 2103 密码错误 

*********************************************************************/
#include "CSDN_Exception.h"
using namespace  std;

int main()
{
	try
	{
		
		CExceptionTable cet_Test(string("字符串错误"), 2102);
	//	cet_Test.InitData();
	//	cout << cet_Test.What() << endl;
	//	cet_Test.Output();

		int iA, iB;
		cin >> iA >>iB;
		if (0 == iB)
		{
			throw cet_Test;
		}
		cout << iA / iB <<endl;
		
	}
	catch (CExceptionTable & Exception_Test)
	{
		cout << Exception_Test.What() << endl;
	}
	
	return 0;
}




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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

/********************************************************************
	created:	2008/07/02
	created:	2:7:2008   22:17
	filename: 	E:\Coding\CSDN_Exception\CSDN_Exception\CSDN_Exception.h
	file path:	E:\Coding\CSDN_Exception\CSDN_Exception
	file base:	CSDN_Exception
	file ext:	h
	author:		hecan
	
	purpose:	
*********************************************************************/

#ifndef __CSDN_EXCEPTION_H__
#define __CSDN_EXCEPTION_H__

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>

//////////////////////////////////////////////////////////////////////////
//单个存储异常信息的节点的类:用户不使用
//异常信息从文件中读取,文件名为ExceptionTable.txt
class CMyException
{
public:
	CMyException(std::string & _sExpKind = std::string("字符串错误"), 
				int _iExpID = 2102,
				std::string &_sExpEvent = std::string("用户名错误"));

 const	std::string &GetExpKind()const;
	const	   int  &GetExpID()const;
  const std::string &GetExpEven()const;

	 inline bool operator ==(const CMyException &front);
	CMyException & operator=(const CMyException &cmyExp_data);

	friend std::ostream & operator <<(std::ostream & os, const CMyException &exp);

	 
	
	~CMyException();

	private:
	std::string sExpKind;//异常种类:如 字符串错误  
		int iExpID;//异常ID号:如 2102 
		std::string sExpEvent;//异常的具体事件:如 用户名错误
};

////////////////////////////////////////////////////
//用于从异常信息中根据类型和ID来查询具体异常的类
/////////////////////////////////////////////////////
class CExceptionTable
{
public:
	//初始化所有数据并存储需要查询的异常
	CExceptionTable( std::string & _sExpKind = std::string("字符串错误"), int _iExpID = 2102);
	//用于给用户来显示查询到的异常	
	const std::string  What();

	///////////////////////////////test////////////////
	void Output();
	///////////////////////////////test////////////////

	
private:
	void InitData();//封装初始化所有异常的函数

	std::vector< CMyException >  vce_ExceptionTbl;//存储所有异常类的表
	std::string sExpKind_Query;//待查询的异常种类
	int iExpID_Query;//待查询的异常ID号

};

/************************************************************************/
/*               Test lie in main                                        */
/************************************************************************/
/*class CTest
{
public:
	void CTest()
	{

	}
	
private:
};
*/

#endif 



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/********************************************************************
	created:	2008/07/02
	created:	2:7:2008   22:20
	filename: 	E:\Coding\CSDN_Exception\CSDN_Exception\CSDN_Exception.cpp
	file path:	E:\Coding\CSDN_Exception\CSDN_Exception
	file base:	CSDN_Exception
	file ext:	cpp
	author:		hecan
	
	purpose:	
*********************************************************************/

#include "CSDN_Exception.h"

using namespace std;

CMyException::CMyException(std::string & _sExpKind ,
						   int _iExpID,
							std::string &_sExpEvent):
							sExpKind(_sExpKind),  
							iExpID(_iExpID), 
							sExpEvent(_sExpEvent)
{

}

const string & CMyException::GetExpKind()const
{
	return  sExpKind;
	}
const int  & CMyException::GetExpID()const
{
	return iExpID;
}
const string & CMyException::GetExpEven()const
{
	return sExpEvent;
}

bool CMyException::operator ==(const CMyException &front)
{
	return (front.GetExpKind() == sExpKind) && 
			(front.GetExpID() == iExpID);
}

CMyException & CMyException::operator=(const CMyException &cmyExp_data)
{
	sExpKind = cmyExp_data.GetExpKind();
	iExpID = cmyExp_data.GetExpID();
	sExpEvent = cmyExp_data.GetExpEven();
	return *this;
}

CMyException::~CMyException()
{

}

std::ostream & operator <<(std::ostream & os, const CMyException &exp)
{
	os <<  exp.GetExpKind() <<" " << exp.GetExpID() <<" " << exp.GetExpEven() <<endl;
	return os;
}
//========================CExceptionTable==================================//


CExceptionTable::CExceptionTable(std::string & _sExpKind, int _iExpID)
:sExpKind_Query(_sExpKind), iExpID_Query(_iExpID)
{
	InitData();
}


void CExceptionTable::InitData()
{

	ifstream ifsExpTbl("ExceptionTable.txt", ios::in);
	if (!ifsExpTbl)
	{
		cerr << "Can't Open File !" << endl;
	}

	string sLine;
	while (getline(ifsExpTbl, sLine))
	{
		istringstream issLine(sLine);
		
		string  sExpKind_Tmp ;
		int iExpID_Tmp;
		string sExpEvent_Tmp;
		issLine >> sExpKind_Tmp  >> iExpID_Tmp >> sExpEvent_Tmp;

	//	cout <<  sExpKind_Tmp <<" " << iExpID_Tmp <<" " << sExpEvent_Tmp <<endl;

		CMyException cexp_Tmp(sExpKind_Tmp, iExpID_Tmp, sExpEvent_Tmp);	
		vce_ExceptionTbl.push_back(cexp_Tmp);
	}
	copy(vce_ExceptionTbl.begin(), vce_ExceptionTbl.end(), ostream_iterator<CMyException>(cout));

}



const string  CExceptionTable::What()
{
	CMyException cmy_Query(sExpKind_Query, iExpID_Query);
	vector< CMyException >::iterator vci_Iter = find(vce_ExceptionTbl.begin(), vce_ExceptionTbl.end(), cmy_Query);
	if (vci_Iter == vce_ExceptionTbl.end())
	{
		return string("Error");
	}
//	cout << vci_Iter->GetExpEven();
	return vci_Iter->GetExpEven();
//		return string("error");
	
	//string yy("yy");
	//return yy;

}


void  CExceptionTable::Output()
{
	copy(vce_ExceptionTbl.begin(), vce_ExceptionTbl.end(), ostream_iterator<CMyException>(cout));
	
}
hi simo and all,

thanks for the help. i will try to figure it out. simo, can i email you privately?
Yes,you can!
Send email to me .
active2volcano@gmail.com
Hi there ^_^... can anyone help me on this program requirement?

A mini-mart has just installed a bar code reader to improve efficiency at their checkouts.
Assume that the bar code is to access a file that store the product descriptions, unit price
and quantity of each product sold in the shop. Assume that there are only 10 products in
this mini-mart.
The format of the product stored in the file is shown below :
Barcode : a text string of 4 numeric character
Product description : a text string of 20 characters
Unit price : a floating point value
Quantity : an integer
Design the necessary classes and member functions to achieve the following tasks :


a. Read the list of products available in the mini-mart from a text file.
b. Allow user to enter the bar code of a product via keyboard and the quantity
purchase during check out.
c. Display the product description, unit price, quantity and the total cost of all the
products bought by a customer.
d. Update the quantity of each product in stock after a purchase is done.
e. Write the bar code and product description to a text file if the level of stock is less
than 10.
f. Continue the program until user press a ‘Q’ to exit.

i have completed some of the parts but how to do "D and question E" any hints for me? pls....going to complete soon... the part where getline doesn't link to the next sentance.

For example i type in the barcode already but it doesn't match with the words that i save in the .txt....i've break it into parts so that i can read the Qty and update...
Last edited on
Hi Terry,
u can try using ofstream fout >> item_barcode >>item _desc >> etc etc.
sorry for reply late

problem is that i now already created the txt file.

then when i type in the barcode in there it doesn't give me the product that i want.

for exmaple:0001= bread , 0002= rice, 0003= sugar etc...

then when i type in 0002, the statement give me bread which mean it wouldn't go to the correct product that i want...

and btw fout meaning? cause i still haven't gone until that far...

Last edited on
terry,
warts your msn?
skylinehack@hotmail.com
Topic archived. No new replies allowed.