Ejemplo n.º 1
0
        public static PayrollUser CreateUser(string username, string firstname, string lastname, string role, string password)
        {
            PayrollUser existingUser = PayrollUser.GetUserByUsername(username);

            if (existingUser != null)
            {
                // A user already exists with this username, these must be unique
                return(null);
            }

            PayrollUser newUser = new PayrollUser();

            newUser.username  = username;
            newUser.firstname = firstname;
            newUser.lastname  = lastname;
            newUser.role      = role;
            if (!PayrollUser.ROLES.Contains(role.ToUpper()))
            {
                // We were passed an invalid role
                return(null);
            }

            newUser.CreatePassword(password);

            newUser.Save();

            return(newUser);
        }
Ejemplo n.º 2
0
        public static Manager GetManagerFromUser(PayrollUser user)
        {
            if (user.role.ToUpper() != "MANAGER")
            {
                return(null);
            }

            Manager manager = new Manager();

            int id    = 0;
            int index = manager.GetUsernameIndex();

            string[] managerRows = FileReader.GetData(Manager.payrollFile);
            foreach (string employeeRow in managerRows)
            {
                string[] dataArray = employeeRow.Split(Manager.separator);
                if (dataArray[index] != user.username)
                {
                    ++id;
                    continue;
                }

                // If we get here we have a match
                Type type = manager.GetType();
                for (int i = 0, count = manager.FORMAT.Count; i < count; ++i)
                {
                    string       paramString = manager.FORMAT[i];
                    PropertyInfo property    = type.GetProperty(paramString);
                    property.SetValue(manager, dataArray[i]);
                }

                manager.id = id;
                return(manager);
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static PayrollUser GetUserByUsername(string username)
        {
            PayrollUser user = new PayrollUser();

            string[] userCollection = FileReader.GetData(PayrollUser.userFile);
            if (userCollection == null)
            {
                return(null);
            }

            int id    = 0;
            int index = user.GetUsernameIndex();

            foreach (string userRow in userCollection)
            {
                List <string> userProps = new List <string>(userRow.Split(PayrollUser.separator).ToList <string>());
                if (userProps[index] != username)
                {
                    ++id;
                    continue;
                }

                // We matched on the username, now we set properties on our object
                Type type = user.GetType();
                for (int i = 0, count = userProps.Count; i < count; ++i)
                {
                    string       paramString = user.FORMAT[i];
                    PropertyInfo param       = type.GetProperty(paramString);
                    param.SetValue(user, userProps[i]);
                }

                user.id = id;
                return(user);
            }

            return(null);
        }
Ejemplo n.º 4
0
        public static Contractor GetContractorFromUser(PayrollUser user)
        {
            if (user.role.ToUpper() != "CONTRACTOR")
            {
                return(null);
            }

            Contractor employee = new Contractor();

            int id    = 0;
            int index = employee.GetUsernameIndex();

            string[] employeeRows = FileReader.GetData(Employee.payrollFile);
            foreach (string employeeRow in employeeRows)
            {
                string[] dataArray = employeeRow.Split(Employee.separator);
                if (dataArray[index] != user.username)
                {
                    ++id;
                    continue;
                }

                // If we get here we have a match
                Type type = employee.GetType();
                for (int i = 0, count = employee.FORMAT.Count; i < count; ++i)
                {
                    string       paramString = employee.FORMAT[i];
                    PropertyInfo property    = type.GetProperty(paramString);
                    property.SetValue(employee, dataArray[i]);
                }

                employee.id = id;
                return(employee);
            }

            return(null);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            if (args.Count() > 0)
            {
                // There are command line arguments, we use these to create a user
                // Ensure there is enough information to create a user
                if (args.Count() != 5)
                {
                    Console.WriteLine("Not enough arguments provided.");
                    return;
                }

                string cmd_username  = args[0];
                string cmd_firstname = args[1];
                string cmd_lastname  = args[2];
                string cmd_role      = args[3];
                string cmd_password  = args[4];

                PayrollUser cmd_user = PayrollUser.CreateUser(cmd_username, cmd_firstname, cmd_lastname, cmd_role, cmd_password);
                if (cmd_user == null)
                {
                    Console.WriteLine("CreateUser unexpectedly returned null after attempting to create a new user");
                    return;
                }

                Console.WriteLine($"Successfully created new user {cmd_user.username}!");
                Console.ReadLine();
                return;
            }

            if (PayrollUser.GetTotalUsers() <= 0)
            {
                Console.WriteLine("There are no valid users, please provide arguments to create a new user");
                return;
            }

            // If we are here there are no command line arguments and there are valid users to load

            Console.Write("Username: "******"Invalid credentials");
                return;
            }

            if (!user.ValidatePassword(password))
            {
                Console.WriteLine("Invalid credentials");
                return;
            }

            Console.Clear();
            Console.WriteLine($"Welcome {user.firstname}!\n");

            string currentRole = user.role.ToUpper();
            bool   runAgain    = false;

            do
            {
                switch (currentRole)
                {
                case "EMPLOYEE":
                    Employee employee = Employee.GetEmployeeFromUser(user);
                    break;

                case "CONTRACTOR":
                    Contractor contractor = Contractor.GetContractorFromUser(user);
                    break;

                case "MANAGER":
                    Manager manager = Manager.GetManagerFromUser(user);
                    break;

                default:
                    break;
                }

                Console.WriteLine("Run again? (y/n)");
                runAgain = Console.ReadLine().Trim() == "y";
            }while (runAgain);

            Console.ReadLine();
        }