I have the most generic class Account and another ContaPoupaça
that inherits from this class Conta
. In this daughter I have an interface called ITributavel
, because this class in addition to the methods and attributes of Conta
, has a ITributavel
method.
I have another class TotalDeTributos
that has a method that does an operation with class ContaPoupanca
that has ITributável
. In the AdicionaConta
method, of the photo below, I put as argument count of type Conta
and it is giving this error, I already tried to use casting to convert novaConta
but gives another error. >
I do not want the argument to count as type ContaCorrente
, because other classes that inherit from the Account will have ITributável
and another not.
What am I doing wrong?
Form1:
using System;
using System.Windows.Forms;
namespace Banco2
{
public partial class Form1 : Form
{
int indice1 = 0;
int indice2 = 0;
private int indiceNovaConta = 0;
string tipoConta = "";
private Conta[] contas = new Conta[10];
TotalDeTributos tributos;
ContaPoupanca c1;
ContaInvestimento c2;
Conta selecionada;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
c1 = new ContaPoupanca();
c1.Titular = new Cliente("Rodrigo");
c1.Numero = 1;
this.AdicionarConta(c1);
c2 = new ContaInvestimento();
c2.Titular = new Cliente("Diego");
c2.Numero = 2;
this.AdicionarConta(c2);
tributos = new TotalDeTributos();
AtualizaTributo();
lblValor.Text = "0";
}
private void btnDeposita_Click(object sender, EventArgs e)
{
double valorAcao = Convert.ToDouble(lblValor.Text);
contas[indice1].Deposita(valorAcao);
MessageBox.Show("Deposito realizado.");
txtSaldo.Text = Convert.ToString(contas[indice1].Saldo);
lblValor.Text = "0";
AtualizaTributo();
}
private void btnSaque_Click(object sender, EventArgs e)
{
double valorOperacao = Convert.ToDouble(lblValor.Text);
contas[indice1].Saque(valorOperacao);
txtSaldo.Text = Convert.ToString(contas[indice1].Saldo);
lblValor.Text = "0";
}
private void btnTransferir_Click(object sender, EventArgs e)
{
double valorOperacao = Convert.ToDouble(lblValor.Text);
contas[indice1].Transfere(valorOperacao, contas[indice2]);
txtSaldo2.Text = Convert.ToString(contas[indice2].Saldo);
txtSaldo.Text = Convert.ToString(contas[indice1].Saldo);
lblValor.Text = "0";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
indice1 = comboBox1.SelectedIndex;
selecionada = this.contas[indice1];
txtTitular.Text = selecionada.Titular.Nome;
txtSaldo.Text = Convert.ToString(selecionada.Saldo);
txtNumConta.Text = Convert.ToString(selecionada.Numero);
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
indice2 = comboBox2.SelectedIndex;
selecionada = this.contas[indice2];
txtTitular2.Text = selecionada.Titular.Nome;
txtSaldo2.Text = Convert.ToString(selecionada.Saldo);
txtNumConta2.Text = Convert.ToString(selecionada.Numero);
}
public void AdicionarConta(Conta novaConta)
{
if (novaConta is ContaPoupanca)
{
this.tipoConta = "Conta Poupança";
}
else if (novaConta is ContaCorrente)
{
this.tipoConta = "Conta Corrente";
}
this.contas[this.indiceNovaConta] = novaConta;
Conta selecionada = this.contas[this.indiceNovaConta];
comboBox1.Items.Add(selecionada.Titular.Nome + " - Tipo: " + this.tipoConta);
comboBox2.Items.Add(selecionada.Titular.Nome + " - Tipo: " + this.tipoConta);
if(selecionada is ContaPoupanca)
{
MessageBox.Show(selecionada.GetType().ToString());
tributos.Acumula((ITributavel)selecionada);
}
indiceNovaConta++;
}
private void btnCadastrar_Click(object sender, EventArgs e)
{
FormCadastrarConta formCadastrarConta = new FormCadastrarConta(this);
int indiceUltimaConta = contas.Length;
formCadastrarConta.ShowDialog();
}
public void AtualizaTributo()
{
if (tributos != null)
{
foreach(Conta conta in contas)
{
if(conta is ContaPoupanca || conta is ContaInvestimento)
{
tributos.Acumula((ITributavel)conta);
}
}
txtTributos.Text = Convert.ToString(tributos.Total);
}
}
}
}
TotalTymbols:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Banco2
{
class TotalDeTributos
{
public double Total { get; private set; }
public void Acumula(ITributavel conta)
{
this.Total += conta.CalcularTributo();
}
}
}
Taxable:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Banco2
{
interface ITributavel
{
double CalcularTributo();
}
}