• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

My "Main" class wants to act as "manager" for two objects. How do I do this cleanly?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the title, I don't really know the proper terms to summarize my situation.

Premise: I have an application that tracks scores for two Basketball (3x3) teams. On the "main" class, I instantiate two Team objects to represent the competing teams. Then I bind the UI elements (Buttons and TextViews) to their respective methods. See my stellar UI for context



(I feel like the implementation details of Team is not necessary for the discussion) Anyway, my problem is implementing a way to track the winner. The fouls, scores, and penalty are calculated independently between teams. Therefore I could just pass a TextView as a dependency on the Team class, delegating the updating of the UI to the Team class itself. As for determining the winner, the calculation is based on each team's scores. I can't think of a clean way for the MainActivity class to solve my problem.

Any tips? Thanks in advance!

 
Marshal
Posts: 77557
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid Iit rather looks as if you had gone around it the wrong way. You give the impression of writing code before you have planned what to write. I think you need a plan for what the game is going to do, completely devoid of programming terms or names of classes. You shouldn't even think of a GUI until after you have got the logic (the game classes working). I suggest you write down the description of what the game is all about. Then you can draw a diagram showing the different classes you are going to write. I would have thought, if you have Game, Team, and Score, you might have a public interface on the Game class including this sort of method:-Once you have got that all working without a GUI, you can add a GUI atop it. Don't start writing FXML or similar until you have it all working. Maybe you should have all actions by both teams called from methods of the Game class.
I think people shouldn't think in terms of main class (even though NetBeans always seem to create a class of that name). I think you want a very short and simple main method(), like this:-You will have to change that if you are working in a class which extends Application.
 
John Alcher Doloiras
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the warm welcome!

I think people shouldn't think in terms of main class..



Yeah, what I meant on that was the Main class being the main class that handles the state for both Team objects. (This is an Android app, btw).

Anyway, I think that your point on writing the whole app without a GUI first is a great idea. I'd do that and report back
 
Campbell Ritchie
Marshal
Posts: 77557
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Alcher Doloiras wrote:Thank you for the warm welcome!

that's a pleasure

. . . the Main class . . . handles the state for both Team objects.

There is a simple solution: call the class Game and we shall be in agreement.

(This is an Android app, btw).

Sorry; I mistook it for FX. I shall add you to the Android forum.

Anyway, I think that your point on writing the whole app without a GUI first is a great idea. . . .

That is the usual advice, and the way I was taught to write GUI applications. Looking forward to the feedback.
 
Rancher
Posts: 516
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an idea I have.

You may consider a winner button and its action, to determine and show the winner. This button's listener has logic to arrive at the winning team, based on the score (or its calculation).

So in the MainActivity class, within this winner button's listener:

TextView scoreHomeText = (TextView) findViewById(R.id.home_score_text_view);
TextView scoreAwayText = (TextView) findViewById(R.id.away_score_text_view);

- Get the home and away's corresponding integer score values.
- Figure who's the winner (all your complex calculations here).
- Display the winner's name, etc in the appropriate text view...

 
Campbell Ritchie
Marshal
Posts: 77557
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good idea; what would happen if you click the winner button before the game has finished?
 
Prasad Saya
Rancher
Posts: 516
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what would happen if you click the winner button before the game has finished?



Can change the button name to: Game complete/end/over; something like that. And then the winner team name can be displayed when the game is over.  I think that's pretty much within the scope of the app.

The new Game Done button can be placed between the Time Expired and Reset Scores buttons.
 
John Alcher Doloiras
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

That "winner button" is essentially what the "TIME EXPIRED" button does (or supposed to, its not yet implemented) in my app. (See UI). Basically in 3x3 basketball, if the time expires, the team with the most points wins. But another way of winning is getting to 21 points first (which is my core problem -- how would the Game class be notified if a team reaches 21). I'm still working on my code
 
Campbell Ritchie
Marshal
Posts: 77557
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try researching the observer pattern.
 
Prasad Saya
Rancher
Posts: 516
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Alcher Doloiras wrote:Hello! But another way of winning is getting to 21 points first (which is my core problem -- how would the Game class be notified if a team reaches 21).


Take a look at: TextView's addTextChangeListener method and TextWatcher.

The definition from Android API: addTextChangedListener(TextWatcher watcher)
Adds a TextWatcher to the list of those whose methods are called whenever this TextView's text changes.

Here is a possibility. When the score text view changes (gets updated/changed and displayed), the listener attached to the score can - arrive at the winner and display.
 
Prasad Saya
Rancher
Posts: 516
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Continuing from the previous message...
Note that in TextWatcher interface there are three methods to be overridden. The code related to arriving at the winner and displaying it need to be in the onTextChanged() method. The other methods (afterTextChanged and beforeTextChanged) can be empty, without any code.
 
John Alcher Doloiras
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys! I tried to implement my 3x3 app with the Observable pattern. Did I got the gist of the pattern right? Here's the repo

I'm currently working on the Android version with Prasad Saya's suggestions
 
Campbell Ritchie
Marshal
Posts: 77557
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the code; many people are reluctant to look in unfamiliar repositories.
 
Prasad Saya
Rancher
Posts: 516
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Alcher Doloiras wrote:Hello guys! I tried to implement my 3x3 app with the Observable pattern. Did I got the gist of the pattern right?



Here is a blog post I had written sometime back, but it uses a Java Swing GUI example. It will give you an idea how the Observer and Observable can be used within a GUI application:
Java Swing Example using Observer and Observable
 
John Alcher Doloiras
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "entry point" of the app.



Game.java



Team.java



Observable.java



Observer.java


 
Happily living in the valley of the dried frogs with a few tiny ads.
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic