Standard constructor to calculate an employee’s annual salary in C#

Asked

Viewed 654 times

1

I need to make a code that calculates the annual salary (float) from the months worked (int) and monthly salary (float), but it has to be using the constructor method.

Here’s a code I made but it’s returning 0 to the value of the annual salary:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalarioAnual
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculo c = new Calculo();
            Console.Write("Informe o numero de meses trabalhados: ");
            c.Meses = int.Parse(Console.ReadLine());
            Console.Write("Informe o salário mensal: ");
            c.Salario = float.Parse(Console.ReadLine());
            Console.WriteLine("O salário anual é: " + c.SalarioAnual);
        }
    }
}

And here the class Cálculo, with attributes in the constructor method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalarioAnual
{
    class Calculo
    {
        int _Meses;
        float _Salario;
        float _SalarioAnual;

        public int Meses
        {
            set { _Meses = value; }
            get { return _Meses; }
        }

        public float Salario
        {
            set { _Salario = value; }
            get { return _Salario; }
        }

        public float SalarioAnual
        {
            set { _SalarioAnual = _Meses * _Salario; }
            get { return _SalarioAnual; }
        }
    }
}

A new code using a constructor:

using static System.Console;

namespace SalarioAnual {
    public class Program {
        public static void Main() {
            int Meses;
            float Salario;

            Write("Informe o numero de meses trabalhados: ");
            Meses = int.Parse(ReadLine());

            Write("Informe o salário mensal: ");
            Salario = float.Parse(ReadLine());

            var c = new Calculo(Meses, Salario);

            WriteLine("O salário anual é: " + c.SalarioAnual);
        }

        public class Calculo
        {
            float _SalarioAnual;

            public Calculo(int meses, float salario)
            {
                _SalarioAnual = meses * salario;
            }

            public float SalarioAnual
            { get { return _SalarioAnual; } }
        }
    }
}
  • 1

    Hello @Full Welcome to Stackoverflow, please edit your question to make it clearer as your problem cannot be clearly understood, I suggest you visit https://answall.com/help/how-to-ask

  • William, can you read ( https://www.dotnetperls.com/constructor ) and rephrase your question so that we can help you.

  • Just waiting for approval to send you the code friend, but it’s simple.

1 answer

2


Your problem, after editing, doesn’t seem to have anything to do with builder. The code has some problems.

There are things that are unused and use old things that are no longer so that you program in C#. Use float for monetary value that is a mistake. Read an external data without validating, which can break the application (I just closed, but you can do the treatment you want if it is invalid), the correct is to use the TryParse(). And I used the C nomenclature standards#.

I imagine what he wanted was for the annual salary to be calculated automatically when requested without having to fill in and that he would have a formula that is the amount of months multiplied by the simple salary. If that’s all it takes to use a method getter in the property that makes this account, then it will always be calculated. The other properties can be simple and don’t need to do anything extra.

using static System.Console;

namespace SalarioAnual {
    public class Program {
        public static void Main() {
            var c = new Calculo();
            Write("Informe o numero de meses trabalhados: ");
            if (!int.TryParse(ReadLine(), out var meses)) return;
            c.Meses = meses;
            Write("Informe o salário mensal: ");
            if (!decimal.TryParse(ReadLine(), out var salario)) return;
            c.Salario = salario;
            WriteLine($"O salário anual é: {c.SalarioAnual}");
        }
    }
    public class Calculo {
        private decimal salarioAnual;
        public int Meses { get; set; }
        public decimal Salario { get; set; }
        public decimal SalarioAnual => Meses * Salario;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

If you are going to use a constructor as it was put in the question, it causes another problem since it will no longer be a property that calculates the annual salary automatically, which does not seem to be the case. And you won’t be using a standard constructor that’s just the one without parameters:

using static System.Console;

namespace SalarioAnual {
    public class Program {
        public static void Main() {
            Write("Informe o numero de meses trabalhados: ");
            if (!int.TryParse(ReadLine(), out var meses)) return;
            Write("Informe o salário mensal: ");
            if (!decimal.TryParse(ReadLine(), out var salario)) return;
            var c = new Calculo(meses, salario);
            WriteLine($"O salário anual é: {c.SalarioAnual}");
        }
    }

    public class Calculo {
        public Calculo(int meses, decimal salario) {
            Meses = meses;
            Salario = salario;
            SalarioAnual = meses * salario;
        }
        private decimal salarioAnual;
        public int Meses { get; set; }
        public decimal Salario { get; set; }
        public decimal SalarioAnual { get; set; }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Finally if you should calculate something without passing arguments then you will use default values, it’s more of a misconception, but if you insist on this you don’t need to create a default constructor, just use one and just create the default property initialization:

using static System.Console;

namespace SalarioAnual {
    public class Program {
        public static void Main() {
            var c = new Calculo();
            WriteLine($"O salário anual é: {c.SalarioAnual}");
        }
    }

    public class Calculo {
        private decimal salarioAnual;
        public int Meses { get; set; } = 12;
        public decimal Salario { get; set; } = 100M;
        public decimal SalarioAnual => Meses * Salario;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

But I want to make it clear that the first is the right solution.

One last detail: what you call attribute is actually a field. Almost everyone speaks wrong and so everyone teaches wrong.

  • I understood, but I still have a question, I am creating this code to answer a question, and this question asks the following: 'Develop a code in C# using the default constructor to calculate an employee’s annual salary, which must be based on the monthly salary and the number of months worked. ' In this code you gave me (It looks really good!) is being used this default constructor?

  • No, it’s just that by that description, it doesn’t tell you how to do it, it has many forms, you can’t tell by this. Actually with this description I don’t even know sense, and if I did it would be with a code quite different from what you did.

  • Um, right, I think I’m going to ask my tutor more information on the question, to wrap up, I made a code before I saw your answer where the constructor method of the Calculus class receives the variables as parameter, worked, but wanted to know if battery with the description of the question: I will edit my question with this code for you to analyze.

  • The problem is that you spoke of a default constructor in the question. How to calculate something with object creation that does not receive parameters? Only if it is a fixed value, but it would be very strange. It seems to me that the question was not very well thought out.

  • Yes, I also thought, but I edited the question with a code where the constructor receives the parameters of months and salary, then I will adapt it to the modifications you made in the other and send to my tutor.

  • @Guilhermevanzaguirres The problem editing with new content invalidates the answer. but I improved the answer. You can vote on everything on the site as well as accept a reply. See the [tour].

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.