Get and display data of an attribute of type class (association between classes)

2

I'm having a small problem getting a value of an attribute of type class .

Person Class

public class Pessoa
    {
        public string Nome {get;set;}
        public int Idade {get;set;}

        public virtual void Add()
        {
            Nome = Console.ReadLine();
            Idade = int.Parse(Console.ReadLine());
        }

    }

Client Class

public class Cliente : Pessoa
    {
        public int Codigo {get;set;}
        public override void Add()
        {
           base.Add();
           Codigo = int.Parse(Console.ReadLine());

           Moto moto = new Moto();

           moto.AddMoto();
           moto.ExibirDados();

        }

}

Moto class

public class Moto
    {
          public string Cor {get;set;}
          public Pessoa Pessoa {get;set;}

          public void AddMoto()
          {
             Cor = Console.ReadLine();
          }
          public void ExibirDados()
          {
             Console.WriteLine("Titular: " + Pessoa); //Como fazer para mostrar o Nome da Pessoa Cadastrada?
             Console.WriteLine("Cor: " + Cor);
             Console.ReadKey();
          }
}

I do not want the AddMoto method to tell the bike holder, I would just like a way to get the value that is already saved in the Nome variable of the Pessoa class.

    
asked by anonymous 21.01.2016 / 21:26

1 answer

2

design is bad, it's mixing things up. I do not know if I understand what you really want. I'm going to change to a form that's correct. It will not be so correct yet because classes blend things that should be in separate places. I understand it's just an artificial exercise, but learning wrong with it will not help.

The bike should not know about the person who owns it. If this is necessary, then design would have to be changed even more. And it would have to have a good justification. So by having the bike manipulate only data from the bike and the client only customer data (which has as integral part the person), everything is alright.

You have to understand the problem as it really is. Wanting to create an artificial situation will create problems sooner or later. Of course in an exercise will not create problems, after all it will not receive maintenance, will not have requirements changed. But if you do wrong in the exercise, you will learn to do it wrong later.

In the current example there is no reason to have a virtual method. It is not the case to use polymorphism here.

Maybe it was the case of using a constructor and not this Add() method. Especially if you take the input and output logic of data that should not be close to the data. In order to fix the classes, you would have to redo it so radically that it would become something completely different.

It is also very strange to have a Add() method in two classes and a AddMoto() in the other, it does not make sense.

You have other problems with your code. I will not try to solve everything.

public class Cliente : Pessoa {
    public int Codigo { get; set; }

    public void Add() {
        base.Add();
        Codigo = int.Parse(ReadLine());

        var moto = new Moto();

        moto.AddMoto();
        moto.ExibirDados();
        ExibirDados();
    }
    public void ExibirDados() {
        WriteLine("Titular: " + Nome);
    }
}

public class Moto {
    public string Cor { get; set; }

    public void AddMoto() {
        Cor = ReadLine();
    }
    public void ExibirDados() {
        WriteLine("Cor: " + Cor);
    }
}

See working on dotNetFiddle .

I wonder why a Pessoa and not Cliente can own a Moto in this case. Even if this is possible, then Cliente is one thing and Pessoa is another. Here% w / w% can be up to composed by

21.01.2016 / 21:59