-2

I'm new to C#. I have read about multilevel inheritance and how it works. My program has a multilevel inheritance. The parent class is Cuenta, and classes Prestamo and Ahorro each derive from their Parent class. The code shows no errors; however, when the program reaches the Cheque constructor, it redirects me to the Ahorro class instead of executing the rest of the code in the Cheque constructor. Any idea how to get around this? Any help would be much appreciated.

The code for the Cheque class is as follows:

using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProyectoFinal
{
    public class Cheque: Ahorro
    {
        double depositoInicial;

        string[] args;
        
        public Cheque()
        {
            int opc = 0;
            Console.WriteLine("Entre numero de la cuenta");
            Numero = Console.ReadLine();
          
            Console.WriteLine("Entre el deposito inicial");
              depositoInicial = Convert.ToDouble(Console.ReadLine());
              do
              {
              Input:

                  Console.WriteLine("Escoja una opcion");

                  Console.WriteLine("1. Crear cuenta de cheque");
                  Console.WriteLine("2. Añadir deposito a cuenta de cheque ");
                  Console.WriteLine("3. Retirar dinero de cuenta de cheque ");
                  Console.WriteLine("4. Calcular intereses (se puede entrar una nueva cantidad de dinero)");
                  Console.WriteLine("5. Ver Balance");
                  Console.WriteLine("6. Ir al menu principal");
                  opc = Int16.Parse(Console.ReadLine());
                  if (opc == 1)
                  {
                      Cheques(depositoInicial);
                      goto Input;
                  }
                  else if (opc == 2)
                  {
                      double deposito = 0;
                      Depositar(deposito);
                      goto Input;
                  }
                  else if (opc == 3)
                  {
                      double retiro = 0;
                      Retirar(retiro);
                      goto Input;
                  }
                  else if (opc == 4)
                  {
                      VerBalance();
                      goto Input;
                  }
                else if (opc == 5)
                {
                    Program.Main(args);
                }
                else if (opc > 5 || opc < 1)
                  {
                      Environment.Exit(0);
                  }
              } while (opc > 5 || opc < 1);
              Console.WriteLine();
          } 
        
        public double Cheques(double depositoInicial)
        {
            double code = 0;
            BalanceInicial = depositoInicial;
            BalanceActual = depositoInicial;
            TipoCuenta = "Checks";
            Intereses = 0;
            Console.WriteLine($"Balance Inicial y balace actual iniciados a {depositoInicial}");
            return code;
        }
       public double Depositar(double deposito)
        {
               new Ahorro().Depositar(deposito);
               return 0; 
        }
       public string Retirar(double retiro)
        {
            new Ahorro().Retirar(retiro);
            return "";
        }

        public void VerBalance()
        {
            new Ahorro().VerBalance();
           
        }
    }
}

The code for the Ahorro class is as follows:

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

namespace ProyectoFinal
{
    public class Ahorro: Cuenta
    {
        public double Intereses;
        string UltimaTransaccion;

        double depositoInicial;
        string[] args;
        public Ahorro()
        {
            
            int opc = 0;
            Console.WriteLine("Entre numero de la cuenta");
            Numero = Console.ReadLine();
            Console.WriteLine("Entre el deposito inicial");
            depositoInicial = Convert.ToDouble(Console.ReadLine());
            do
            {
            Input:
                
                Console.WriteLine("Escoja una opcion");
                
                Console.WriteLine("1. Crear cuenta de ahorro o de cheque");
                Console.WriteLine("2. Añadir deposito a cuenta de ahorro o de cheque ");
                Console.WriteLine("3. Retirar dinero de cuenta de ahorro o de cheque");
                Console.WriteLine("4. Calcular intereses (se puede entrar una nueva cantidad de dinero, No se puede usar con cuenta de cheques)");
                Console.WriteLine("5. Ver Balance");
                Console.WriteLine("6. Ir al menu principal");
                opc = Int16.Parse(Console.ReadLine());
                if (opc == 1)
                {
                    Ahorros(depositoInicial);
                    goto Input;
                }
                else if (opc == 2)
                {
                    double deposito = 0;
                    Depositar(deposito);
                    goto Input;
                }
                else if (opc == 3)
                {
                    double retiro = 0;
                    Retirar(retiro);
                    goto Input;
                }
                else if (opc == 4)
                {
                    double cantidad = 0;
                    double intereses = 0;
                    CalcularInteres(cantidad, intereses);
                    goto Input;
                }
                else if (opc == 5)
                {
                    VerBalance();
                    goto Input;
                }
                else if (opc == 6)
                {
                    Program.Main(args);
                }
                
                else if (opc > 6|| opc < 1)
                {
                    Environment.Exit(0);
                }
            } while (opc > 6 || opc  < 1);
            Console.WriteLine();
        }   
        public string Ahorros(double depositoInicial)
        {
            double deposito = 0;
            BalanceInicial = depositoInicial;
            BalanceActual = depositoInicial;
            Console.WriteLine("Que tipo de cuenta usted desea crear?");
            string cuenta = Console.ReadLine();
            if (cuenta == "Ahorro" || cuenta == "ahorro")
            {
                TipoCuenta = "Savings";
                Intereses = 0.06;
            } else if (cuenta == "Cheque" || cuenta == "cheque")
            {
                TipoCuenta = "Checks";
                Intereses = 0;
            }
            
            Console.WriteLine($"Informacion de cuenta de ahorro \nBalance Inicial: {BalanceInicial.ToString("C2")}\nBalance Actual: {BalanceActual.ToString("C2")}\nTipo de Cuenta: {TipoCuenta}");
            return TipoCuenta;
        }//Menu program
        //Account prestamo ahorro cheque
        public double Depositar(double deposito)
        {
            if (BalanceActual ==0)
            {
                Console.WriteLine("ERROR: No puede usar esta funcion a menos de que usted haya creado una cuenta en opcion 1.");
                goto returnInput;
            }
            Console.WriteLine("Entre cantidad adicional de deposito");
            deposito = Convert.ToDouble(Console.ReadLine());
            BalanceActual = BalanceActual + deposito;
            UltimaTransaccion = "deposito";
            Console.WriteLine($"El balance de la cuenta es {BalanceActual.ToString("C2")} y el tipo de cuenta es de {UltimaTransaccion}");
            CantididadUltimaTransaccion = depositoInicial;
            returnInput:
            return BalanceActual;
        }
        public string Retirar(double retiro)
        {
            if (BalanceActual == 0)
            {
                Console.WriteLine("ERROR: No puede usar esta funcion a menos de que usted haya creado una cuenta en opcion 1.");
                goto returnInput;
            }
        retiroInput:
            Console.WriteLine("Entre cantidad de dinero para retirar");
            retiro = Convert.ToDouble(Console.ReadLine());
            if (retiro > BalanceActual)
            {
                Console.WriteLine("NO PUEDE RETIRAR ESA CANTIDAD");
                goto retiroInput;
            }
            BalanceActual = BalanceActual - retiro;
            UltimaTransaccion = "retiro";
            CantididadUltimaTransaccion = retiro;
            
            Console.WriteLine($"El balance de la cuenta es {BalanceActual.ToString("C2")} y el tipo de cuenta es de {UltimaTransaccion}");
            returnInput:
            return "";
        }
        public double CalcularInteres(double cantidad, double interes)
        {
            if (TipoCuenta != "Savings")
            {
                Console.WriteLine("Esta funcion solamente se puede usar con cuenta de ahorro");
                goto MainMenu;
            }
            Console.WriteLine("Entre cantidad de dinero que desea calcular con intereses");
            cantidad = Convert.ToDouble(Console.ReadLine());
            interes = 0.06;
            BalanceActual = cantidad * (1 + interes);
            Console.WriteLine($"El balance de la cuenta es {BalanceActual.ToString("C2")} y el tipo de cuenta es de {UltimaTransaccion}");
            MainMenu:
            return BalanceActual;
        }
        
        public void VerBalance()
        {
            Console.WriteLine($"Numero de cuenta: {Numero}");
            Console.WriteLine($"Tipo de cuenta: {TipoCuenta}");
            Console.WriteLine($"Balance inicial fue: {BalanceInicial.ToString("C2")}");
            Console.WriteLine($"Balance actual es: {BalanceActual.ToString("C2")}");
            Console.WriteLine($"La ultima transaccion echa es de: {UltimaTransaccion.ToString()}");
            Console.WriteLine($"La cantidad de la ultima transaccion es: {CantididadUltimaTransaccion.ToString("C2")}");
        }
    }  
}
  • 4
    There is *a lot*--and I mean *A LOT*--of code that's unrelated to your inheritance question. Please reduce it to a [mre]. – madreflection Dec 05 '21 at 22:09
  • @madreflection To summarize my question, I would like to understand why a constructor from an inherited class is redirecting me to the parent class instead of executing the code from the constructor of the inherited class. I left the code in case more information was needed to solve the problem. – Felipe Xavier Diaz Dec 05 '21 at 22:19
  • Where is the code wherein you instantiated an instance of the inherited class? How did you instantiate an instance of Cheque class? – Joseph Dec 05 '21 at 22:20
  • @madreflection The next time I ask a question, I will keep that in mind. – Felipe Xavier Diaz Dec 05 '21 at 22:20
  • That's great for the next question, but you need to do that for this question, too. Don't just summarize in a comment. – madreflection Dec 05 '21 at 22:23
  • @Joseph The Cheque class inherits from the Ahorro class 'public class Cheque: Ahorro' and the constructor of the class that is giving me problems is ' public Cheque()' – Felipe Xavier Diaz Dec 05 '21 at 22:24
  • This would make a lot more sense (and give you a better experience) if you just edited your question so that it only contains the relevant parts. Stop replying to comments and start editing the question down to what was requested. – madreflection Dec 05 '21 at 22:24
  • Part of the problem might be terminology, so explanations just make it harder to understand. There is no "redirection" in classes. That's a web request terminology. Are you asking why the base class's constructor gets called before the body of the derived class runs? – madreflection Dec 05 '21 at 22:27
  • @madreflection The inherited class is `public class Cheque: Ahorro {` and the constructor that is giving me problems is `public Cheque()` – Felipe Xavier Diaz Dec 05 '21 at 22:28
  • @madreflection Yes, that is my question – Felipe Xavier Diaz Dec 05 '21 at 22:29
  • I'm not going to look for that the way it's written; few people would. Edit the question down. – madreflection Dec 05 '21 at 22:29
  • @madreflection Never mind about how I wrote my question. My question is why the base class's constructor gets called before the body of the derived class runs. – Felipe Xavier Diaz Dec 05 '21 at 22:31
  • @madreflection That's all I want to know. – Felipe Xavier Diaz Dec 05 '21 at 22:32
  • Does this answer your question? https://stackoverflow.com/questions/1882692/c-sharp-constructor-execution-order – madreflection Dec 05 '21 at 22:33
  • *"Never mind about how I wrote my question"* - That's a great way to get a question downvoted and ignored. Quality counts here. – madreflection Dec 05 '21 at 22:34
  • @madreflection Yes, it does – Felipe Xavier Diaz Dec 05 '21 at 22:34
  • I think that this is the information you're after? https://www.csharp411.com/c-object-initialization/ – FriedLychees Dec 05 '21 at 22:34
  • 2
    Pasting hundreds of lines of code like this will just get you down votes... you need to put some effort into creating a minimal reproducible example. – Helder Sepulveda Dec 05 '21 at 22:37

1 Answers1

0

The code shows no errors; however, when the program reaches the Cheque constructor, it redirects me to the Ahorro class instead of executing the rest of the code in the Cheque constructor.

The code will reach the cheque constructor if you initialized an instance of it say:

Cheque c = new Cheque();

Since Cheque inherits from Ahorro, the code will execute the implementation from Ahorro constructor followed by Cheque

https://dotnetfiddle.net/6Jy5wO

If you want your Cheque and Ahorro to have different implementations, then Cheque must not inherit from Ahorro. Rather, have them inherit both from a parent class because in multi-level inheritance, a derived class will inherit a base class and as well as the derived class also act as the base class to other class.

Joseph
  • 502
  • 4
  • 15