How do I leave a required field?

0

I'm setting up a birthday management application (Consoleapp) for college ... I would like to know how do I make it mandatory to enter the Name / Surname / CPF / dataNascimento fields when the user is adding a new person? Oh and I would like to know how to get the CPF to have 11 characters?

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

namespace CSharpUltimateASS
{
    public class Pessoa
    {
        public string _nome;
        public string _sobrenome;
        public string _CPF;
        public DateTime _dataNascimento;

        public Pessoa(string nome, string sobrenome, string CPF, DateTime dataNascimento)
        {
            _nome = nome;
            _sobrenome = sobrenome;
            _CPF = CPF;
            _dataNascimento = dataNascimento;

        }
    }
}
    
asked by anonymous 10.01.2018 / 22:50

1 answer

1

In order to keep the solution in order to meet the scope of your exercise, I've made these changes in your code, but I've done it in a way that is simple enough for your teacher not to think bad and clear for you to learn too .

As I do not know how far your teacher has taught you, I'm not going to change anything in your class, I believe that the goal of "_" before names is to create public properties using private variables, your code anyway.

I am creating here a static method that will return you an instance of the Person class, only if all fields are validated. Otherwise, it will return you null

Notice that I put the constructor as private so the programmer is required to call the GetInstancia

public class Pessoa
{
    public string _nome;
    public string _sobrenome;
    public string _CPF;
    public DateTime _dataNascimento;

    private Pessoa(string nome, string sobrenome, string CPF, DateTime dataNascimento)
    {
        _nome = nome;
        _sobrenome = sobrenome;
        _CPF = CPF;
        _dataNascimento = dataNascimento;
    }

    public static Pessoa GetInstancia(string nome, string sobrenome, string CPF, DateTime dataNascimento)
    {
        bool construir = true;

        if (nome == "")
        {
            Console.WriteLine("O nome é obrigatório.");
            construir = false;
        }

        if (sobrenome == "")
        {
            Console.WriteLine("O sobrenome é obrigatório");
            construir = false;
        }

        if (CPF == "")
        {
            Console.WriteLine("O CPF é obrigatório.");
            construir = false;
        }
        else if (CPF.Length != 11)
        {
            Console.WriteLine("O CPF é inválido.");
            construir = false;
        }

        if (dataNascimento == DateTime.MinValue)
        {
            Console.WriteLine("A data é obrigatória.");
            construir = false;
        }

        if(construir)
        {
            return new Pessoa(nome, sobrenome, CPF, dataNascimento);
        }
        else
        {
            return null;
        }
    }
}

Remember, however, that the purpose of this is just didactic!

I do not recommend putting Console.WriteLine inside the class at all.

However, since the goal is to maintain simplicity, I do not see any problems.

Example how to consume this method:

Pessoa _pessoa = Pessoa.GetInstancia("teste", "teste", "1234", DateTime.Now);

if(_pessoa != null)
{
  //Código para adicionar em uma lista.
}
    
11.01.2018 / 13:23