John
John

Reputation: 151

c# Having users choose from elements in array

I have a project in which users enter a job description, hours needed, and hourly pay for 5 jobs. Once entered they must choose 2 of the jobs they entered to combine into one job. I'm unsure how to retrieve the job number that they enter from the array (jobArray) (job 1 and job2 for example).

So if they enter in job 1 as mowing, and job 2 as trimming they should have the option to combine both to make mowing and trimming.

Here is my code, any help would be appreciated.

   namespace DemoJobs
    {
    class Class2
    {
        public static Job[] jobArray = new Job[5];
        static void Main(string[] args)
        {

            string option;
            do
            {

                Console.WriteLine("Menu");
                Console.WriteLine("\t1. Enter Jobs");
                Console.WriteLine("\t2. Combine 2 Jobs");
                Console.WriteLine("\t3. Display All Jobs");
                Console.WriteLine("\t4. Exit");

                option = Console.ReadLine();
                switch (option)
                {
                    case "1":
                        Console.Clear();
                        EnterJobs();
                        break;


                    case "2":
                        Console.Clear();
                         //CombineJobs();
                        break;

                    case "3": //display jobs
                        DisplayAllJobs();
                        break;

                    case "4":
                        break;

                    default:
                        Console.WriteLine("Option invalid.");
                        Console.ReadLine();
                        break;

                }

            } while (option != "4");
            Console.WriteLine("Press enter to exit the window.");
            Console.ReadLine();

        }

        private static void EnterJobs()
        {

            for (int i = 0; i < jobArray.Length; i++)
            {
                // int totFee;
                Job job = new Job();

                Console.WriteLine("Job " + i);

                Console.WriteLine("Enter a job description.");
                job.Description = Console.ReadLine();

                Console.WriteLine("Enter the amount of hours required to complete the job.");
                job.hoursToComplete = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter the hourly rate for the job.");
                job.hourlyRate = Convert.ToDouble(Console.ReadLine());

                jobArray[i] = job;

                //calcing total fee
                job.totalFee = job.hourlyRate * job.hoursToComplete;


            }
            Console.WriteLine(" ");


        } //end of enterjobs


        //combining jobs
        private static void CombineJobs(Job first, Job second)
        {
            Console.WriteLine("Which 2 jobs would you like to combine?");
            first.Description = Console.ReadLine();

        }

        private static void DisplayAllJobs()
        {

           // jobArray.ToList().Sort();  

            //sorting jobs from totalFee
            Array.Sort(jobArray);
            //printing array
            Console.WriteLine(jobArray[0].ToString());
            Console.WriteLine(jobArray[1].ToString());
            Console.WriteLine(jobArray[2].ToString());
            Console.WriteLine(jobArray[3].ToString());
            Console.WriteLine(jobArray[4].ToString());  

        }


    }
}

Upvotes: 1

Views: 86

Answers (1)

lozzajp
lozzajp

Reputation: 1016

You can search the array like this.

Job jobToFind = jobArray.FirstOrDefault(s => s.Description.Equals(first.Description));
Job job2ToFind = jobArray.FirstOrDefault(s => s.Description.Equals(second.Description));

and then combine jobs as you see fit.

newJob.Description = jobToFind.Description + job2ToFind.Description; 
// newJob.Pay = more combining here etc.

Upvotes: 1

Related Questions