Take and display data from a class attribute (class association)

Asked

Viewed 1,104 times

2

I’m having a little trouble picking up a value from an attribute of the type class.

Classe Pessoa

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 don’t want the method AddMoto i inform the holder of the bike, just like a way to catch the amount that is already saved in the variable Nome class Pessoa.

  • The personal property of the motorcycle class must be initialized, did you perform the initialization of it? if yes just use Pessoa.Nome

  • I’m finding these classes very odd. And there’s no clear explanation of what you want, what your difficulty is. So I’m lost in how to help you. The solution to what you’re asking for is even easy, but it’s something completely meaningless. Because a Moto must have a property called Pessoa? I can’t imagine an explanation plausible. So the problem seems to be there. If you stop having this, it starts to make more sense. Not so much yet. Mixing the input and output logic of data is torando all very confusing. Try to explain better what you really want.

  • @Bigown was actually looking to make an association between the Moto and Pessoa class, where what I would like to say is that a person can own a motorcycle. The goal of doing this is that when display the data of the bike I show who is the owner of the bike, but without the need to during the registration of the data of the bike I have to inform again who is the holder because I’ve done it in the method Add().

1 answer

2


The design is bad, is mixing things. I do not know if I understand what you really want. I will change to a form that is correct. It still won’t be so right because classes mix things that should be in separate places. I understand it’s just an artificial exercise, but learning wrong from it won’t help.

The bike should not have knowledge about the person who holds it. If by chance this is necessary, then the design would have to be changed even more. And it would have to have a good justification. Then making the bike manipulate only data of the bike and the customer only customer data (which has as an integral part the person), everything is right.

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 changed requirements. But if you do wrong on the exercise, you’ll learn wrong later.

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

Perhaps it was the case to use a constructor and not this method Add(). Especially if you take the input and output logic of data that shouldn’t be next to the data. To fix the classes would have to redo it so radically that it would become something else entirely.

It’s also very strange to have a method Add() in two classes and one AddMoto() in the other, it makes no sense.

There are other problems in the code. I won’t try to solve everything.

using static System.Console;

public class Program {
    public static void Main() => new Cliente().Add();
}

public class Pessoa {
    public string Nome { get; set; }
    public int Idade { get; set; }
    public virtual void Add() {
        Nome = ReadLine();
        Idade = int.Parse(ReadLine()); //não é assim que faz, só pra simplificar
    }
}

public class Cliente : Pessoa {
    public int Codigo { get; set; }
    public override 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);
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I wonder why a Pessoa and not a Cliente may own a Moto in this case. Even if this is possible, then Cliente is a thing and Pessoa is another. There the Cliente might even be composed of a Pessoa, but not be to Pessoa. That is, the design I’d be even more wrong. Considering this hypothesis and considering that the data of the person and bike can be null (another code will take care of it) I made a better code but still having problems. Something like that:

using static System.Console;

public class Program {
    public static void Main() {
        var pessoa = new Pessoa();
        var moto = new Moto(pessoa);
        var cliente = new Cliente(moto);
        cliente.ExibirDados();
        cliente.Moto.ExibirDados();
        cliente.Pessoa.ExibirDados();
    }
}

public class Pessoa {
    public string Nome { get; set; }
    public int Idade { get; set; }
    public Pessoa() {
        Write("Nome: ");
        Nome = ReadLine();
        Write("Idade: ");
        Idade = int.Parse(ReadLine());  //não é assim que faz, só pra simplificar
    }
    public void ExibirDados() => WriteLine($"Nome: {Nome}");
}

public class Cliente {
    public int Codigo { get; set; }
    public Pessoa Pessoa { get; set; }
    public Moto Moto { get; set; }
    public Cliente() {
        Write("Código do cliente: ");
        Codigo = int.Parse(ReadLine());
    }
    public Cliente(Pessoa pessoa) : this() => Pessoa = pessoa;
    public Cliente(Moto moto) : this(moto.Pessoa) => Moto = moto;
    public void ExibirDados() => WriteLine($"Código: {Codigo}");
}

public class Moto {
    public string Cor { get; set; }
    public Pessoa Pessoa { get; set; }
    public Moto() {
        Write("Cor: ");
        Cor = ReadLine();
    }
    public Moto(Pessoa pessoa) : this() => Pessoa = pessoa;
    public void ExibirDados() => WriteLine($"Cor: {Cor}");
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Note that this cross-reference of Cliente who has a Pessoa X Moto who also has a Pessoa I don’t like it. But then I’ll have to go further.

I’m not a fan of the term attribute there, I prefer field.

  • 1

    I think that if I had another class as for example employee, I might have to indicate that that bike belongs to the employee who is being registered along with the data of the bike or the customer as was the case. That is why a Person is the owner of the bike. But in fact I can be wrong in my logic. Good but the idea is really only a basic exercise what I wanted here was to compose two classes and access the attributes of another through the use of properties of the type of a class. We accept suggestions for improvement or alternative.

  • Good and if it had an employee class that inherited also of Person and it had also a bike?

  • I think it’s getting even more confusing. I showed that the thing is simpler than it looks. The problem with artificial exercise is that the requirements can be crazy, meaningless. Then the codes to meet these things will be crazy and meaningless. If you don’t have a well-defined problem, you won’t have a usable code. I understand what you said, but it wouldn’t work out the way you were doing it. Now I put an example that can be adapted to have the employee, although this does not make sense either. Employees do not own motorcycles. If the customer happens to be an employee, it’s another problem.

  • Note that the main problem of the code is a consequence of the wrong definition of what the problem is. There are also errors of structuring the code itself, but this is useful in practice. What you can’t do is do something that doesn’t make sense and won’t actually solve any problems. Depending on the context of the problem I would not even put person as owner of the bike but the customer. There are still several improvements that could be made there, just to be able to let it be more extensible. Although not the way you’re thinking.

  • Thanks, with your help I was able to adjust my code better!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.