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;}
}
You haven't specified a visibility modifier for your class, which makes it internal.
Try changing this line:
class Job
to this:
public class Job
it means u are accessing a class that is not public.. make the class public like this
public class Job
{
public Job(string description, double time, double rate)
{
Description = description;
Time = time;
Rate = rate;
}
You are right Lasse V. Karlsen you do need to add a public access modifier to the front of the class Job. Over looking the case that a method point "A" is showing the syntax error that indicates that the person was trying to access the private field. When the person should of tried to access the property of the field instead of accessing a field that was a private field.
public static void TotalPay(Job method)
{
A: A:
double totalFee = Job.rate * Job.time;
Console.WriteLine("The total fee is: {0}", TotalFee.ToString("C"));
}
}
There were several different place that had similar errors just follow all of the "A"'s using System;
public class AddJobs
{
private double totalFee;
//A:
public AddJobs(double TotalFee)
{ //A:
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 = method.Rate * method.Time;
Console.WriteLine("The total fee is: {0}", totalFee.ToString("C"));
}
}
public 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; }
}
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 = method.Rate * method.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; }
}