I compiled and ran the source code below successfully by omitting the totalFee field. How do I write totalFee into this program so that it will accurately calculate the total fee for each job (rate * time)? Below, you'll see I tried using a method; which generated the error CS0051 (Inconsistent accessibility: parameter type 'Job' is less accessible than method 'AddJobs.TotalPay(Job)').
This source code is in response to the following assignment:
"Design a Job class for Harold’s Home Services. The class contains four data fields—Job description (for example, “wash windows”), time in hours to complete the Job (for example, 3.5), per-hour rate charged for the Job (for example, $25.00), and total fee for the Job (hourly rate times hours). Include properties to get and set each field except the total fee—that field will be read-only, and its value is calculated each time either the hourly fee or the number of hours is set. Overload the + operator so that two Jobs can be added. The sum of two Jobs is a new Job containing the descriptions of both original Jobs ( joined by “and”), the sum of the time in hours for the original Jobs, and the average of the hourly rate for the original Jobs. Write a Main()function that demonstrates all the methods work correctly. Save the file as DemoJobs.cs."
Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell
Here is the source code:
using System;
public class AddJobs
{
private double totalFee;
public AddJobs(double totalFee)
{
TotalFee = totalFee;
}
public static void Main()
{
Job job1 = new Job("washing windows", 5.00, 25.00);
Job job2 = new Job("walking a dog", 3.00, 11.00);
Job job3;
job3 = job1 + job2;
Console.WriteLine("The first job's description: {0} \nTotal time needed to complete the job: {1} hours \nHourly fee: {2} per hour", job1.Description, job1.Time, job1.Rate.ToString("C"));
TotalPay(job1);
Console.WriteLine("The second job's description: {0} \nTotal time needed to complete the job: {1} hours \nHourly fee: {2} per hour", job2.Description, job2.Time, job2.Rate.ToString("C"));
TotalPay(job2);
Console.WriteLine("The third job's description: {0} \nTotal time needed to complete the job: {1} hours \nHourly fee: {2} per hour", job3.Description, job3.Time, job3.Rate.ToString("C"));
TotalPay(job3);
}
public static void TotalPay(Job method)
{
double totalFee = Job.rate * Job.time;
Console.WriteLine("The total fee is: {0}", TotalFee.ToString("C"));
}
}
class Job
{
public Job(string description, double time, double rate)
{
Description = description;
Time = time;
Rate = rate;
}
public static Job operator+(Job first, Job second)
{
string newDescription = first.Description + " and " + second.Description;
double newTime = first.Time + second.Time;
double newRate = (first.Rate + second.Rate) / 2;
double newTotalFee = newRate * newTime;
return(new Job(newDescription, newTime, newRate));
}
public string Description {get; set;}
public double Time {get; set;}
public double Rate {get; set;}
}