Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Business logic running training

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I would like to express the next business logic in code; a user starts running and every 10 minutes(600 seconds) counts as a round. The thing is that in the loop I am searching for a way to express the 4 rounds in seconds and particularly in the if express when a round happens (600, 1200, 1800,2400 secs)...now it is just hardcoded to 600.

Any idea, please?


 
Saloon Keeper
Posts: 9422
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're trying to write object-oriented code, you have it backwards.  The "logic" should be inside the objects, not outside as you have it now. What you should see from the outside is just the behavior. Any data that pertains to that behavior should be managed by the object itself.

I don't really know what use there is in a SportsBottle class but from an "external" point of view, I would expect to see something more like this:




 
Junilu Lacar
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the difference between what you wrote and what I wrote was that in my version, the logic for incrementing the number of rounds performed was inside the goOneRound() method. That's the state of the Person object that is managed by the Person object itself.  Your code managed that state outside of the Person object.

From an external point of view, you shouldn't have to care how that state is managed. All you care is that it does get managed somehow. All you need to do is tell the object to do something and its state should automagically be updated. This is the basis of the Tell, Don't Ask principle of object-oriented programming.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the Design of the classses and the code now the behaviour is manage by the object itself.



What do you think?
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This logic gives an output as expected but I dont like very much in particular how the if...else conditions are....



Any idea, please?
 
Marshal
Posts: 76082
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method called isXXX() is often called a predicate method; it is like a getXXX() method but returns a boolean. It should therefore not take any parameters. If you are passing arguments to that method, it si more like a setXXX() method. But you don't set a bottle full in real life: you fill it, as Junilu showed yesterday. You may fill it full, or you may fill it with any (non‑negative) amount. If you fill it with more water than its size, it contains the same as the size and the remaining water is regarded as having overflowed.
The code looks as if you were guessing and as if you hadn't understood what you have been told already. Junilu showed a call to a method isNotEmpty() which I would probably have written as !myBottle.isEmpty(). I presume you know what the bang sign ! means; if not, look here. It is a bit like the pessimist who says his glass is half‑full and the optimist who says his glass is half‑empty or something like that. That tells you whether there is anything left in the bottle or not. It is like in real life. You don't work out whether a bottle is full or empty by counting how much you have drunk from it. You work out whether it is full or empty by seeing how much water it contains. This is where Junilu's comment about more object‑oriented allows you to take an approach more similar to real life. Nobody goes round thinking, “I have reached the fourth lap; my bottle is therefore empty and I must stop,” but, “I haven't got any more water in my bottle; I had better stop.” The magic number 4 in lines 5 and 11 is inconsistent with that behaviour, unless you have a rule in the logic saying that nobody is allowed to run more than four laps. That rule would not be implemented like that anyway.
I think that method and the design of the classes need taking back to square one. Nobody drinks the bottle's ID; they drink the water. Nobody intends to drink ΒΌ of the bottle per lap; they drink as much water as will stop them being thirsty. Remember bottles come in different sizes; one bottle will not supply enough water for a single lap and others are so large you would have to stop every 5 minutes after drinking a quarter of the bottle. I think you are going to have to turn your computer off and resort to carbon‑based technology: pencil paper and eraser, the latter preferably very large because you are going to have lots of evidence to destroy. Write down in words of one syllable (water and bottle are allowed however) what the logic is. Any computing terms must be removed from the description. Show us your description of the logic and we can progress from there.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The words I can think are : water, bottle, round, sportMan.

The idea for the logic is ; a user has ideClubSport, if yes just grab a filled bottle, else he needs to buy a bottle which is empty. In the last case it gets filled. Now that the bottle for both cases is filled, the user goes starts running, it drinks and a round gets added (At the Junilu example I didnt saw how they are incremented the while would continue forever ?)

Lets see the attachment with the UML diagram.
e1.PNG
[Thumbnail for e1.PNG]
e2.PNG
[Thumbnail for e2.PNG]
e3.PNG
[Thumbnail for e3.PNG]
 
Campbell Ritchie
Marshal
Posts: 76082
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good start

What you haven't said is how much the bottle contains, how much it is filled with (probably the same) and how much the runner drinks each lap.
Junilu's example terminates because the runner stops running when he has drunk the whole bottle.
Separate the part about buying a bottle out. Regard that as a separate process. You will of course have to implement that part, but it will be in a different part of the code.

Isaac Ferguson wrote:. . . UML diagram.

Unless you have been told to use that specific UML diagram, that would only serve to confuse the description.

Suggest: implement a bottle class with size and contents field (size will never change). Give it fill() and drink() methods. Work out how to implement isFull() and isEmpty() methods. Remember the two are mutually exclusive, but both might return false, if the bottle is half‑full or even half‑empty.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am  assuming that the runner drinks 1/3 per lap. So when the rounds are 2, I assume that the bottle is close to be empty... I know it is not very human natural but I cant figure it out without having into account thousand variables... It is my own free UML...
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that drink would go with the SportUser class because he realizes the action of drinking... is it right? Also I can use inheritance from a User class which also drink from  a glass.

Out SportMan drink from a bottle..
 
Campbell Ritchie
Marshal
Posts: 76082
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you “assuming” anything? Where does ⅓ come from? Do people usually drink a third of a bottle rather than a certain amount? So you might pour 100 out of a bottle containing 500, and the runner will drink that 100. In which case you will get five laps before the bottle is empty. Maybe I should have called the Bottle method pour(int) or empty(int) rather than drink(int). Remember that you might be trying to pour more water than the bottle actually contains; If you try pouring 100 out of a bottle containing 150, you will get 100, but if you try to get 200 you will only get 150.
Why have you got height and width for the bottle? How are you validating the capacity against height and width?  Don't those measurements simply add another layer of complexity without giving you any benefits? Similarly, doesn't having a glass also simply add another unnecessary layer of complexity? You want a simple situation working and then afterwards you can consider enhancing it.
 
Junilu Lacar
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:At the Junilu example I didnt saw how they are incremented the while would continue forever ?)


That's precisely the point: any code outside a class should have NO knowledge of how the state is changed; all it needs to know is that it does change when you tell an object to do something.

Your code has external knowledge of state because it has the formula for counting the number of rounds:

First, let's just rename this to runner because the name sportUser is very awkward for the kind of story we're trying to tell here.

This code is like this conversation:

Why would you have to tell the runner object how many rounds it has run? That seems silly.

On the other hand, if you wrote something like this:

That's like this conversation:


One can probably guess that the implementation of the runUpTo() uses a Random object. Or it may not. The point is, from an external point of view, you don't care. You let the runner object take care of those details. All you care is that it does run some arbitrary number of rounds up to a maximum of 20 rounds. Then, after it does that, you can have it tell you how many rounds it actually ran.

Do you get the difference between letting the object manage its state versus you doing it outside of the object?
 
Junilu Lacar
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:Lets see the attachment with the UML diagram.



Sorry, but that's not really a UML diagram, as far as I know. That is just a simple flow chart with a stick person figure thrown in there.
 
Campbell Ritchie
Marshal
Posts: 76082
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that a runner has a full water bottle at the start of the session and runs, drinking a certain amount after each lap. Then the runner stops when the bottle is empty, so as not to overheat. Drinking 150 each lap out of a bottle containing 500 of water means the runner will have to stop after the fourth lap, when the bottle is empty. Are we saying that the runner may decide to run three laps and stop before the water is all drunk?
Agree, Junilu, that it is the runner who remembers how many laps he has run, and the bottle which contains water and might be full or part‑full or empty.
 
Junilu Lacar
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP - if you're trying to learn object-oriented programming/design, a flowchart like the one you created (and again, your diagram is not a UML diagram -- UML is a very specific set of notations) tends to draw you into a very procedural way of thinking which leads to designs that are distinctly not object-oriented.

I normally start out with finding out what needs to be done.

For example, from what you wrote, I might have these:

1. A Person must be a member before they can use the exercise facilities
2. A Person is given a bottle when they are first registered as a member
3. Any member can have their bottle filled up as many times as they need
4. A Person needs to drink after running one round

Then I try to identify "entities" that can represent capabilities and attributes :

Person
====
Can be a member of the club or not a member
Can run one round
Can run multiple rounds
Can remember how many rounds have been run
Can hold a bottle
Can drink from a bottle
Drinks a certain amount from a bottle after running a round

Bottle
====
Can be filled to capacity
Can be partially filled
Can be queried for amount of its contents
Can tell if it is empty or not

For me, these are more important than some of the things you have been concentrating on so far. From here, I can quickly figure out where to put certain logic like incrementing round counts, subtracting from amounts as in the case of drinking from a bottle, and all other kinds of calculations.

The focus of object-orientation is behaviors and capabilities and assigning those to the appropriate abstraction.  Data/information mostly plays a supporting role that enables those behaviors and capabilities.
 
Junilu Lacar
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:I am  assuming that the runner drinks 1/3 per lap. So when the rounds are 2, I assume that the bottle is close to be empty... I know it is not very human natural but I cant figure it out without having into account thousand variables.



Here's the problem: you don't have one. You're like someone walking around with a toolbox looking for things to fix. If you just go around like that, then everything will be a problem to you and you'll try to address each and every one of them. That's how you end up thinking that you have to account for a "thousand variables."

Object-orientation is not really meant to model the real world. It is meant to provide mechanisms of abstraction that make it easier to manage data and the behaviors that operate on that data. The purpose of these abstractions is to organize your design in such a way as to minimize, and in some cases, eliminate certain kinds of dependencies.

I can't really think of a problem I would have to solve using these abstractions. In another related thread, I suggested you might want to have some kind of "smart" bottle that could inform its user how much has been filled and consumed over time. But you insisted that it would be just a plain old water bottle. In that case, I don't really see much value in this example you have concocted.

I would suggest you practice on simple problems, like a game of Tic Tac Toe maybe, and see what kind of abstractions, behaviors, and data related to those behaviors you can identify to help you write a well-designed object-oriented program.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic