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; } }
}
}
}