Creating objects from a class within that same class

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
Hi guys

If I have a Program class with main
and i have a Team class

And in the Team class I have fields
teamName
teamValue

then in Team i want methods

CreateTeam
DeleteTeam
AddTeamValue
ViewTeams

now I know in the constructor in my Program class I can go
Team liverpool = new Team(4.5); - 4.5 billion for example, and whatever other properties for the team etc.

now I have a liverpool team object, but how can I have the user actually name the team and add it to an array?

would the actual team manipulation need to be done in a different method in a different class eg: class TeamWork
method AddTeam
get a variable from the user then somehow use that in
Team userVariable = new Team(4.5);

or can it be done from in the Team class, I find this very confusing when working with objects OOP in general, what takes responsibility where and how.
and what is a good design etc.

I hope you understand the difficulty im having and can point me in the right direction.
 
The Team class should represent a team in the real world. It should have properties for the data a team has and methods for the behaviour a team does. A ViewTeams method doesn't make sense because that's not behaviour of a team. CreateTeam and DeleteTeam are in pretty much the same boat. The management of objects of a type is not the responsibility of those objects. Some other object would keep and manage a collection of Team objects and be responsible for creating, deleting and other functions of the collection. The details would depend on the scenario.

If every Team must have a name then it should be a constructor parameter, so you can't create an instance without providing a name. If each Team in a particular context must have a unique name then you need some way to enforce that in that context, e.g. a HashSet<Team>.
 
Very good, will work through it slowly

for starters where would teams be managed/manipulated? maybe a League class?
 
Please explain clearly what is it you are trying to create?

I just wrote you out a whole game management system just for fun, but what's the point in sharing it if its not entirely what you want?
I don't know what you are trying to build. Is this a Game like Fifa or something?
What platform are you building this in? I assume console, but specifics are required to give you a decent answer and pseudo example.

 
Back
Top Bottom