Untitled

 avatar
unknown
plain_text
a year ago
8.6 kB
20
Indexable
#include<iostream>
#include<vector>
#include<string>
#include<windows.h>
using namespace std;
void SET_COLOR(int color)
{
	WORD wColor;
     

     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
          wColor = (csbi.wAttributes & 0xF0) + (color & 0x0F);
          SetConsoleTextAttribute(hStdOut, wColor);
     }
}
class IEmployee{
    protected:
        string Name;
        int paymentPerHour;
    public:
        IEmployee(){}
        IEmployee(string Name,int paymentPerHour){
            this-> Name = Name;
             this->paymentPerHour = paymentPerHour;
        }
        ~IEmployee(){}
        int GetpaymentPerHour(){
		return this->paymentPerHour; 
		}
        string getname()const{
		return this->Name;
		}
        void setpaymentPerHour(int paymentPerHour){ 
		this->paymentPerHour=paymentPerHour;
		}
        void setname(string Name){
		this->Name=Name;
		}
        virtual float calculateSalary() = 0;
        virtual void Input(){
            cin.ignore();
            SET_COLOR(3);
			cout<<"Enter name : ";getline(cin,Name);
            cout<<"Enter paymentPerHour : ";cin>>paymentPerHour;
        }
        virtual void Output(){
           cout<<"This name : "<<Name<<endl;
            cout<<"This paymentPerHour : "<<paymentPerHour<<endl;
        }
};
class PartTimeEmployee : public IEmployee {
    private:
        int workingHours;
    public:
        PartTimeEmployee(){}
        PartTimeEmployee(string Name,int paymentPerHour, int workingHours) : IEmployee(Name, paymentPerHour) {
        this->workingHours = workingHours;
    }
        int GetWorkingHours(){
		return this->workingHours;}
         float calculateSalary(){
            return this->workingHours*this->paymentPerHour;
        }
        void Input(){
            IEmployee::Input();
          SET_COLOR(2);
		    cout<<"Enter workingHours : ";
            cin>>workingHours;
        }
        void Output(){
            IEmployee::Output();
           SET_COLOR(3);
		    cout<<"This working Hours: "<<this->workingHours<<" hours"<<endl;
            cout<<"This calculateSalary : "<<this->calculateSalary()<<endl;
            
        }
};
class FullTimeEmployee : public IEmployee {
    private:
        
    public:
        FullTimeEmployee(){}
        FullTimeEmployee(string Name,int paymentPerHour) : IEmployee(Name, paymentPerHour) {
        
    }
        
         float calculateSalary(){
            return this->paymentPerHour*8;
        }
        void Input(){
            IEmployee::Input();
            
        }
        void Output(){
            IEmployee::Output();
            SET_COLOR(3);
            cout<<"This calculateSalary : "<<this->calculateSalary()<<endl;
            
        }
};
class EmployeeManager{
    private:
        vector<IEmployee*> ds;
    public:
        EmployeeManager(){}
        ~EmployeeManager(){
            for(int i = 0;i<ds.size();i++){
                delete ds[i];
            }
            ds.clear();
        }
         
    

        void NhapDS(){
			while(true){
			SET_COLOR(4);
				cout << "===Lua chon ===" << endl;
			SET_COLOR(6);
				cout << "1. PartTimeEmployee " << endl;
				cout << "2. FullTimeEmployee  " << endl;
			SET_COLOR(9);
                cout<<"3.Xuat danh sach "<<endl;
				cout << "0. Thoat" << endl;
				int loai;
				cout << "Nhap: ";
				cin >>loai;
				if(loai == 1){
					IEmployee *pt = new PartTimeEmployee();
					pt->Input();
					ds.push_back(pt);
					
				}else if(loai == 2){
					IEmployee *ft = new FullTimeEmployee();
					ft->Input();
					ds.push_back(ft);
			
				}else if(loai == 0){
					break;
				}else{
					cout << "Lua chon khong dung!" << endl;
				}
			}
	}
//void DeleteEmployee(const string& name) {
    //
//}

void AddEmployee(int type, string name, int paymentPerHour, int workingHours = 0) {
    if (type == 1) { 
        PartTimeEmployee* pt = new PartTimeEmployee(name, paymentPerHour, workingHours);
        ds.push_back(pt);
    } else if (type == 2) { 
        FullTimeEmployee* ft = new FullTimeEmployee(name, paymentPerHour);
        ds.push_back(ft);
    } else {
       SET_COLOR(4);
	    cout << "Invalid employee type!" << endl;
    }
}
		void SearchByName(const string& searchName) {
    bool found = false;
    for (int i = 0; i < ds.size(); i++) {
        if (ds[i]->getname() == searchName) {
            cout << "Found Employee: " << endl;
            ds[i]->Output();
            found = true;
        }
    }
    if (!found) {
        SET_COLOR(4);
		cout << "No employee found with the name: " << searchName << endl;
    }
}

        float TongTienFullTime() {
        float tong = 0;
        for (int i = 0; i < ds.size(); i++) {
            FullTimeEmployee* ft = dynamic_cast<FullTimeEmployee*>(ds[i]);
            //neu chay loi thi sua NULL thanh nullptr
            if (ft != NULL) {
                tong = tong + ft->calculateSalary();
            }
        }
        return tong;
        }
        float TongTien(){
			float tong = 0;
			for(int i = 0; i < ds.size(); i++){
				tong = tong + ds[i]->calculateSalary();
			}
			return (size_t)tong;
		}
		void bubbleSortByName() {
        for (int i = 0; i < ds.size() - 1; i++) {
            for (int j = 0; j < ds.size() - i - 1; j++) {
                if (ds[j]->getname() > ds[j + 1]->getname()) {
                    swap(ds[j], ds[j + 1]);
                }
            }
        }
    }
        void XuatDS(){
			SET_COLOR(3);
			for(int i = 0; i < ds.size(); i++){
				cout << "==Thong tin nhan vien thu " << (i+1) << ": " << endl;
				ds[i]->Output();
				cout<<"This calulaSalary : "<<this->TongTien()<<endl;

			}
			
		}
 

        void Menu(){
			while(true){
			SET_COLOR(8);	cout << "===MENU===" << endl;
			SET_COLOR(1);	cout << "1. Nhap DS" << endl;
				cout << "2. Xuat DS" << endl;
                cout<<"3.Show total Salary "<<endl;
                cout<<"4.Show total Salary of FullTime Employee"<<endl;
                cout<<"5.Sort Employee by name"<<endl;
                cout<<"6.SearchByName "<<endl;
                cout<<"7.Them nhan vien "<<endl;
				cout << "0. Thoat" << endl;
				int luachon;
			SET_COLOR(6);	cout << "Nhap lua chon: ";
				cin >> luachon;
				if(luachon == 1){
					NhapDS();
				}else if(luachon == 2){
					XuatDS();
                }else if(luachon==3){
                   SET_COLOR(2); 
                    cout<<"=====Show total Salary "<<endl;
                    cout<<"Total : "<<this->TongTien()<<endl;
                
				}
                else if(luachon==4){
                    SET_COLOR(2);
                    cout<<"=====Show total Salary of Fulltime Emloyee "<<endl;
                    cout<<"Total : "<<this->TongTienFullTime()<<endl;
                
				}
				else if(luachon==5){
                    SET_COLOR(2);
                    cout<<"=====Sort Employee by name "<<endl;
                    this->bubbleSortByName();
            }else if(luachon==6){
            SET_COLOR(2);	cout<<"=========SearchByName =========="<<endl;
            	string name;
            SET_COLOR(3); cout<< "Enter the name to search: ";
            cin.ignore();
            getline(cin, name);
            SearchByName(name);
				}else if(luachon==7){
					int loai, paymentPerHour, workingHours;
            string name;
            SET_COLOR(2);cout << "Chon loai nhan vien (1. PartTime, 2.FullTime): ";
            cin >> loai;
            cin.ignore();
           SET_COLOR(1); cout << "Enter name: ";
            getline(cin, name);
            cout << "Enter payment per hour: ";
            cin >> paymentPerHour;
            if (loai == 1) {
                cout << "Enter working hours: ";
                cin >> workingHours;
                AddEmployee(loai, name, paymentPerHour, workingHours);
            } else {
                AddEmployee(loai, name, paymentPerHour);
            }
			}else if(luachon==8){
			SET_COLOR(3);cout << "Nhap vao ten nhan vien muon xoa: ";
            
			  break;
			
				}
                else{
			SET_COLOR(3);		cout << "Lua chon khong dung!" << endl;
				}
			
		}}
        
};


int main(){
    EmployeeManager *ql=new EmployeeManager();
        ql->Menu();
        delete ql;
        return 0;
}
Editor is loading...
Leave a Comment