0
I am currently studying about classes, more specifically class/UML relationship and I am doing it in C#. I would like to know if this code I wrote below is the most suitable or if there is a different and more efficient way to do it.
It is a very simple program that relates the classes: Bank, Current Account, Savings Account and Client (I apologize for the size of the code in the example);
using static System.Console;
public class Cliente
{
private string nome;
private float saldo;
public Cliente(string nome)
{
this.nome=nome;
saldo=0;
}
public string GetNome()
{
return nome;
}
public float GetSaldo()
{
return saldo;
}
public void SetSaldo(float atualizar_saldo)
{
saldo+=atualizar_saldo;
}
}
public class ContaCorrente
{
private Cliente cliente;
public void AbrirConta()
{
cliente = new Cliente("João");
cliente.SetSaldo(50.0f);
}
public void FecharConta()
{
cliente = null;
}
public void Sacar(float valor)
{
cliente.SetSaldo(valor);
}
public void Depositar(float valor)
{
cliente.SetSaldo(valor);
}
public Cliente GetCliente()
{
return cliente;
}
}
public class ContaPoupança()
{
private Cliente cliente;
public void AbrirConta()
{
cliente = new Cliente("Maria");
cliente.SetSaldo(100.0f);
}
public void FecharConta()
{
cliente = null;
}
public void Sacar(float valor)
{
cliente.SetSaldo(valor);
}
public void Depositar(float valor)
{
cliente.SetSaldo(valor);
}
public Cliente GetCliente()
{
return cliente;
}
}
public class Banco
{
private ContaCorrente conta_corrente;
private ContaPoupança conta_poupança;
public void IniciarContaCorrente()
{
conta_corrente = new ContaCorrente();
conta_corrente.AbrirConta();
}
public void IniciarContaPoupança()
{
conta_poupança = new ContaPoupança();
conta_poupança.AbrirConta();
}
public void InformaçõesDaConta()
{
WriteLine($"Nome do Cliente Conta Corrente: {conta_corrente.GetCliente().GetNome()}");
WriteLine($"Saldo em sua Conta: {conta_corrente.GetCliente().GetSaldo()}");
WriteLine($"Nome do Cliente Conta Poupança: {conta_poupança.GetCliente().GetNome()}");
WriteLine($"Saldo em sua Conta: {conta_poupança.GetCliente().GetSaldo()}");
}
}
public class MainProgram
{
public static void Main(string[] args)
{
Banco banco = new Banco();
banco.IniciarContaCorrente();
banco.IniciarContaPoupança();
banco.InformaçõesDaConta();
}
}
My goal in this code was to test if it would work as I hoped it would, also better understand how relationships between classes work and how they should be done.
The Client will always be "John" to
ContaCorrente
and always "Maria" toContaPoupanca
? The Balance should not be from the accounts instead of theCliente
? A customer cannot have a checking and saving account? Why didn’t you include the class diagram in the question?– Leandro Angelo
The part of the customer’s name was intentional, for example only. The part of the balance I actually did the wrong reading, should even be owned by the current accounts and savings. Is that this is my first contact with class relationship, I’m still learning about the diagram :) Thanks for the comment
– user237967