#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
using std::cin;
using std::cout;
using std::string;
 
// clothing codes
struct codes
{
	char code;
	const char* explanation_1;
	float total_sales;
};

codes CODES[]={'A',"BABY WEAR",0.0F,
'B',"CHILDREN WEAR",0.0F,
'C',"LADIES WEAR",0.0F,
'D',"MENSWEAR",0.0F,
0,NULL,0.0F };

 
struct RECORD
{
	char category;
	int item_number;
	float price;
};
 
const string filename="jualan.dat";
int menu();
void AddRecord();
void ShowByCategory();
void ShowAll();

__inline void ignore_line (std::istream& in)
{
in.clear();
}

int main()
{
	int select;
	while((select=menu())<4)
		{
			switch(select)
				{
					case 1: // add a record
					AddRecord();
					break;
					case 2: //Display sales report for a category
					ShowByCategory();
					break;
					case 3: // Display sales report for all category
					ShowAll();
					break;
				};
		}
}

int menu()
{
	cout<<"\n\tFamily Outfit Shop"<<endl;
	cout<<"\t    MAIN MENU"<<endl;
	cout<<"\t-------------------"<<endl;
	cout<<"1 = Add a record\n";
	cout<<"2 = Display sales report for a category\n";
	cout<<"3 = Display sales report for all category\n";
	cout<<"4 = Exit\n";
	string input;
	bool failed;
	int select=0;
	do {
		failed=false;
		cout<<"\nEnter Number Menu : ";
		cin>>input;
		for(size_t i=0; i <input.length(); i++)
		{
			if(!isdigit(input[i]))
			{
				cout<<"Numeric digits only -- please try again.\n";
				failed=true;
				break;
			}
		}
		if(failed==false)
		{
			select=atoi(input.c_str());
			if(select<1||select>4 )
			{
				cout<<"Select a number between 1 and 4\n";
				failed=true;
				break;
			}
		}
		cout.clear();
		ignore_line(cin);
	}
	while(failed==true );
	return select;
}


void AddRecord() // add a record
{
	std::ofstream out(filename.c_str(),std::ios::binary);
	bool done=false;
	string answer;
	while(done==false)
		{
			RECORD r;
			cout<<"\nEnter code (A, B, C or D) : ";
			cin>>r.category;
			r.category=toupper(r.category);
			ignore_line(cin);
			cout<<"Enter item number : ";
			cin>>r.item_number;
			ignore_line(cin);
			cout<<"Enter price : ";
			cin>>r.price;
			ignore_line(cin);
			out.seekp(0,std::ios::end);
			out.write((char *)&r,sizeof(RECORD));
			cout<<"\nEnter another record? (Y/N) : ";
			cin>>answer;
			ignore_line(cin);
			if(answer=="N"||answer=="n")
			done=true;
		}
	while(done==false);
}

bool compare(const RECORD& r1,const RECORD& r2)
{
	return r1.item_number < r2.item_number ? true : false;
}


void ShowByCategory()//Display sales report for a category
{
	string category;
	char code;
	int item_number;
	float price, total_price;
	bool done=false;
	while(done==false)
	{
		cout<<"\nPlease enter category (A, B, C or D) : ";
		cin>>category;
		std::transform(category.begin(),category.end(),category.begin(),toupper);
		switch(category[0])
		{
			case 'A':
			if(item_number=101)
			{
				price=10.00;
			}
			if(item_number=102)
			{
				price=5.00;
			}
			break;
			
			case 'B':
			if(item_number=101)
			{
				price=12.00;
			}
			if(item_number=104)
			{
			price=20.00;
			}
			if(item_number=105)
			{
				price=17.00;
			}
			break;
			
			case 'C':
			if(item_number=103)
			{
			price=40.00;
			}
			break;
			
			case 'D':
			if(item_number=101)
			{
				price=15.00;
			}
			break;
			done=true;
			break;
			default:
			cout<<"Wrong input\n";
			break;
		}
	total_price=price+price;

	std::ifstream in(filename.c_str(),std::ios::binary);
	std::vector<RECORD>theList;
	RECORD r;
	while(in.read((char *)&r, sizeof(RECORD)))
	{
		if(code==category[0])
		theList.push_back(r);
	}
	in.close();
	std::sort(theList.begin(),theList.end(),compare);
	cout<<"\nSALES REPORT FOR ";
	for(int i=0; CODES[i].code !=0; i++)
	{
		if(CODES[i].code==category[0] )
		{
			cout<<CODES[i].explanation_1<< '\n';
			break;
		}
	}
	cout<<"\nItem Number\t\tPrice"<<endl;
	cout<<"------------------------------"<<endl;
	cout<<category<<item_number<<'\t'<<price<<'\n';
	cout<<"------------------------------"<<endl;
	cout<<"Total sales \t\t"<<total_price<<endl;
	cout<<"\n\n";
	}
}


void ShowAll() // Display sales report for all category
{
	char explanation_1;
	float total_price, total_sales;
	std::ifstream in(filename.c_str(),std::ios::binary);
	RECORD r;
	for(int i =0; CODES[i].code !=0; i++)
		{
			CODES[i].total_sales =0.0F;
		}
	while(in.read((char *)&r, sizeof(RECORD)))
		{
			for(int i =0; CODES[i].code !=0; i++)
			{
				if(CODES[i].code==total_price )
				{
					CODES[i].total_sales +=total_price;
					break;
				}
			}
		}
		in.close();
		cout << "\nSALES REPORT FOR ALL CATEGORIES"<<endl;
		cout<<"\nCategory\t\tSales"<<endl;
		cout<<"------------------------------"<<endl;
		for(int i=0; CODES[i].code !=0; i++)
		{
			cout<<CODES[i].explanation_1<<"\t\t"<<total_price<<endl;
		}
		cout<<"\nTotal Sales\t\t"<<total_sales<<endl;
		cout<<"\n\n";
}

like this to display, choose by category...


SALES REPORT FOR BABY WEAR

Item Number Price

A101 10.00
A102 5.00

Total Sales 15.00

and also disaplay all category

SALES REPORT FOR ALL CATEGORIES

Category Sales

Baby wear 15.00
Children wear 49.00
Ladies wear 40.00
Menswear 15.00

Total Sales 119.00

Recommended Answers

All 2 Replies

So what's the question?

to display sales by category

and to display sales with all category...

i'm in still did coding by refering my friend's code, error, becosz she use if else...

that's okay but, still cannot..

i'm in try, but like a hell...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.