예제 #1
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);
        }
예제 #2
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();
        }