//Class Nhan Vien
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
class NhanVien
{
	private:
		char tennv[30];
		char msnv[30];
		char gioitinh[20];
		double luong;
	public:
		void setTennv(char*);
		char* getTennv();
		void setMsnv(char*);
		char* getMsnv();
		void setGioitinh(char*);
		char* getGioitinh();
		void setLuong(double);
		double getLuong();
		void Input();
		void show();		
};
void NhanVien::setTennv(char* ten)
    {
      strcpy(tennv,ten);
    }
char* NhanVien::getTennv() 
    {
    	return tennv;
	}
void NhanVien::setMsnv(char* ms)
    {
       strcpy(msnv,ms);	
	}	     
char* NhanVien::getMsnv()
	{
		return msnv;
	}
void NhanVien::setGioitinh(char* gt)
    {	
	     strcpy(gioitinh,gt);
	}	
char* NhanVien::getGioitinh()
    {
        return gioitinh;	
	}	
void NhanVien::setLuong(double luog)
    {
    	luong=luog;
	}	
double NhanVien::getLuong()
    {
        return luong;	
	}	
void NhanVien::Input(){
	char ten[30];
	char gt[20];
	char ms[30];
	double luog;
	cout<<"Ten nhan vien: "; cin.ignore(); cin.getline(ten,30); setTennv(ten);
	cout<<"Ma so nhan vien: "; cin.getline(ms,30); setMsnv(ms);
	cout<<"Gioi tinh: ";  cin.getline(gt,20); setGioitinh(gt); 
	cout<<"Luong: "; cin>>luog; setLuong(luog);
}
void NhanVien::show(){

	cout<<"Ten nhan vien: " << getTennv() << endl; 
	cout<<"Ma so nhan vien: "<< getMsnv() << endl; 
	cout<<"Gioi tinh: "<< getGioitinh() << endl;
	cout<<"Luong: "<< getLuong() << endl;
}

class VATTU {
private:
	char maVT[10];
	char tenVT[20];
	char hangVT[20];
	int soluong;
	int giaVT;
public:
	
	void  setmaVT(char* maVTIn) {
		strcpy(maVT,maVTIn);
	};  
	char*  getmaVT(){
		return maVT;
    };   
	
	void settenVT(char* tenVTIn)
	{
		strcpy(tenVT,tenVTIn);	
	};
	char* gettenVT() {
		return tenVT;
	};
	
	void sethangVT(char* hangVTIn){
		strcpy(hangVT,hangVTIn);
	}
	char* gethangVT(){
		return hangVT;
	}

	void  setsoluong(int soluongIn) {
		soluong = soluongIn;
    };  
	int   getsoluong() {
		return soluong;
	}; 
	void setgiaVT(int giaVTIn){
		giaVT = giaVTIn;
	}
	int getgiaVT(){
		return giaVT;
	}
	void  nhapVT();
	void  hienthiVT();
};

void VATTU::nhapVT() {
	char maVTIn[10];
	char tenVTIn[20];
	char hangVTIn[20];
	int soluongIn;
	int giaVTIn;
	cout << "Nhap ten vat tu:"; cin.ignore(); cin.getline(tenVTIn,20); settenVT(tenVTIn);
	cout << "Nhap ma vat tu: "; cin >> maVTIn; setmaVT(maVTIn);
	cout << "Nhap hang : "; cin.ignore(); cin.getline(hangVTIn,20); sethangVT(hangVTIn);
	cout << "Nhap gia: "; cin >> giaVTIn; setgiaVT(giaVTIn);
	cout <<"Nhap so luong: "; cin >> soluongIn; setsoluong(soluongIn);
}
void VATTU::hienthiVT(){
	cout << "\nMa vat tu : " << getmaVT() << endl;
	cout << "Ten vat tu :" << gettenVT() << endl;
	cout << "Hang: " << gethangVT() << endl;
	cout << "So luong : " << getsoluong() << endl;
	cout << "Don Gia: " << getgiaVT() << endl;
};
void menu(){
	cout<< "THONG TIN NHAN VIEN " << endl;
	cout<< "1. Nhap thong tin nhan vien"<<endl;
	cout<< "2. Hien thi thong tin nhan vien"<<endl;
	cout<< "3. Nhan '0' de thoat. "<<endl;
	cout << "Thong tin vat tu "<< endl;
	cout << "Push 1 nhap thong tin vat tu " << endl;
	cout << "Push 2 hien thi thong tin vat tu " << endl;
	cout << "Push 0 de thoat." << endl;

}
int choice(){
	int c;
	cout<<" Moi nhap lua chon: ";
	cin>>c;
	return c;
}
void option(){
	//NhanVien *nv = new NhanVien[100];
	NhanVien nv[100];
	int choose,i;
	do{
		menu();
		choose=choice();
		switch(choose){
			case 1:{
				fstream fileOut("cuong123.txt",ios::out|ios::binary|ios::app);
					if (!fileOut){   
				     cout << "Khong the tao duoc tep tin " << endl;
				       exit(1);
			    }
				int n;
				cout << "Nhap so luong nhan vien: ";
				cin >> n;
				//fileOut.write(reinterpret_cast<char *>(nv),sizeof(NhanVien));
				for(int i=1;i<=n;i++){
				    cout << "Moi nhap thong tin nhan vien " << i <<endl;
					nv[i].Input();	
			      fileOut.write(reinterpret_cast<char *>(nv),sizeof(NhanVien)); 
			    }
				fileOut.close();
				break;
				} 
			case 2:{
				fstream fileIn("cuong123.txt",ios::in|ios::binary);
				if(!fileIn){
					cout << "Khong the mo duoc tep tin "<<endl;
					exit(1);
				}
				
					fileIn.read(reinterpret_cast<char*>(&nv[100]),sizeof(NhanVien));
			    
				cout << "Hien thi nhan vien thu: ";
				cin >> i ;
				nv[i].show();
				fileIn.close(); 
				break; 
			} 
			case 3: {
				fstream fileOut("khoVT.txt", ios::out | ios::binary | ios::app);
				if (!fileOut) {    // Không mở được tệp 
					cout << "Khong the tao duoc tep tin " << endl;
					exit(1);
				}
	
				int n;
				cout << "So luong vat tu muon nhap: ";
				cin >> n;
				for (int i = 0; i < n; i++) {
					cout << "Thong tin vat tu " << i + 1 << endl;
					vt[i].nhapVT();
					fileOut.write(reinterpret_cast<char *>(vt),
						sizeof(VATTU));
				}
				fileOut.close();
				
				break;	
			}
			case 4: {
				int m = 0;
				fstream fileIn("khoVT.txt", ios::in | ios::binary);
	
				while (fileIn.read(reinterpret_cast<char *>(vt), sizeof(VATTU))) {
					m = m + 1;
				}
				for (int i = 0; i < m; i++) {
	
					vt[i].hienthiVT();
				}
				fileIn.close();
				break;
			}
		}
	while(choose!=0);
}
int main()
{
	option();
	return 0;
}
