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.
The personal property of the motorcycle class must be initialized, did you perform the initialization of it? if yes just use
Pessoa.Nome
– Felipe Assunção
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 calledPessoa
? 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.– Maniero
@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()
.– Wesley Heron