Default constructor to calculate an employee's annual salary in C #

1

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

Here is a code that I did but is returning 0 for the annual salary value:

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 is the class float , with the 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; } }
        }
    }
}
    
asked by anonymous 16.10.2018 / 15:58

1 answer

1

Your problem after editing does not seem to have anything to do with builder . The code has some problems.

There are things that are not used and uses old stuff that is not what you are after in C #. Use float for monetary value which is an error . It reads an external data without validating, which may break the application (I just closed, but you can do whatever treatment you want if it is invalid), the correct one is to use the TryParse() . And I used the C # naming standards .

I imagine what he wanted was that the annual salary be calculated automatically when requested without having to fill out and that he would have a formula that is the amount of months multiplied by the simple salary. If this is all you just need to use a getter method on the property that makes this account, then it will always be calculated. The other properties can be simple and do not 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;
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

If you are going to use a constructor like this one when editing the question, it causes another problem since there will no longer be a property that calculates the annual salary automatically, which does not seem to be the case. And you will not be using a default constructor that is just the one with no 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; }
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

Finally you should calculate something without passing arguments so you will use default values of calculation, it is more a misconception, but if you insist on this you do not 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;
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

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

One last detail: what is called an attribute is actually a field . Almost everyone says it wrong and so everyone teaches wrong.

    
16.10.2018 / 20:17