• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating objects from user input

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys

im trying to create a simple program to take a team name from a user,create the object and add it to an arraylist but im finding it harder than i thought

could an object be created from a method within the object im trying to instantiate?

heres what i have so far...but im finding actual design...of how things all fit together a problem.




can i have a method in a class create an object and put it into an arraylist?

can somebody just quickly outline how this should be setup...please
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could, but you would have to declare the method static, otherwise there's no way to call the method before creating an object of the class.
I don't understand why you would do that. I would eather ask the user for the teamname in the mainmethod and set it in the constructor or use a setter method to set the teamname
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks daniel yes...i agree was just toying with the idea...ii have done all the basics but am battling to string things together
which is the better way to do things in the constructor or with getters and setters or does it not really matter in this situation?
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would op for the constructor
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you should create the Team class first and get that working before trying anything else. Are you going to use a public constructor or a factory method?
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Campbell is a public constructor just a constructor or is it a special constructor no I have had no experience with factory methods but I will read up on it... So first thing to instantiate team objects?  That should be very easy as there is no data yet... All I want is a user created team name to create the associated object and put it in an array list... As always thanks for your help guys..
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A public constructor is a basic constructor. I would seperate your buiseness logic from your teamclass.
As I would do it, you make your teamclass with a private variable teamname, a getTeamNameMerhod and a constructor that takes the teamname as a parameter.
The creating of the object and the aadding to the list I would do in my main.
If you create your list with teamnames in your teamclass you will create a new one for each team.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A factory method is often paired with constructors which are all private, which forces users to use the factory methods and also makes it impossible to subclass this class.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"The creating of the object and the aadding to the list I would do in my main. "

shouldnt it be in a method though...as what if a want to add,delete,list the teams in my arraylist...isnt that awkward from just main?

Campbell...i didnt really understand your technical description there...well i did s bit...just not the practical application.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could listing the teams in the arraylist be a static method? or should this always be avoided? and...why?
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry guys...yes im jumping the gun a bit...will focus on the Team class and get that working first...sorry again
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:"The creating of the object and the aadding to the list I would do in my main. "

shouldnt it be in a method though...as what if a want to add,delete,list the teams in my arraylist...isnt that awkward from just main?



That sounds to me like some sort of TeamManager.
It would have the List<Team> and handle all the changes to that List.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It could but no point in doing so, generally you suposed to only declare utility methods static.
If you want to seperate your buisiness logic, in this case adding or removing a team from the list, you could create the list in your main and make methods in your teamclass and give the list as a parameter, but that's also seems kind of weird. A factory method would return an object of an surtain class based on the information you provided, but I don't see why you would use this here, since all you want to do is create a team that has a teamname and at it to an list.
If you had for example an interface team and some classes like footballteam, baseballteam and volleyballteam that all implement the teaminterface you could wright an teamfactory to deside which kind of team object should be created.
It all depends on how extensive the final program is going to be.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very interesting thanks guys...

no this particular project is only about soccer teams so no need to elaborate too much just trying to get some key concepts down that i imagine would be useful
would it be necessary to have a clubs class then a sub class teams off that...would it make it more flexible and extensive down the line?

 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the example you provided I wouldn't bougther, if you're talking about multiple devissions all with their teams yes
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so daniel just to clarify

i want to have a menu

1) list all teams
2) Add team
3) remove team
3) quit

so i will need methods
addTeam
removeTeam
listTeams

quit can just be done from the menu

do those methods go in another class called LeagueTable for example,
then the Team class can take in the parameter as a name...i dont know i feel lost


 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you going to use as frontend?
If you would put your methods in a seperate class you once again would have to make them static, or else you would have to create an object to acces them...

That's why I ask what you use as frontend. Normally you would have your entityclass being Team and a or several controllerclass(es) where you put your methods in.
If you just use commandline I would wright an interface and let my entityclass implement it.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ignore my previous remark. For contoleline I would put the methods in my main. I'll make an example
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This cpuld be your main method for example:

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't put so much code in the main() method. Don't put so much code in each case: use separate methods.Don't use System#exit. If you have a loop with while(option != 4) in, that loop will terminate whenever 4 is entered, and you simply allow control to pass to the end of the method.
Don't create multiple Scanner objects reading from System.in. You only need one Scanner instance.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is factory method the same as factory design pattern...are factory methods just part of the factory design pattern? just watching a video on factory design patterns?
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should my array list that will contain all my teams be static? would that make sense?
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so here is what i have so far:









ignore the commented out code that was my object creation from user input,it works but i need to assemble this program better now.

in the TeamManager class i have a static int variable that will keep track of how many teams have been created

in the TeamManager class i will have the various methods to add a team,minus a team etc etc

ok so a few questions:

where would the best place be to put the arraylist?

shouldnt it be a static arraylist with static methods accessing the arraylist as it will be created and will always keep it in memory and make it available from everywhere in the program?
as what happens when somebody lists a team,and havent created a team yet...the arraylist needs to be initialised first but if i do that in a local scope it wont be able to be used later in the program

thanks for reading looking forward to feedback...


 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i added this method:



to TeamManager

and this instance variable
ArrayList<String> teamList = new ArrayList<String>();

but in this code:



TeamManager.getTeamList method is not available...how can i have it use methods of a class without instantiating the class? surely the arraylist has to be static...or have i got something wrong?

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:in the TeamManager class i have a static int variable that will keep track of how many teams have been created

in the TeamManager class i will have the various methods to add a team,minus a team etc etc

ok so a few questions:

where would the best place be to put the arraylist?

shouldnt it be a static arraylist with static methods accessing the arraylist as it will be created and will always keep it in memory and make it available from everywhere in the program?
as what happens when somebody lists a team,and havent created a team yet...the arraylist needs to be initialised first but if i do that in a local scope it wont be able to be used later in the program

thanks for reading looking forward to feedback...



Good, now we're talking about a design. You can't decide whether a variable should be static until you understand the design where it's going to live.

So you have a TeamManager class. That sounds like a class to manage a team, but based on the fact that it keeps track of how many teams have been created, maybe it's actually a class to manage the teams? Well, yes, that's what it is because it has methods to add and delete teams. So it would be better to call it TeamsManager, or maybe League or something like that.

Okay. A class to manage the teams. Sounds good. If it's going to manage the teams then perhaps it would be a good idea for it to have a list of the teams? I think so. Then that answers the question of where the list of teams should go. (It also means that TeamsManager doesn't need a variable to keep track of the number of teams, since it can get that directly from the list of teams.)

But now since the TeamsManager has the responsibility for maintaining the list of teams, that means that other code has to come to the TeamsManager and ask for the list of teams. The easiest way to implement that is for TeamsManager to have a getter method which returns that list. This is actually frowned on in the object-oriented design world, because if you get the list of teams from TeamsManager then you can mess about with the list, adding and deleting Teams outside of the manager. This is a bad thing. But for getting started, let's say you provide that getter method and promise never ever to modify the list outside of TeamsManager? That would keep it simple and move you forward in the design.

 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul: He already has a getter metthod, getTeamList.

TeamManager.getTeamList method is not available...how can i have it use methods of a class without instantiating the class? surely the arraylist has to be static...or have i got something wrong?  



If you want to be able to acces the getMethod without initializing the class, you should declare the method static, not the variable, but in this case I wouldn't.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He has a method called "getTeamList", but that is a poor name for the method he has written.
It would be better for him to call that method "printTeamList", since that is what it does.
I would expect a method named "getTeamList" to return the arraylist, not print it.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks fred yes that makes sense
thanks all,will try a few things tomorrow
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel "If you want to be able to acces the getMethod without initializing the class, you should declare the method static, not the variable, but in this case I wouldn't."

so whats the alternative?
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on what you realy want to do with the program, for the moment you don't have a way to save you data, every time you run your program you start with an empty list. I would eather use a db to store the data, embeded or otherwise or use a file to write the data to and get it from there.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The alternative is to not use any static variables. Here's what your code ought  to look like:


 
reply
    Bookmark Topic Watch Topic
  • New Topic