Create methods in classes to manipulate fields

Asked

Viewed 74 times

0

I made a code of a binary association between two classes Cliente and Caixa, some methods I might not understand how they are inserted into the code in logical way, but the code itself is working, I would like a help as the code is incomplete.

Missing methods are marked in red rectangles.

ERD

using static System.Console;

public class Cliente
{
    public string Nome { get; set; }
    public decimal Saldo { get; set; }

    public Caixa caixa;

    public string GetNome()
    {
        return this.Nome;
    }

    public void SetNome(string Nome)
    {
        this.Nome = Nome;
    }

    public decimal GetSaldo()
    {
        return this.Saldo;
    }

    public void SetSaldo(decimal Saldo)
    {
        this.Saldo = Saldo;
    }

    public string toString()
    {
        return $"Cliente: {Nome} | Saldo: {Saldo}";
    }
}

public class Caixa
{
    public int Codigo { get; set; }
    public string NomeOperador { get; set; }

    public Cliente cliente;

    public int GetCodigo()
    {
        return this.Codigo;
    }

    public void SetCodigo(int Codigo)
    {
        this.Codigo = Codigo;
    }

    public string GetNomeOperador()
    {
        return this.NomeOperador;
    }

    public void SetNomeOperador(string NomeOperador)
    {
        this.NomeOperador = NomeOperador;
    }

    public string toString()
    {
        return $"Código {Codigo} | Operador: {NomeOperador}";
    }
}

public class AppCaixa
{
    static int Main(string[] args)
    {
        Cliente _cliente = new Cliente();
        Caixa _caixa = new Caixa();

        Write("Digite o nome do cliente: ");
        _cliente.Nome = ReadLine();
        Write("Digite o nome do saldo do cliente: ");
        if (!decimal.TryParse(ReadLine(), out decimal digitesaldo)) return 1;
        _cliente.Saldo = digitesaldo;

        Write("Digite o código do caixa: ");
        if (!int.TryParse(ReadLine(), out int digitecodigo)) return 1;
        _caixa.Codigo = digitecodigo;
        Write("Digite o nome do caixa: ");
        _caixa.NomeOperador = ReadLine();

        _cliente.caixa = _caixa;
        _caixa.cliente = _cliente;

        WriteLine($"{_caixa.toString()}");
        WriteLine($"{_cliente.toString()}");
        ReadKey();
        return 0;
    }
}

2 answers

2


This is what we call methods getters/Setter. There are controversies about its use and almost always people use wrong (read everything, including the links). In C# if you want to reproduce them idiomatically you should use properties, since a property is the implementation of the language precisely for the Pattern design called getter/Setter. And you’ve used one part right.

I think I told you that the ToString() is not fit for this. I’m going to change to something that makes sense. In this case I might even leave the toString() that not the same as i ToString() that every object has, but the intention was to be this, since this model was made thinking in another language, but it would be very confusing.

So this model doesn’t make much sense and is a bad example of how to model something, don’t consider it as something to learn well.

The only thing you should do since you used property is to eliminate these methods, simple as this (I modified names to meet the C nomenclature standard#):

using static System.Console;

public class Cliente {
    public string Nome { get; set; }
    public decimal Saldo { get; set; }
    public Caixa caixa;
    public string Imprimir() => $"Cliente: {Nome} | Saldo: {Saldo}";
    public Cliente(string nome) => Nome = nome;
}

public class Caixa {
    public int Codigo { get; set; }
    public string NomeOperador { get; set; }
    public Cliente cliente;
    public string Imprimir() => $"Código {Codigo} | Operador: {NomeOperador}";
    public Caixa(int codigo) => Codigo = codigo;
}

public class AppCaixa {
    static int Main(string[] args) {
        Write("Digite o nome do cliente: ");
        var nome = ReadLine();
        var cliente = new Cliente(nome);
        Write("Digite o nome do saldo do cliente: ");
        if (!decimal.TryParse(ReadLine(), out decimal digiteSaldo)) return 1;
        cliente.Saldo = digiteSaldo;
        Write("Digite o código do caixa: ");
        if (!int.TryParse(ReadLine(), out int digiteCodigo)) return 1;
        var codigo = digiteCodigo;
        var caixa = new Caixa(codigo);
        Write("Digite o nome do caixa: ");
        caixa.NomeOperador = ReadLine();
        cliente.caixa = caixa;
        caixa.cliente = cliente;
        WriteLine($"{caixa.Imprimir()}");
        WriteLine($"{cliente.Imprimir()}");
        return 0;
    }
}

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

2

The method with the same class name is builder class, which is the method executed when constructing a class object, using the new, for example Cliente cli = new Cliente().

In your example, the class builder Cliente takes a name as parameter, which is an attribute of this class, so it should do the same as the setNome:

public Cliente(string nome)
{
   this.nome = nome;
}

The same reasoning for the class Caixa:

public Caixa(int codigo)
{
    this.codigo = codigo;
}

The others are common get/set, which returns/setam a type of a class, just use the same logic as the others get/set you have already done, for example:

public void SetCaixa(Caixa caixa)
{
    this.caixa = caixa;
}

public Caixa GetCaixa()
{
    return this.caixa;
}


public void SetCliente(Cliente cliente)
{
    this.cliente = cliente;
}

public Cliente GetCliente()
{
    return this.cliente;
}

Now an observation: to implement a get/set, in C# you should not use properties, as they already implement this for you.
The public string Nome { get; set; } itself already a get/set name.

To do this manually, you must declare variables without access outside the class, which can only be accessed via get/set methods, so they can be private:

private string nome;

public string GetNome()
{
    return this.nome;
}

public void SetNome(string Nome)
{
    this.nome = nome;
}

This would be the correct way to "encapsulate the variable", which should be the concept behind what you’re doing. This implementation is very common in Java for example, but in C#, simply state the property, as in your example public string Nome { get; set; } that does this, so your get/set code is redundant with the properties, instead of them declaring variables private, as the example above.

Browser other questions tagged

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