0
I have the most generic class Account and another ContaPoupaça
who inherits from that class Conta
. In this daughter I have an interface called ITributavel
, because this class beyond the methods and attributes of Conta
, has a method of ITributavel
.
I have another class TotalDeTributos
that has a method that does an operation with the class ContaPoupanca
who has the ITributável
. In the method AdicionaConta
, of the photo below, I put as argument account of the type Conta
and is giving this error, I have tried to use casting to convert novaConta
but makes another mistake.
I don’t want the script to count as ContaCorrente
, because other classes that inherit from Account will have the ITributável
and another no.
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);
}
}
}
}
Totaldetributos:
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();
}
}
}
Itributavel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Banco2
{
interface ITributavel
{
double CalcularTributo();
}
}
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero
Try catch helps a lot
– Renato Junior