 #include <iostream>
#include <fstream>
#include <string>
using namespace std;
// player node for linkedlist
class Player {
public:
    int id;
    string name;
    string position;
    int age;
     float salary;
    Player* next;
    Player() : id(0), name(" "), position(" "), age(0), salary(0.0), next(NULL) {}

};
// player linkedlist
class team {
public:
    int rank;
    string name;
    string president;
    Player* head;
    team() : rank(0), name(""), president(""), head(NULL) {}

  // function to insert newnode(add new player)
    void addPlayer(int id, string name, string position, int age, float salary) {
        Player* newPlayer = new Player();
        newPlayer->id = id;
        newPlayer->name = name;
        newPlayer->position = position;
        newPlayer->age = age;
        newPlayer->salary = salary;

        if (head == NULL) {
            head = newPlayer;
        } else {
            Player* temp = head;
            while (temp->next != NULL) {
                temp = temp->next;
            }
            temp->next = newPlayer;
        }
    }

    void readTeamFromFile( string fileName) {
        ifstream inputFile(fileName);

        if (!inputFile.is_open()) {
            cout << "Unable to open file "<< endl;
            return;
        }

        string line;
        while (getline(inputFile, line)) {
            // Find positions of commas
            /*19,nada mohamed
            pos1 -> 1->9->, =2;
            pos2->1->9->,->n->a->d->a->space->m->o->h->a->m->e->d->,=15
            */
            size_t pos1 = line.find(",");
            size_t pos2 = line.find(",", pos1 + 1);
            size_t pos3 = line.find(",", pos2 + 1);
           size_t pos4 = line.find(",", pos3 + 1);
            // Extract data from the line
             /*pos1+1 skip first comma 19|,n/ada
            pos1=| & pos1+1=/ , pos2-pos1 find space between tow comma & -1 without second comma*/
            int id = stoi(line.substr(0, pos1));//stoi convert string into int num
            string name = line.substr(pos1 + 1, pos2 - pos1 - 1);
            string position = line.substr(pos2 + 1, pos3 - pos2 - 1);
            int age = stoi(line.substr(pos3 + 1, pos4 - pos3 - 1));
             float salary = stof(line.substr(pos4 + 1));

            // Add the player to the linked list
            addPlayer(id, name, position, age, salary);
        }

        inputFile.close();
    }

    void displayPlayers() {
        Player* temp = head;
        if (temp==NULL) {
            cout << "No players in the team." << endl;
            return;
        }

        while (temp) {
            cout << temp->id << "," << temp->name<< "," << temp->position << "," << temp->age << "," << temp->salary << endl;
            temp = temp->next;
        }
    }
     
};

int main() {
      team team1, team2, team3, team4, team5, team6, team7, team8, team9;

    // Read and display data for each team manually without using a loop
    cout << "Team 1 (El Bank Al-ahly):******" << endl;
    team1.readTeamFromFile("El Bank Al-ahly.txt");
    team1.displayPlayers();
     

    cout << "Team 2 (Elgouna):*******" << endl;
    team2.readTeamFromFile("Elgouna.txt");
    team2.displayPlayers();
     

    cout << "Team 3 (Enppi):******" << endl;
    team3.readTeamFromFile("Enppi.txt");
    team3.displayPlayers();
    

    cout << "Team 4 (Ghazl elmahala):*****" << endl;
    team4.readTeamFromFile("Ghazl elmahala.txt");
    team4.displayPlayers();
   

    cout << "Team 5 (Haraas elhodod):******" << endl;
    team5.readTeamFromFile("Haraas elhodod.txt");
    team5.displayPlayers();
    

    cout << "Team 6 (ismaaily):*******" << endl;
    team6.readTeamFromFile("ismaaily.txt");
    team6.displayPlayers();
   

    cout << "Team 7 (Modern):********" << endl;
    team7.readTeamFromFile("Modern.txt");
    team7.displayPlayers();
    

    cout << "Team 8 (pharco):**********" << endl;
    team8.readTeamFromFile("pharco.txt");
    team8.displayPlayers();
    

    cout << "Team 9 (ZED):**********" << endl;
    team9.readTeamFromFile("ZED.txt");
    team9.displayPlayers();
     

    return 0;
}
